我想我重新加载图像的次数太多,或者我的循环中有错误,但这是我的第一个画布项目,所以我不确定如何处理调用和方法。有人介意帮我一把或解释一下吗?
顺便提一句; 我需要在网站上具有不同模式的这些,因此需要向量。我<canvas id="CSCanvas" width="2800" height="2000"></canvas>
在“wave”div 中使用了另外三个具有不同名称和矢量位置(否则它们相同)和不同 canvas() 的 animateC() 函数。这是正确的方法吗?
这是脚本..
$(document).ready(function(){
var offset = 0;
var offset2= 0;
var delta = 1;
var delta2 = .5;
var wavesCanvas = [ 'CCanvas', 'CSCanvas', 'SCanvas', 'SSCanvas' ];
var wavesIMG = ['wt1', 'shadow', 'wt2', 'shadow'];
function animateC() {
var wavevar=0;
var imgname=wavesIMG[wavevar];
var canvasname=wavesCanvas[wavevar];
var Ccanvas = document.getElementById(canvasname);
var Ccontext = Ccanvas.getContext('2d');
var pattern = new Image();
pattern.onload = function(){
Ccontext.clearRect(0,0,Ccanvas.width,Ccanvas.height);
Ccontext.save();
Ccontext.translate(-offset, 0);
Ccontext.beginPath();
Ccontext.moveTo(0, Ccanvas.height);
Ccontext.lineTo(0, 0);
var waux=120;
for(i=0;i<50;i++){
Ccontext.quadraticCurveTo(5+(waux*i), 0, (10+(waux*i)), 6);
Ccontext.quadraticCurveTo((60+(waux*i)), 56, (120+(waux*i)), 6);
}
Ccontext.lineTo(Ccanvas.width, Ccanvas.height);
Ccontext.closePath();
var fillP = Ccontext.createPattern(pattern,'repeat');
Ccontext.fillStyle = fillP;
Ccontext.fill();
};
pattern.src= './images/'+imgname+'.png';
Ccontext.restore();
offset += delta;
if (offset > 120) offset=0;
requestAnimationFrame(animateC);
}
window.requestAnimationFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 3000 / 60);
};
})();
animateC();
});
..这是HTML ..
<div id='wave'>
<canvas id="CCanvas" width="2800" height="2000"></canvas>
</div>