Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good method for dynamically drawing clouds with HTML5 canvas and javascript without using images?

I am trying to create a background for my website that uses canvases to draw stuff, get the data URL of it and set it as the background-image property of an element. The method works, but I can't find a good way to draw a cloud on the canvas. Other simpler things like the sun and stars are easy to do without images, and I would prefer to keep the entire script image-less. Drawing a multitude of circles around a point can slightly do it, but I would prefer a more realistic way to do it. Thanks in advance.

like image 511
invisible bob Avatar asked Feb 07 '11 23:02

invisible bob


People also ask

How to draw on a canvas with JavaScript?

1 Create the canvas element — give it an id, and a width/height (HTML) 2 Add base styles — center the canvas, add a background color, etc (CSS) 3 In JavaScript, get your canvas element by using the id 4 Use the canvas element to get the context (your toolbox; more on it later) 5 Use the context to draw

What is HTML Canvas?

What is HTML Canvas? The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.

What are HTML Canvas drawing tools?

It gets even better with the use of HTML canvas drawing tools, which allow users to sketch ideas all from their web browsers and also save it for further use in PNG and other formats. So, we have compiled a list of HTML canvas drawing tools available for anyone or an artist to use.

How to draw on a web page using HTML?

The HTML <canvas> element is used to draw graphics on a web page. The graphic to the left is created with <canvas>. It shows four elements: a red rectangle, a gradient rectangle, a multicolor rectangle, and a multicolor text. The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript.


2 Answers

I have just created Cloudgen.js, an open source library that generates clouds for the canvas. The approach I took uses overlapping circles, each with a radial gradient and transparency. Cloudgen.js provides a way to draw a single cloud with the "drawCloud" method or many clouds at once with "drawCloudGroup". As clouds are roughly circular, using "drawCloudGroup" also allows you to create single clouds with more interesting or predefined shapes.

You can find Cloudgen.js at https://github.com/Ninjakannon/Cloudgen.js

like image 95
Ninjakannon Avatar answered Nov 02 '22 06:11

Ninjakannon


Ok, I have been thinking about the problem of draw clouds, and this is where I am at.

HTMLCanvasElement.prototype.makeCloud=function (xLast, yLast, rLast, lLast){
    rLast = (rLast || 50)
    lLast = lLast || Math.random()*25+75;
    var x = (xLast || 250) + Math.random()*(2*rLast)-rLast/1,
        y = (yLast || 250) + Math.random()*(1*rLast)-rLast/1.5,
        r = (rLast) + Math.random()*10-7.5,
        l = (lLast);

    if(x-r < 0)
        x = r+1;
    else if (x+r > this.width)
        x = this.width-r-1;
    if(y-r < 0)
        y = r+1;
    else if (y+r > this.height)
        y = this.height-r-1;
    if(r <= 0)
        return this;
    if(l<0)
        l=0;
    else if(l>100)
        l=100;

    var ctx=this.getContext('2d');
    ctx.fillStyle='hsl(0, 0%, '+l+'%)';
    ctx.beginPath();
    ctx.arc(x, y, r, 0, Math.PI*2, true);
    ctx.closePath();
    ctx.fill();
    if(Math.random < 0.01)
        return this;
    this.makeCloud(x, y, r, l)
    return this;
} 

It generates some pretty crazy stuff... can anyone think of any improvements? i know editing the canvas prototype isn't the best thing to do, but my application uses a bit of chaining and this makes it flow better.

like image 36
invisible bob Avatar answered Nov 02 '22 04:11

invisible bob