Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your demo code, you should be changing the temporary canvas width/height, not the context’s.</p> <pre><code> helperCanvas.width = image.width; helperCanvas.height = image.height; </code></pre> <p>Here is code with a test filter that just turns all non-transparent pixels red.</p> <p>It also renders the filtered canvas image to an image on the page.</p> <p>BTW, when creating an image object, there is a new Chrome bug that can be avoided if you create like this:</p> <pre><code> var img=document.createElement("img"); </code></pre> <p>Fiddle that must be viewed in Chrome or FF (IE==CORS failure): <a href="http://jsfiddle.net/m1erickson/LeGD5/" rel="nofollow">http://jsfiddle.net/m1erickson/LeGD5/</a></p> <p>Here is code: </p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /&gt; &lt;!-- reset css --&gt; &lt;script type="text/javascript" src="http://code.jquery.com/jquery.min.js"&gt;&lt;/script&gt; &lt;style&gt; body{ background-color: ivory; } canvas{border:1px solid red;} &lt;/style&gt; &lt;script&gt; $(function(){ var canvas=document.createElement("canvas"); var ctx=canvas.getContext("2d"); var img=document.createElement("img"); img.onload=function(){ canvas.width=img.width; canvas.height=img.height; ctx.drawImage(img,0,0,img.width,img.height); // test -- turn every non-transparent pixel red var imgData = ctx.getImageData(0, 0, canvas.width, canvas.height); var pixels = imgData.data; // get pixel data for (var i = 0; i &lt; pixels.length; i +=4) { // if this pixel is not transparent, // mask it in pure red if(pixels[i+3]&gt;0){ pixels[i]=255; // this is the red component of the pixel pixels[i+1]=0; // this is the green component of the pixel pixels[i+2]=0; // this is the blue component of the pixel pixels[i+3]=255; // this is the alpha component of the pixel } } ctx.putImageData(imgData, 0, 0); var theImage=document.getElementById("theImage"); theImage.src=canvas.toDataURL(); } img.crossOrigin="anonymous"; img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/KoolAidMan.png"; }); // end $(function(){}); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;img id="theImage" width=300 height=300&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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