答案 0 :(得分:1)
此快速演示可在指定数量的动画帧中为其个人[startX, startY]
的个人[endX, endY]
动画1 +张图像。
您可以通过侦听IE上的mousewheel
事件和其他浏览器上的DOMMouseScroll
事件来触发它。
快速示例代码和演示:
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var cw=canvas.width;
var ch=canvas.height;
var images=[];
var img=new Image();
img.onload=start;
img.src="https://dl.dropboxusercontent.com/u/139992952/shipSmall.jpg";
function start(){
images.push(defineImageAnimation(img,20,20,200,100,60*2));
requestAnimationFrame(animate);
}
function animate(time){
ctx.clearRect(0,0,cw,ch);
var stillAnimating=false;
for(var i=0;i<images.length;i++){
var img=images[i];
if(img.durationCountdown>0){
img.x+=img.dx;
img.y+=img.dy;
img.durationCountdown--;
stillAnimating=true;
}else{
img.x=img.endX;
img.y=img.endY;
}
ctx.drawImage(img.image,img.x,img.y);
}
if(stillAnimating){
requestAnimationFrame(animate);
}
}
// duration is expressed as how many requestAnimationFrame loops it
// should take the animation to complete. Loops occur about every
// 1/60th of a second
function defineImageAnimation(img,startX,startY,endX,endY,duration){
return({
image:img,
x:startX, y:startY,
endX:endX, endY:endY,
dx:(endX-startX)/duration,
dy:(endY-startY)/duration,
durationCountdown:duration
});
}
&#13;
body{ background-color:white; }
#canvas{border:1px solid red; }
&#13;
<canvas id="canvas" width=512 height=512></canvas>
&#13;