Skip to main content

Image manipulation in canvas



I have a number of images within #mycontainer like:







<div id="mycontainer">

<img src="http://localhost:8080/images/my-image.png" />



</div>







I need to convert those into B/W. Pretty common task but I didn't find any solution for this that would just work for me — there is some problem with the actions execution. What I have now is the following:







function grayscale(src) {

var ctx = document.createElement('canvas').getContext('2d'),

imgObj = new Image(),

pixels, i, n, gs, url;



// wait until the image has been loaded

imgObj.onload = function () {

ctx.canvas.width = this.width;

ctx.canvas.height = this.height;

ctx.drawImage(this, 0, 0);

pixels = ctx.getImageData(0, 0, this.width, this.height);

for (i = 0, n = pixels.data.length; i < n; i += 4) {

gs = pixels.data[i] * 0.3 + pixels.data[i+1] * 0.59 + pixels.data[i+2] * 0.11;

pixels.data[i] = gs; // red

pixels.data[i+1] = gs; // green

pixels.data[i+2] = gs; // blue

}

ctx.putImageData(pixels, 0, 0);

};

imgObj.src = src;

return ctx.canvas.toDataURL('image/png');

}







In general the actions are:





  • I supply src of an image to process,



  • wait for the image to be fully loaded,



  • draw the image on the canvas, convert the pixels and put the converted pixels back on the canvas



  • then I want the data URL of the resulting image from the canvas to be returned.







Right now, when in Developer Tools I am tring something like:







c = $('#mycontainer').find('img')[0];

grayscale(c.src);







I get back data URL of a fully transparent default 300px x 150px canvas as if that imgObj.onload() doesn't exist in the script at all.





Can anybody point me to a mistake here please?


Comments

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex