【发布时间】:2014-09-17 11:01:47
【问题描述】:
我今天正在查看 HTML 5 画布,我想在画布上移动 3 个圆圈。从我目前所读到的内容来看,我需要每次重新绘制圆圈(每 60 毫秒似乎是一个不错的起点)并清除旧圆圈,以便在屏幕上具有新位置的新圆圈取代它
到目前为止,我有一个draw(),它将渲染 3 个不同颜色的圆圈,其想法是控制每个圆圈。
我在这里寻找一些关于如何初始设置和让每个球移动的指导。
这是我目前所拥有的:
<html>
<head>
<title>Ball Demo</title>
<style type="text/css">
#ball-canvas {
border: 1px solid #666;
}
</style>
</head>
<body>
<canvas id="ball-canvas" width="900" height="600"></canvas>
<script>
function renderCircle(context, x, y, radius, fill) {
var strokeWidth = 2
context.beginPath();
context.arc(x, y, radius - (2 * strokeWidth), 0, 2 * Math.PI, false);
context.fillStyle = fill;
context.fill();
context.lineWidth = strokeWidth;
context.strokeStyle = '#666';
context.stroke();
}
function draw() {
renderCircle(context, radius, canvas.height / 2, radius, 'yellow');
renderCircle(context, canvas.width / 2, canvas.height / 2, radius, 'blue');
renderCircle(context, canvas.width - radius , canvas.height / 2, radius, 'red');
}
var canvas = document.getElementById('ball-canvas');
var context = canvas.getContext('2d')
var radius = 50;
setInterval(draw, 1000 / 60);
</script>
</body>
【问题讨论】:
标签: javascript html canvas html5-canvas