0
假設我有一個對象(比如一個球),我想移動一個矩形(或正方形,三角形)。我知道我可以使用sin/cos將該物體移動成一個圓形(或任何其他圓形),但是如何移動成一個矩形/三角形?移動一個矩形對象
下面的代碼示例:
function Ball(radius, color) {
this.radius = radius || 40;
this.color = color || "#ff0000";
this.x = 0;
this.y = 0;
this.rotation = 0;
this.scaleX = 1;
this.scaleY = 1;
this.lineWidth = 1;
}
Ball.prototype.draw = function (c) {
// code for drawing ball
}
function drawRightTriangle(h, w, range) {
// Code for triangle
}
// Initialize the ball
var ball = new Ball();
ball.x = cw/2, ball.y = ch/2, ball.radius = 10;
var speed = 0.05,
range = 50,
height = 0,
hypo = 0,
base = 0;
// Here I have use, base = 3, hypo = 5, height = 3 for limits;
(function drawFrame() {
window.requestAnimationFrame(drawFrame);
c.clearRect(0, 0, cw, ch);
ball.x = cw/2 + base*range;
if(base > 3) {
base = 3;
ball.y = ch/2 - height*range;
height += speed;
if(height > 4) {
height = 4;
// Now what to do
// Also, I think this function is messy.
}
}
base += speed;
drawRightTriangle(4, 3, range);
ball.draw(c);
}());
這裏是爲了什麼,我迄今所取得的演示:http://jsfiddle.net/sxy2A/1/
我認爲這將是有益的,如果你還可以添加一些代碼,你已經有,使用正弦/餘弦,這樣我們就可以更好地看到它移動它。 – mavrosxristoforos