我想要一些帮助,使形状随机出现在我的画布上并保持在它们出现的位置。目前我有一个按钮,按下时会出现三个形状,但我希望这三个形状在画布周围连续出现,直到再次按下按钮。
HTML:
<canvas id="example1" width="800" height="600">
<p class="canvas-no-support">Your Browser Does Not Support HTML5 canvas!</p>
</canvas>
<button onclick="start()">Go!!</button>
脚本:
function start() {
setInterval(draw, 100);
}
function draw() {
// get the canvas element using the DOM
var canvas = document.getElementById('example1');
// Make sure we don't execute when canvas isn't supported
if (canvas.getContext) {
var context = canvas.getContext('2d');
//Red Square
function dSqu() {
context.strokeStyle = "rgb(255,215,0)"; //Yellow Outline
context.fillStyle = "rgb(200,0,0)";
context.lineWidth = 4; //Stroke Thickness
context.fillRect(75, 75, 137, 137);
context.strokeRect(75, 75, 137, 137); //Outline
}
function dCir() {
//Purple Circle
context.beginPath();
context.arc(380, 137, 100, 0, Math.PI * 2, true);
context.fillStyle = "rgb(200,80,200)";
context.fill();
context.lineWidth = 4; //Stroke Thickness
context.strokeStyle = "rgb(255,215,0)";
context.stroke();
}
function dTri() {
//Green Triangle
context.beginPath();
context.fillStyle = "rgb(0,200,0)";
context.moveTo(100, 100);
context.lineTo(300, 100);
context.lineTo(200, 200);
context.lineTo(100, 100);
context.fill();
context.strokeStyle = "rgb(255,215,0)";
context.stroke();
ctx.closePath();
}
dSqu();
dCir();
dTri();
} else {
document.write('Your Browser does not support HTML5 Canvas.');
}
}