Canvas使用教程——图像合成(Compositing)

来源:https://developer.mozilla.org/en/Canvas_tutorial/Compositing

In all of our previous examples, shapes were always drawn one on top of the other. This is more than adequate for most situations. This, for instance, limits in what order composite shapes are built up. We can however change this behaviour by setting the globalCompositeOperation property.

globalCompositeOperation

We can not only draw new shapes behind existing shapes but we can also use it to mask off certain areas, clear sections from the canvas (not limited to rectangles like the clearRect method does) and more.

globalCompositeOperation = type

type is a string representing any one of twelve compositing operations. Each of the available types is described below.

Note: In all of the examples below the blue square is drawn first and referred to as 'existing canvas content'. The red circle is drawn second and referred to as 'new shape'.

source-over (default)
This is the default setting and draws new shapes on top of the existing canvas content.

 

destination-over
New shapes are drawn behind the existing canvas content.

 

source-in
The new shape is drawn only where both the new shape and the destination canvas overlap. Everything else is made transparent

 

destination-in
The existing canvas content is kept where both the new shape and existing canvas content overlap. Everything else is made transparent.

 

source-out
The new shape is drawn where it doesn't overlap the existing canvas content.

 

destination-out
The existing content is kept where it doesn't overlap the new shape.

 

source-atop
The new shape is only drawn where it overlaps the existing canvas content.

 

destination-atop
The existing canvas is only kept where it overlaps the new shape. The new shape is drawn behind the canvas content.

 

lighter
Where both shapes overlap the color is determined by adding color values.

 

darker
Where both shapes overlap the color is determined by subtracting color values.

 

xor
Shapes are made transparent where both overlap and drawn normal everywhere else.

 

copy
Only draws the new shape and removes everything else.

 

Note: Currently the copy and darker settings don't do anything in the Gecko 1.8 based browsers (Firefox 1.5 betas, etc).

View all the examples

Clipping paths

A clipping path is like a normal canvas shape but it acts as a mask to hide unwanted parts of shapes. This is visualized in the image on the right. The red star shape is our clipping path. Everything that falls outside of this path won't get drawn on the canvas.

If we compare clipping paths to the globalCompositeOperation property we've seen above; settings that achieve more or less the same effect are source-in and source-atop. The most important differences between the two are that clipping paths are never actually drawn to the canvas and the clipping path is never affected by adding new shapes. This makes clipping paths ideal for drawing multiple shapes in a restricted area.

In the chapter about Drawing shapes I only mentioned the stroke and fill methods, but there's a third method we can use with paths, called clip.

clip()

We use the clip method to create a new clipping path. By default the canvas element has a clipping path that's the exact same size as the canvas itself (i.e. no clipping occurs).

A clip example

In this example I'll be using a circular clipping path to restrict the drawing of a set of random stars to a particular region.

In the first few lines of code I draw a black rectangle the size of the canvas as a backdrop and translate the origin to the center. Below this I create the circular clipping path by drawing an arc. By calling the clip method the clipping path is created. Clipping paths are also part of the canvas save state. If we wanted to keep the original clipping path we could have saved the canvas state before creating the new one.

Everything that's drawn after creating the clipping path will only appear inside that path. You can see this clearly in the linear gradient that's drawn next. After this a set of 50 randomly positioned and scaled stars is drawn (I'm using a custom function for this). Again the stars only appear inside the defined clipping path.

View this example

  1. function draw() {   
  2.   var ctx = document.getElementById('canvas').getContext('2d');   
  3.   ctx.fillRect(0,0,150,150);   
  4.   ctx.translate(75,75);   
  5.   
  6.   // Create a circular clipping path   
  7.   ctx.beginPath();   
  8.   ctx.arc(0,0,60,0,Math.PI*2,true);   
  9.   ctx.clip();   
  10.   
  11.   // draw background   
  12.   var lingrad = ctx.createLinearGradient(0,-75,0,75);   
  13.   lingrad.addColorStop(0, '#232256');   
  14.   lingrad.addColorStop(1, '#143778');   
  15.      
  16.   ctx.fillStyle = lingrad;   
  17.   ctx.fillRect(-75,-75,150,150);   
  18.   
  19.   // draw stars   
  20.   for (var j=1;j<50;j++){   
  21.     ctx.save();   
  22.     ctx.fillStyle = '#fff';   
  23.     ctx.translate(75-Math.floor(Math.random()*150),   
  24.                   75-Math.floor(Math.random()*150));   
  25.     drawStar(ctx,Math.floor(Math.random()*4)+2);   
  26.     ctx.restore();   
  27.   }   
  28.      
  29. }   
  30. function drawStar(ctx,r){   
  31.   ctx.save();   
  32.   ctx.beginPath()   
  33.   ctx.moveTo(r,0);   
  34.   for (var i=0;i<9;i++){   
  35.     ctx.rotate(Math.PI/5);   
  36.     if(i%2 == 0) {   
  37.       ctx.lineTo((r/0.525731)*0.200811,0);   
  38.     } else {   
  39.       ctx.lineTo(r,0);   
  40.     }   
  41.   }   
  42.   ctx.closePath();   
  43.   ctx.fill();   
  44.   ctx.restore();   
  45. }  

加支付宝好友偷能量挖...


评论(0)网络
阅读(141)喜欢(0)Canvas/VML/SVG