4

我似乎遇到了一条错误消息,表明我很难调试。我想要做的是 用画布动态创建一个图像,比如这个( http://www.webreference.com/programming/javascript/mk/column3/creating/cp_mini_gradient_details.png )。

我收到的错误消息是:

[Exception... "An invalid or illegal string was specified" code: "12" nsresult: "0x8053000c (NS_ERROR_DOM_SYNTAX_ERR)"]

我用谷歌搜索,似乎有很多原因导致此错误消息,但我似乎找不到任何与画布相关的具体内容。

这是我的代码:

var newCanvas = document.createElement('canvas');
newCanvas.height="30";
newCanvas.width="113";
document.body.appendChild(newCanvas);

var context = newCanvas.getContext('2d');

context.fillStyle = '#FFFFFF';  
context.fillRect(0, 0, 113, 15);
context.fillStyle = '#000000';  
context.fillRect(0, 15, 113, 30);

var numCanH = Number(newCanvas.height);
var numCanW = Number(newCanvas.width);
var imgd;
if (context.createImageData) {
    console.log('context.createImageData');
    imgd = context.createImageData(numCanW, numCanH);
} 
else if (context.getImageData) {
    console.log('context.getImageData');
    imgd = context.getImageData(0, 0, numCanW, numCanH);
} 
else {
    console.log('else');
    imgd = {'width' : numCanW, 'height' : numCanH, 'data' : new Array(numCanW*numCanH*4)};
}  
var pix = imgd.data;
var ndv = numCanW/6;

for (var i = 0; i <= numCanH; i++) {

    var a=1-Math.abs(2*i-numCanH)/numCanH;

    for (var j = 0; j < numCanW; j+=4) {

        var bitUp = Math.ceil((255/130)*j);
        var bitDown = 255-bitUp;

        if(j<(ndv)){    //Red to Yellow - (rgb) 255,0,0 to 255,255,0
            pix[j] = 255; // red channel
            pix[j+1] = bitUp; // green channel
            pix[j+2] = 0; // blue channel 
        }    
        else if(j>(ndv) && j<(ndv)*2){  //Yellow to Green - (rgb) 255,255,0 to 0,255,0
            pix[j] = 255; // red channel
            pix[j+1] = bitDown; // green channel
            pix[j+2] = 0; // blue channel 
        }  
        else if(j>(ndv)*2 && j<(ndv)*3){    //Green to Cyan - (rgb) 0,255,0 to 0,255,255
            pix[j] = 0; // red channel
            pix[j+1] = 255; // green channel
            pix[j+2] = bitUp; // blue channel 
        }  
        else if(j>(ndv)*3 && j<(ndv)*4){    //Cyan to Blue - (rgb) 0,255,255 to 0,0,255
            pix[j] = 0; // red channel
            pix[j+1] = bitDown; // green channel
            pix[j+2] = 255; // blue channel 
        }  
        else if(j>(ndv)*4 && j<(ndv)*5){    //Blue to Magenta - (rgb) 0,0,255 to 255,0,255
            pix[j] = bitUp; // red channel
            pix[j+1] = 0; // green channel
            pix[j+2] = 255; // blue channel 
        }  
        else if(j>(ndv)*5 && j<(ndv)*6){    //Magenta to Red - (rgb) 255,0,255 to 255,0,0
            pix[j] = 255; // red channel
            pix[j+1] = 0; // green channel
            pix[j+2] = bitDown; // blue channel 
        }             

        pix[j+3] = a; // alpha channel+

        console.log('bitUp  '+bitUp);
        console.log(typeof bitUp);
        console.log('bitDown  '+bitDown);
        console.log(typeof bitDown);
        console.log('a  '+j);
        console.log(typeof a);
        console.log('j  '+j);
        console.log(typeof j);
        console.log('i  '+i);
        console.log(typeof i);
        console.log(imgd);
        console.log('before context.putImageData');
        context.putImageData(imgd, j,i);
        console.log('after context.putImageData');      
    }
}

奇怪的是,错误只发生在循环的第二次迭代期间。

4

1 回答 1

3

您的 putImageData 调用正在尝试将您创建为画布宽度和高度的整个 imgd 绘制到左上角位于 (j,i) 的画布中。这在 lop 的第一次迭代后失败,因为 imgd 的底部/右侧离开了画布的底部/右侧。

在外循环之外调用 putImageData 并使其始终在 (0,0) 处绘制。此外,在计算您正在设置的像素的数组索引时,不要忘记添加 i*numCanW*4; 请注意,您的内部循环需要从 0 到 iCanW*4 而不是 0 到 iCanW,因为您正在对颜色分量进行交互,而不是像素。

于 2009-02-25T17:09:13.130 回答