Animando um único valor dentro de uma função
-
13-12-2019 - |
Pergunta
Estou tentando animar uma forma que criei em HTML5.
myShape.drawFunc = function(){
var context = this.getContext();
context.beginPath();
context.arc(480, 480, animationValue, startingPoint * Math.PI, endingPoint * Math.PI, false);
context.lineTo(480,480);
context.closePath();
this.fillStroke();
}
Quero usar jQuery.animate para alterar o animationValue
de um valor para outro ao longo do tempo.Não tenho certeza de como conseguir isso, também precisarei executar uma função em cada etapa da animação Layer.draw();
porque minha forma é uma forma de tela.
Alguém sabe como animar o animationValue
em myShape.drawFunc?
Devo acrescentar que estou tentando animar dentro de um myShape.on("mouseover",{});
isso representa algum problema ao usar o método setInterval etc?
ATUALIZAR:
segment.on("mouseover",function(){
var startingPoint = segData[index].startingPoint;
var endingPoint = segData[index].endingPoint;
var startingRadias = segData[index].radias;
var maxRadias = 250;
var updateRadias = startingRadias;
setInterval(function(){
if(updateRadias < maxRadias){
updateRadias++;
console.log("frame : "+updateRadias);
this.drawFunc = function(){
var context = this.getContext();
context.beginPath();
context.arc(480, 480, updateRadias, startingPoint * Math.PI, endingPoint * Math.PI, false);
context.lineTo(480,480);
context.closePath();
this.fillStroke();
}
rawLayer.draw();
}
},1000/30);
});
o console.log está mostrando que setInterval está sendo chamado, mas a forma não parece ter sido redesenhada.
Solução
Sim, você pode usar animate()
para mudar o seu animationValue
de um valor para outro ao longo do tempo:
var drawFunc = function (animationValue) {
var context = $("#myCanvas")[0].getContext("2d");
context.fillStyle="#FF0000";
context.fillRect(animationValue, animationValue, 150, 75);
},
from = 0, to = 50;
// now animate using the jQuery.animate()
$({ n: from }).animate({ n: to}, {
duration: 1000,
step: function(now, fx) {
drawFunc(now);
}
});
Neste caso, estamos animando de 0 a 50 em 1 segundo.
Este não é exatamente o seu código de animação, mas você deve pegar o jeito. Demonstração aqui
Mais informações sobre animando variáveis
Outras dicas
var FPS = 30;
setInterval(function() {
update();
draw();
}, 1000/FPS);