Skip to content Skip to sidebar Skip to footer

How To Draw Cloud Shape In Html5 Canvas?

I am working on drawing of different shapes using HTML5 Canvas & javascript. But i am not gettting the calculation for drawing CLOUD shape in HTML5Canvas. I am trying to draw C

Solution 1:

Ref. Link : http://www.html5canvastutorials.com/advanced/html5-canvas-save-drawing-as-an-image/

var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

// draw cloud
context.beginPath();
context.moveTo(170, 80);
context.bezierCurveTo(130, 100, 130, 150, 230, 150);
context.bezierCurveTo(250, 180, 320, 180, 340, 150);
context.bezierCurveTo(420, 150, 420, 120, 390, 100);
context.bezierCurveTo(430, 40, 370, 30, 340, 50);
context.bezierCurveTo(320, 5, 250, 20, 250, 50);
context.bezierCurveTo(200, 5, 150, 20, 170, 80);
context.closePath();
context.lineWidth = 5;
context.fillStyle = '#8ED6FF';
context.fill();
context.strokeStyle = '#0000ff';
context.stroke();

// save canvas image as data url (png format by default)
var dataURL = canvas.toDataURL();

// set canvasImg image src to dataURL
// so it can be saved as an image
document.getElementById('canvasImg').src = dataURL;
body {
  margin: 0px;
  padding: 0px;
}
<!DOCTYPE HTML>
<html>

<head>
</head>

<body>
  <canvas id="myCanvas" width="578" height="200" style="display:none;"></canvas>
  <img id="canvasImg" alt="Right click to save me!">
</body>

</html>

Solution 2:

You can create a cloud shape using the arc method. Check this out.

var canvas = document.querySelector('canvas');
var c = canvas.getContext('2d');

const drawCloud = (x, y) => {
    c.beginPath();
    c.arc(x, y, 60, Math.PI * 0.5, Math.PI * 1.5);
    c.arc(x + 70, y - 60, 70, Math.PI * 1, Math.PI * 1.85);
    c.arc(x + 152, y - 45, 50, Math.PI * 1.37, Math.PI * 1.91);
    c.arc(x + 200, y, 60, Math.PI * 1.5, Math.PI * 0.5);
    c.moveTo(x + 200, y + 60);
    c.lineTo(x, y + 60);
    c.strokeStyle = '#797874';
    c.stroke();
    c.fillStyle = '#8ED6FF';
    c.fill()
}
drawCloud(100,135);
body {
  margin: 0px;
  padding: 0px;
}
<!DOCTYPE HTML>
<html>
    <head>
    </head>
    <body>
        <canvas width="500" height="300"></canvas>
    </body>
</html>

Solution 3:

see image to pick code for draw cloud in canvas html5 [1]: https://i.stack.imgur.com/s3Yd2.png


Post a Comment for "How To Draw Cloud Shape In Html5 Canvas?"