简体   繁体   English

将鼠标悬停在动画中的HTML-5画布图像上以播放/暂停

[英]mouse-over HTML-5 canvas image in animation to play/pause it

I have a HTML-5 canvas in which there is a circle which is animated. 我有一个HTML-5画布,其中有一个动画的圆圈。 Refer to the following JS code. 请参考以下JS代码。

function doFirst(){
   canvas = document.getElementById('myCanvas');
   context = canvas.getContext('2d');
   centerX = canvas.width / 4;
   centerY = canvas.height / 4;
   radius = 20;    
   dy=4;
   drawCircle(centerX,centerY,radius);
   myVar=setInterval(moveCirc,40);      
}

function moveCirc(){                
context.clearRect(0,0,canvas.width,canvas.height);
drawCircle(centerX,centerY,radius);
centerY+=dy;
if(centerY>canvas.height-50)
  clearInterval(myVar);     
}

function drawCircle(x,y,r){     
context.lineWidth = 2;
context.strokeStyle = '#003300';
context.fillStyle = 'blue';
context.beginPath();
context.arc(x, y, r, 0, 2 * Math.PI, true);
context.stroke();
context.fill();
}
window.addEventListener("load",doFirst,false);

I want that whenever the user brings his mouse over the circle, the animation should pause and a message should be displayed. 我希望每当用户将鼠标移到圆圈上时,动画都应暂停并显示一条消息。 When the user moves his mouse out of the circle, then the animation should start again from the point where it paused, and also, the message should disappear. 当用户将鼠标移出圆圈时,动画应从其暂停的位置重新开始,并且消息也应消失。 Please help me in achieving this functionality. 请帮助我实现此功能。

If you are using css3 animation then on mouse over change the css property 如果使用css3动画,则将鼠标悬停在css属性上

-webkit-animation-play-state: to paused to pause your animation ; -webkit-animation-play-state: 暂停以暂停动画;

and to start the animation again from that point change the css property 然后从该点再次开始动画,更改css属性

-webkit-animation-play-state: to running -webkit-animation-play-state: 运行

source : http://css-infos.net/property/-webkit-animation-play-state 来源: http : //css-infos.net/property/-webkit-animation-play-state

NB " -webkit " for webkit based browser; NB-webkit ”用于基于webkit的浏览器; use " -moz " for Mozilla , " -o " for opera etc. 对于Mozilla使用“ -moz ”,对于Opera等使用“ -o ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM