Canvas Compositing

Canvas Compositing
Nikhil
Published on 2021-07-05 10:35:57

Draw behind existing shapes with "destinationover"


context.globalCompositeOperation = "destination-over"

"destination-over" compositing places new drawing under existing drawings.

context.drawImage(rainy,0,0);
context.globalCompositeOperation='destination-over'; // sunny UNDER rainy
context.drawImage(sunny,0,0);

 

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 erases
context.drawImage(bitemark,100,40);

 

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 default
context.drawImage(background,0,0);
context.drawImage(parachuter,0,0);

 

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 oval
context.drawImage(oval,0,0);

 

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 oval
context.drawImage(picture,0,0);

 

Inner shadows with "source-atop"


context.globalCompositeOperation = 'source-atop'

source-atop compositing clips new image inside an existing shape.

// gold filled rect
ctx.fillStyle='gold';
ctx.fillRect(100,100,100,75);
// shadow
ctx.shadowColor='black';
ctx.shadowBlur=10;
// restrict new draw to cover existing pixels
ctx.globalCompositeOperation='source-atop';
// shadowed stroke
// "source-atop" clips off the undesired outer shadow
ctx.strokeRect(100,100,100,75);
ctx.strokeRect(100,100,100,75);

 

Change opacity with "globalAlpha"


You can change the opacity of new drawings by setting the global alpha 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 rectangle
context.fillRect(10,10,50,50);
// change alpha to 50% -- all new drawings will have 50% opacity
context.globalAlpha=0.50;
// draw a semi-transparent rectangle
context.fillRect(100,10,50,50);

 

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 controlled with the alpha setting

// Render the image
ctx.globalCompositeOperation='source-atop';
ctx.drawImage(image, 0, 0);
// set the composite operation
ctx.globalCompositeOperation='difference';
ctx.fillStyle = "white";
ctx.globalAlpha = alpha; // alpha 0 = no effect 1 = full effect
ctx.fillRect(0, 0, image.width, image.height);

 

Black & White with "color"


Remove color from an image via

ctx.globalCompositeOperation = 'color';

The amount of the effect can be controlled with the alpha setting

// Render the image
ctx.globalCompositeOperation = 'source-atop';
ctx.drawImage(image, 0, 0);
// set the composite operation
ctx.globalCompositeOperation = 'color';
ctx.fillStyle = "white";
ctx.globalAlpha = alpha; // alpha 0 = no effect 1 = full effect
ctx.fillRect(0, 0, image.width, image.height);

 

Increase the color contrast with "saturation"


Increase the saturation level of an image with

ctx.globalCompositeOperation = 'saturation';

The amount of the effect can be controlled with the alpha setting or the amount of saturation in the fill overlay

// Render the image
ctx.globalCompositeOperation='source-atop';
ctx.drawImage(image, 0, 0);
// set the composite operation
ctx.globalCompositeOperation ='saturation';
ctx.fillStyle = "red";
ctx.globalAlpha = alpha; // alpha 0 = no effect 1 = full effect
ctx.fillRect(0, 0, image.width, image.height);

 

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 controlled with the alpha setting or the amount of saturation in the fill overlay

// Render the image
ctx.globalCompositeOperation='source-atop';
ctx.fillStyle = "#F80"; // the color of the sepia FX
ctx.fillRect(0, 0, image.width, image.height);
// set the composite operation
ctx.globalCompositeOperation ='luminosity';
ctx.globalAlpha = alpha; // alpha 0 = no effect 1 = full effect
ctx.drawImage(image, 0, 0);

ATutorialHub Related Guide

Comments (8)

Leave a Comment

Your email address will not be published. Required fields are marked*

User Comments

html tutorial comments

panduranga gupta

2021-07-05 07:03:13

good website for learning and help me a lot

html tutorial comments

raju

2021-09-25 14:58:47

The awsome website i am looking like for a long time, good work atutorialhub team keep doing

html tutorial comments

Shivani

2021-09-01 15:03:56

Learning a lot from the courses present on atutorialhub. The courses are very well explained. Great experience

html tutorial comments

Harshitha

2021-09-10 15:05:45

It is very helpful to students and easy to learn the concepts

html tutorial comments

Sowmya

2021-09-14 15:06:41

Great job Tutorials are easy to understand Please make use of it

html tutorial comments

Zain Khan

2021-09-18 15:07:23

Great content and customized courses.

html tutorial comments

Rudrakshi Bhatt

2021-09-09 15:08:10

Well structured coursed and explained really well!

html tutorial comments

Pavana Somashekar

2021-09-11 15:09:08

Good platform for beginners and learn a lot on this website