Note that there are some explanatory texts on larger screens.

plurals
  1. POTransparency lost with getImageData - HTML5 2d Context
    primarykey
    data
    text
    <p>I noticed a strange problem with <code>getImageData</code>; transparency of the image seems to be ignored when the image data is fetched. </p> <p>Since any image needs to be drawn on to a <code>canvas</code> before its image data can be obtained, I assumed this was a problem with the <code>canvas</code> in question being opaque. But I was wrong, since using the <code>canvas</code> as an argument in <code>drawImage</code> maintains transparency.</p> <p>Here is how I loaded the image;</p> <pre><code>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); }; </code></pre> <p>The <code>callback</code> is simply the draw function, which invokes <code>draw_image</code> to draw the image.</p> <p>The usual version;</p> <pre><code>var draw_image = function(ctx, img, sx, sy, w, h, dx, dy) { ctx.drawImage(img.canvas, sx, sy, w, h, dx, dy, w, h); }; </code></pre> <p>It simply takes the canvas as an argument for <code>drawImage</code>, and the result is as intended with transparency maintained. <strong><a href="http://jsfiddle.net/MnG7a/3/" rel="nofollow">Example</a></strong>.</p> <p>The image data version;</p> <pre><code>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); }; </code></pre> <p>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. <strong><a href="http://dl.dropbox.com/u/80133449/imagedataTest/index.html" rel="nofollow">Example</a></strong>. (This is a Dropbox link because of the <code>origin-clean</code> flag.)</p> <p>Am I wrong in assuming that transparency should be maintained with <code>getImageData</code>? Or am I just using it in a wrong manner?</p> <p>Either way, help would really be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload