admin管理员组文章数量:1332896
I noticed a strange problem with getImageData
; transparency of the image seems to be ignored when the image data is fetched.
Since any image needs to be drawn on to a canvas
before its image data can be obtained, I assumed this was a problem with the canvas
in question being opaque. But I was wrong, since using the canvas
as an argument in drawImage
maintains transparency.
Here is how I loaded the image;
var load_image = function(name, url, holder, callback) {
var img = new Image();
img.src = url;
img.addEventListener('load', function(e) {
var canvas = make_canvas(e.target.width, e.target.height);
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(e.target, 0, 0);
holder[name] = {canvas: canvas, ctx: ctx};
delete e.target;
callback.call();
}, false);
};
The callback
is simply the draw function, which invokes draw_image
to draw the image.
The usual version;
var draw_image = function(ctx, img, sx, sy, w, h, dx, dy) {
ctx.drawImage(img.canvas, sx, sy, w, h, dx, dy, w, h);
};
It simply takes the canvas as an argument for drawImage
, and the result is as intended with transparency maintained. Example.
The image data version;
var draw_image = function(ctx, img, sx, sy, w, h, dx, dy) {
var imagedata = img.ctx.getImageData(sx, sy, w, h);
ctx.putImageData(imagedata, dx, dy);
};
This one obtains the image data of the required rectangle from the same canvas as the one used in the usual version, and puts the image data on the canvas I want to draw to. I believe transparency should be maintained, but this is not so. Example. (This is a Dropbox link because of the origin-clean
flag.)
Am I wrong in assuming that transparency should be maintained with getImageData
? Or am I just using it in a wrong manner?
Either way, help would really be appreciated.
I noticed a strange problem with getImageData
; transparency of the image seems to be ignored when the image data is fetched.
Since any image needs to be drawn on to a canvas
before its image data can be obtained, I assumed this was a problem with the canvas
in question being opaque. But I was wrong, since using the canvas
as an argument in drawImage
maintains transparency.
Here is how I loaded the image;
var load_image = function(name, url, holder, callback) {
var img = new Image();
img.src = url;
img.addEventListener('load', function(e) {
var canvas = make_canvas(e.target.width, e.target.height);
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(e.target, 0, 0);
holder[name] = {canvas: canvas, ctx: ctx};
delete e.target;
callback.call();
}, false);
};
The callback
is simply the draw function, which invokes draw_image
to draw the image.
The usual version;
var draw_image = function(ctx, img, sx, sy, w, h, dx, dy) {
ctx.drawImage(img.canvas, sx, sy, w, h, dx, dy, w, h);
};
It simply takes the canvas as an argument for drawImage
, and the result is as intended with transparency maintained. Example.
The image data version;
var draw_image = function(ctx, img, sx, sy, w, h, dx, dy) {
var imagedata = img.ctx.getImageData(sx, sy, w, h);
ctx.putImageData(imagedata, dx, dy);
};
This one obtains the image data of the required rectangle from the same canvas as the one used in the usual version, and puts the image data on the canvas I want to draw to. I believe transparency should be maintained, but this is not so. Example. (This is a Dropbox link because of the origin-clean
flag.)
Am I wrong in assuming that transparency should be maintained with getImageData
? Or am I just using it in a wrong manner?
Either way, help would really be appreciated.
Share Improve this question edited Jan 1, 2013 at 13:55 Rikonator asked Jan 1, 2013 at 13:11 RikonatorRikonator 1,86016 silver badges27 bronze badges 01 Answer
Reset to default 8I believe your problem is that putImageData doesn't use a posite operation to blend the source and destination image data. Rather, it's doing a direct write of the red, green, blue, and alpha channels into the canvas. For fully transparent pixels, this means they may or may not appear in the color you expect.
Instead, you can create an intermediate canvas element, draw to that, and then use drawImage() to render that to your target canvas with the appropriate globalCompositeOperation being applied. Mask for putImageData with HTML5 canvas? discusses this issue in more detail.
Update: You can verify that the data you're writing into your "broken" example actually does have transparent data in it by doing ctx.getImageData(230,50,1,1).data
. The result is [0,0,0,0] - i.e. a fully transparent pixel.
本文标签: javascriptTransparency lost with getImageDataHTML5 2d ContextStack Overflow
版权声明:本文标题:javascript - Transparency lost with getImageData - HTML5 2d Context - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742316590a2451938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论