Я пытаюсь использовать clearTimeout, но не удалось, может ли кто-нибудь показать мне, как остановить или приостановить эту анимацию? Ниже мой Javascript код:Как остановить или приостановить эту программу?
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000/60);
};
})();
function drawRectangle(myRectangle, context) {
context.beginPath();
context.rect(myRectangle.x, myRectangle.y, myRectangle.width, myRectangle.height);
context.fillStyle = 'green';
context.fill();
context.lineWidth = myRectangle.borderWidth;
context.strokeStyle = 'black';
context.stroke();
}
function myFunction() {
var m = document.getElementById("myRange").value;
document.getElementById("demo").innerHTML = m;
}
function animate(myRectangle, canvas, context, startTime) {
var time = (new Date()).getTime() - startTime;
var m = document.getElementById("myRange").value;
myRectangle.y = 380 - m * Math.cos(time * Math.PI/2000);
context.clearRect(0, 0, canvas.width, canvas.height);
drawRectangle(myRectangle, context);
requestAnimFrame(function() {
animate(myRectangle, canvas, context, startTime);
});
}
var myRectangle = {
x: 150,
y: 150,
width: 50,
height: 50,
borderWidth: 4
};
drawRectangle(myRectangle, context);
setTimeout(function() {
var startTime = (new Date()).getTime();
animate(myRectangle, canvas, context, startTime);
}, 0);
Чтобы "остановить" анимационной прекратить называть 'requestAnimFrame (...)' в 'одушевленные()' – Andreas
Это полный сценарий? Где вы объявляете холст и контекст? – Seth