无法使用画布更改图像像素

时间:2019-07-05 21:03:08

标签: javascript canvas

我有这段代码,我想同时更改一个灰色像素,但它只是复制了testImage:

 $("body").append('<canvas id="canvas"></canvas>');
                var image = document.getElementById('testImage');
                var canvas = document.getElementById('canvas');
                canvas.width = image.width;
                canvas.height = image.height;
                var ctx = canvas.getContext("2d");
                ctx.drawImage(image, 0, 0);
                var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
                var data = imgData.data;
                for (var i = 0; i < data.length; i += 4)
                    {

                            data[i] = data[i + 1] = data[i + 2] = data[i + 3] = 255;                        

                    }              

                ctx.putImageData(imgData, image.width, image.height);

我犯了什么错误?

0 个答案:

没有答案