-3

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

4

3 回答 3

6

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

于 2013-10-23T12:16:42.253 回答
3

您可以使用弧形方法创建云形状。看一下这个。

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>

于 2020-10-02T14:16:23.700 回答
-1

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

于 2021-04-07T06:22:03.097 回答