Image manipulation in <canvas>
Christmas is a time to meet family and friends. Incidentally it’s also a time to code on all the stuff you never have a chance to do during regular working hours. During my one-week holiday, I’ve spent some nights playing with the <canvas> element, creating a few demos of image manipulation.
globalCompositeOperation
The globalCompositeOperation attribute on the canvas context allow authors to change how images and shapes are drawn on to the canvas. This can be used to great effect. The most famous example of it’s use so far is in the Canvas Reflection Demo where the destination-out value is used to make an image transparent.
Extracting R, G and B.
The darker composite operation displays the sum of the source image and destination images, with color values approaching 0 as a limit. This is done on a per-component basis, comparing the R, G and B values separately. This effectively allows us to remove individual color components from the canvas, using the correct fill style.
To remove the green and blue components of the canvas, we paint a rectangle over the canvas, using darker and fillStyle = "rgba(255,0,0,1)";
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
img = new Image();
img.src = "sabrina.jpg";
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img,0,0,img.width,img.height);
ctx.globalCompositeOperation = "darker";
ctx.fillStyle = "rgba(255,0,0,1)";
ctx.fillRect(0,0,ca.width,ca.height);
I have created three examples, each fading the test image to red, green and blue, respectively:
Note that Firefox does not support the darker attribute value, and so these examples only work in Opera 9. If you don’t have Opera 9, download and install the Technical Preview to view these demos.
Forcing something to transparency
The destination-out attribute value will display the destination image wherever the destination image is opaque and the source image is transparent, and will display transparency elsewhere. This means that when we overlay something opaque over an image, the previous part of the image becomes transparent. I have created an example that fades an image to transparent.
Searchlights
Finally, in these examples, I have created a searchlight that follows the mouse. These two examples use a combination of techniques discussed earlier. In addition the soft searchlight uses a gradient to create a soft light effect:
Speed: Avoid clearRect
Something you will notice if you study the examples, is that whenever a full repaint of the canvas is required, the clearRect() method is usually not called, but the composite operation copy is used instead. This is being done to speed up the operation, since copy will replace the current content of the canvas with whatever it’s painting. This is like calling clearRect() implicitly, but you don’t have to take any transformations and rotations into consideration, and so it will become much faster.
Comments
Comment from Daniel on 2005-12-29 17:26
Could you make an example on how to create a black and white image? Not desaturate, but kinda like the channel mixer.
Comment from Arve on 2005-12-29 18:14
Daniel: Greyscaling an image impossible using canvas, since it involves reading the R, G and B values separately, and then averaging them. For the same reason, creating real sepia toned images is also impossible.
Comment from ROBO Design on 2005-12-30 17:15
Now that’s really nice. Canvas will really provide the base for some “mind-bending” web applications :).
Comment from Sean on 2006-01-03 07:23
That chick is hot.
Comment from Arioch on 2006-01-11 07:15
Oooh, i wish we will also see this in IE7 or at least IE7.sf.net :)
And what about XML Binding Language ? this is quite cool thing fro Web Apps, and afair it was requested long ago :)
Comment from Arve on 2006-01-11 18:22
Arioch, Canvas has been implemented using VML
As for XBL — I hold no particular opinion on it.
Comment from treesie727 on 2006-04-09 20:54
I’ve been trying to view the “hard searchlight” and “soft searchlight” files above. The only thing I get is a blank page that says “mouseover the image” but there is no image. Am I doing something wrong or do I have to have a certain application to view these files?
Comment from Arve on 2006-04-09 21:14
treesie727: You need to have Opera 9, downloadable from here
This discussion has been closed. No further comments may be added.