如何在HTML5 Canvas中绘制云形状?

时间:2013-10-23 12:05:25

标签: javascript html5 canvas

我正在使用HTML5 Canvas& amp; JavaScript的。但我没有得到在HTML5Canvas中绘制CLOUD形状的计算。 我试图使用2个点(即起点和终点)绘制CLOUD形状。 请提出一些想法? 感谢

3 个答案:

答案 0 :(得分:5)

参考。链接: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>

答案 1 :(得分:0)

您可以使用arc方法创建云形状。检查一下。

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>

答案 2 :(得分:0)

查看图片以选择在 canvas html5 中绘制云的代码 [1]:https://i.stack.imgur.com/s3Yd2.png