当谈到 HTML5 和臭名昭著的Canvas元素时,我是个新手。目前,我在我的网页上绘制了一个蓝色球,单击画布元素时,球滑动到我传递给 drawCircle 函数的位置 (Y)。我想将球滑到Y 位置,而球跳到 Y 位置。
到目前为止,这是我的代码:
var context, canvas;
var x = 100;
var y = 200;
var dx = 5;
var dy = .02;
function drawCircle(move) {
if(move) {
x = move.x
y = move.y
}
canvas = document.getElementById('myCanvas');
context = canvas.getContext('2d')
context.clearRect(0,0,canvas.width, canvas.height);
context.beginPath()
context.fillStyle = "#0000ff";
context.arc(x, y, 20, 0, Math.PI*2, true);
context.closePath();
context.fill();
// if(x < 0 || x > canvas.width) dx=-dx;
// if(y < 0 || y > canvas.height) dy =- dy;
if(move) {
y+=dy
}
// x+=dx
// y+=dy
}
window.onload = function(e){
// setInterval(drawCircle, 10)
drawCircle()
canvas.onclick = function(){
var move = {
x: 100,
y: 100
}
drawCircle(move)
}
}
JSFiddle:http: //jsfiddle.net/Uam8z/1/