How do i tint an image with HTML5 Canvas? -


my question is, best way tint image drawn using drawimage method. target useage advanced 2d particle-effects (game development) particles change colors on time etc. not asking how tint whole canvas, current image draw.

i have concluded globalalpha parameter affects current image drawn.

//works drawimage() canvas2d.globalalpha = 0.5; 

but how tint each image arbitrary color value ? awesome if there kind of globalfillstyle or globalcolor or kind of thing...

edit:

here screenshot of application working with: http://twitpic.com/1j2aeg/full alt text http://web20.twitpic.com/img/92485672-1d59e2f85d099210d4dafb5211bf770f.4bd804ef-scaled.png

you have compositing operations, , 1 of them destination-atop. if composite image onto solid color 'context.globalcompositeoperation = "destination-atop"', have alpha of foreground image, , color of background image. used make tinted copy of image, , drew tinted copy on top of original @ opacity equal amount want tint.

here full code:

<!doctype html public "-//w3c//dtd html 4.01//en" "http://www.w3.org/tr/html4/strict.dtd"> <html>     <head>         <title>html5 canvas test</title>         <script type="text/javascript"> var x; //drawing context var width; var height; var fg; var buffer  window.onload = function() {     var drawingcanvas = document.getelementbyid('mydrawing');     // check element in dom , browser supports canvas     if(drawingcanvas && drawingcanvas.getcontext) {         // initaliase 2-dimensional drawing context         x = drawingcanvas.getcontext('2d');         width = x.canvas.width;         height = x.canvas.height;          // grey box grid transparency testing         x.fillstyle = '#666666';         x.fillrect(0,0,width,height);         x.fillstyle = '#aaaaaa';         var i,j;         (i=0; i<100; i++){             (j=0; j<100; j++){                 if ((i+j)%2==0){                     x.fillrect(20*i,20*j,20,20);                 }             }         }          fg = new image();         fg.src = 'http://uncc.ath.cx/layercake/images/16/3.png';          // create offscreen buffer,          buffer = document.createelement('canvas');         buffer.width = fg.width;         buffer.height = fg.height;         bx = buffer.getcontext('2d');          // fill offscreen buffer tint color         bx.fillstyle = '#ff0000'         bx.fillrect(0,0,buffer.width,buffer.height);          // destination atop makes result alpha channel identical fg, pixels retaining original color *as far can tell*         bx.globalcompositeoperation = "destination-atop";         bx.drawimage(fg,0,0);           // tint image, draw first         x.drawimage(fg,0,0);          //then set global alpha amound want tint it, , draw buffer directly on top of it.         x.globalalpha = 0.5;         x.drawimage(buffer,0,0);     } }         </script>     </head>     </body>         <canvas id="mydrawing" width="770" height="400">             <p>your browser doesn't support canvas.</p>         </canvas>     </body> </html> 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -