【发布时间】:2012-03-20 22:30:21
【问题描述】:
如果我删除“alert(stepHor);”线,我的画布上不会画任何东西。但是如果我让警报(stepHor),一切都按预期进行。如果它有类似的 img.onload,我会理解,但在这里我没有。它与“关闭”有关吗?提前致谢。
function draw(imageData) {
var canvas=document.getElementById('bg');
if(canvas.getContext){
var ctx=canvas.getContext('2d');
ctx.scale(0.4,0.4);
var stepHor=Math.floor(imageData.width/imax);
var stepVer=Math.floor(imageData.height/jmax);
alert(stepHor);
for(i=0;i<=imax;i++){
for(j=0;j<=jmax;j++){
var index=(j*stepVer+Math.floor((imageData.height-jmax*stepVer)/2))*(4*imageData.width)+(i*stepHor*4)+Math.floor((imageData.width-imax*stepHor)/2)*4;//the first 'Math.floor' member is to center the image vertically, the second 'Math.floor' member is to center the image horizontally
var red=imageData.data[index];
var green=imageData.data[index+1];
var blue=imageData.data[index+2];
var alpha=imageData.data[index+3];
ctx.fillStyle='rgb(' +red+ ',' +green+ ',' +blue+ ')';
ctx.fillRect(wireWidth*i,wireHeight*j,wireWidth,wireHeight);
}
}
}
}
【问题讨论】:
-
何时/如何调用这个函数?
imax变量来自哪里? -
imax 和 jmax 是全局变量,我这样定义它们:
var imax=Math.floor($(window).width()/wireWidth); var jmax=Math.floor($(window).height()/wireHeight);其中一个“线”实际上是一个“像素”,我在全局变量中定义它的大小
标签: javascript html canvas closures