여기 내 코드가 있습니다. 캔버스에 VIBGOYR 패턴을 생성하고 시간 간격 후에 시스템의 방사형 그래디언트를 변경하여 애니메이션으로 보이게하려고합니다.
setTimeout() 및 캔버스가 의도 한대로 작동하지 않습니다.
function activate(index){
canvas =document.getElementById("exp");
context = canvas.getContext('2d');
//Start.
draw(index);
//Functions.
function draw(index){
drawGradiantSq(index);
var t=setTimeout(doTimer,1000);
}
function doTimer(){
draw(index+10);
}
function drawGradiantSq(fi){
console.log(fi);
var g1 = context.createRadialGradient(0,300,0,500,300,fi);
colors = ["violet","indigo","blue","green","orange","yellow","red"];
var x= 1/12;
for (var i=0;i<colors.length;i++){
g1.addColorStop(i*x,colors[i]);
}
context.fillStyle = g1;
context.fillRect(0,0,500,600);
var g2 = context.createRadialGradient(1000,300,0,500,300,fi);
for (var i=0;i<colors.length;i++){
g2.addColorStop(i*x,colors[i]);
}
context.fillStyle = g2;
context.fillRect(500,0,1000,600);
}
}
<body>
<canvas id="exp" width="1000px" height="600px"></canvas>
<button onclick="activate(index+=10);">Paint</button>
</body>
변수 지수는 시도 setInterval
대신 setTimeout
사용하여 0
지수는 변경되지 않고 유지'기능 doTimer은() {그릴 (인덱스 + = 10);} 시도' – Leonid