问候语,
使用javascript,我试图制作一个非常简单的动画,一个图像从一个x和y坐标移动到另一个x和y坐标。
我有4个常数,例如:
var startX = 0; //starting X of an image
var startY = 0; //starting Y of an image
var endX = 100; //ending X of an image
var endY = 200; //ending Y of an image
//these 2 are used for keeping the "current" position of animated image,
var currentX = startX ;
var currentY = startY ;
//every 150 ms, updates the location of the coordinates
function move(){
if( (currentX == endX) && (currentY == endY) )
break;
if(currentX < endX){
currentX = currentX + step_amount;
}
if(currentX > endX){
currentX = currentX - step_amount;
}
if(currentY < endY){
currentY = currentY + step_amount;
}
if(currentY > endY){
currentY = currentY - step_amount;
}
setInterval("move()", 150);
}
这样做的工作,但它并不顺利,如果你能帮助我改进我的天真算法,为更好的移动功能,总是走“最短路径”,我将不胜感激。
最佳答案
听起来你需要Bresenham line drawing算法。