Compositing
Draw behind existing shapes with “destination-over”
Section titled “Draw behind existing shapes with “destination-over””context.globalCompositeOperation = "destination-over"“destination-over” compositing places new drawing under existing drawings.
context.drawImage(rainy,0,0);context.globalCompositeOperation='destination-over'; // sunny UNDER rainycontext.drawImage(sunny,0,0);Erase existing shapes with “destination-out”
Section titled “Erase existing shapes with “destination-out””context.globalCompositeOperation = "destination-out"“destination-out” compositing uses new shapes to erase existing drawings.
The new shape is not actually drawn — it is just used as a “cookie-cutter” to erase existing pixels.
context.drawImage(apple,0,0);context.globalCompositeOperation = 'destination-out'; // bitemark erasescontext.drawImage(bitemark,100,40);Default compositing: New shapes are drawn over Existing shapes
Section titled “Default compositing: New shapes are drawn over Existing shapes”context.globalCompositeOperation = "source-over"“source-over” compositing [default], places all new drawings over any existing drawings.
context.globalCompositeOperation='source-over'; // the defaultcontext.drawImage(background,0,0);context.drawImage(parachuter,0,0);Clip images inside shapes with “destination-in”
Section titled “Clip images inside shapes with “destination-in””context.globalCompositeOperation = "destination-in"“destination-in” compositing clips existing drawings inside a new shape.
Note: Any part of the existing drawing that falls outside the new drawing is erased.
context.drawImage(picture,0,0);context.globalCompositeOperation='destination-in'; // picture clipped inside ovalcontext.drawImage(oval,0,0);Clip images inside shapes with “source-in”
Section titled “Clip images inside shapes with “source-in””context.globalCompositeOperation = "source-in";source-in compositing clips new drawings inside an existing shape.
Note: Any part of the new drawing that falls outside the existing drawing is erased.
context.drawImage(oval,0,0);context.globalCompositeOperation='source-in'; // picture clipped inside ovalcontext.drawImage(picture,0,0);Inner shadows with “source-atop”
Section titled “Inner shadows with “source-atop””context.globalCompositeOperation = 'source-atop'source-atop compositing clips new image inside an existing shape.
// gold filled rectctx.fillStyle='gold';ctx.fillRect(100,100,100,75);// shadowctx.shadowColor='black';ctx.shadowBlur=10;// restrict new draw to cover existing pixelsctx.globalCompositeOperation='source-atop';// shadowed stroke// "source-atop" clips off the undesired outer shadowctx.strokeRect(100,100,100,75);ctx.strokeRect(100,100,100,75);Invert or Negate image with “difference”
Section titled “Invert or Negate image with “difference””Render a white rectangle over an image with the composite operation
ctx.globalCompositeOperation = 'difference';The amount of the effect can be controled with the alpha setting
// Render the imagectx.globalCompositeOperation='source-atop';ctx.drawImage(image, 0, 0);
// set the composite operationctx.globalCompositeOperation='difference';ctx.fillStyle = "white";ctx.globalAlpha = alpha; // alpha 0 = no effect 1 = full effectctx.fillRect(0, 0, image.width, image.height);Black & White with “color”
Section titled “Black & White with “color””Remove color from an image via
ctx.globalCompositeOperation = 'color';The amount of the effect can be controled with the alpha setting
// Render the imagectx.globalCompositeOperation='source-atop';ctx.drawImage(image, 0, 0);
// set the composite operationctx.globalCompositeOperation='color';ctx.fillStyle = "white";ctx.globalAlpha = alpha; // alpha 0 = no effect 1 = full effectctx.fillRect(0, 0, image.width, image.height);Increase the color contrast with “saturation”
Section titled “Increase the color contrast with “saturation””Increase the saturation level of an image with
ctx.globalCompositeOperation = 'saturation';The amount of the effect can be controled with the alpha setting or the amount of saturation in the fill overlay
// Render the imagectx.globalCompositeOperation='source-atop';ctx.drawImage(image, 0, 0);
// set the composite operationctx.globalCompositeOperation ='saturation';ctx.fillStyle = "red";ctx.globalAlpha = alpha; // alpha 0 = no effect 1 = full effectctx.fillRect(0, 0, image.width, image.height);Sepia FX with “luminosity”
Section titled “Sepia FX with “luminosity””Create a colored sepia FX with
ctx.globalCompositeOperation = 'luminosity';In this case the sepia colour is rendered first the the image.
The amount of the effect can be controled with the alpha setting or the amount of saturation in the fill overlay
// Render the imagectx.globalCompositeOperation='source-atop';ctx.fillStyle = "#F80"; // the color of the sepia FXctx.fillRect(0, 0, image.width, image.height);
// set the composite operationctx.globalCompositeOperation ='luminosity';
ctx.globalAlpha = alpha; // alpha 0 = no effect 1 = full effectctx.drawImage(image, 0, 0);Change opacity with “globalAlpha”
Section titled “Change opacity with “globalAlpha””context.globalAlpha=0.50You can change the opacity of new drawings by setting the globalAlpha to a value between 0.00 (fully transparent) and 1.00 (fully opaque).
The default globalAlpha is 1.00 (fully opaque).
Existing drawings are not affected by globalAlpha.
// draw an opaque rectanglecontext.fillRect(10,10,50,50);
// change alpha to 50% -- all new drawings will have 50% opacitycontext.globalAlpha=0.50;
// draw a semi-transparent rectanglecontext.fillRect(100,10,50,50);









