【发布时间】:2015-07-21 02:36:57
【问题描述】:
我在画布上画了一片叶子,但现在我想在画布上和随机位置画多次。我主要使用贝塞尔曲线来绘制我的叶子,我不知道如何使用循环在随机位置创建更多的叶子,因为坐标。
我的代码:
<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="800" height="800" style="border:1px solid #c3c3c3;">
</canvas>
<script>
var c = document.getElementById("myCanvas");
var context = c.getContext("2d");
context.lineWidth = 5;
context.beginPath();
context.moveTo(100, 150);
context.strokeStyle="#009900";
context.bezierCurveTo(170, 110, 400, 10, 500, 150);
context.stroke();
context.moveTo(100, 150);
context.strokeStyle="#009900";
context.bezierCurveTo(170, 130, 430, 310, 500, 150);
context.stroke();
context.fillStyle = '#99FF66';
context.fill();
context.strokeStyle="#009900";
context.moveTo(250, 150);
context.bezierCurveTo(400, 100, 400, 180, 500, 150);
context.stroke();
context.closePath();
context.beginPath();
context.strokeStyle="#996633";
context.moveTo(500, 150);
context.lineTo(580,150);
context.stroke();
context.closePath();
</script>
</body>
</html>
【问题讨论】:
-
随机生成一个 X 和 Y 值作为起点并将其添加到所有初始值中。
标签: javascript html loops canvas html5-canvas