HTML5 Canvas code example for scaling, turning, and color gradient of images,
Flip, move, pan, zoom in, zoom out
Copy XML/HTML Code to clipboard
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Var context = canvas. getContext ('2d ');
- // Zoom in and out
- Context. beginPath ();
- Context. strokeStyle = "#000000 ";
- Context. strokeRect (10, 10, 150,100 );
- // Zoom in 3 times
- Context. scale (3, 3 );
- Context. beginPath ();
- Context. strokeStyle = '# cccccc ';
- Context. strokeRect (10, 10, 150,100)
- // Zoom out
- Context. scale (0.5, 0.5 );
- Context. beginPath ();
- Context. strokeStyle = '# cccccc ';
- Context. strokeRect (10, 10, 150,100)
- // Flip
- Var img = new Image ();
- Img. src = 'images/1.jpg ';
- Img. onload = function (){
- Context. drawImage (img, 10, 10 );
- Context. scale (1,-1 );
- Context. drawImage (img, 0,-500 );
- }
- // Translation
- Context. beginPath ();
- Context. strokeStyle = '#000000 ';
- Context. strokeRect (10,101,150,100 );
- // X move 50 y move 100.
- Context. translate (50,100 );
- Context. beginPath ();
- Context. strokeStyle = '# cccccc ';
- Context. strokeRect (10, 10, 150,100 );
- // Rotate
- Context. beginPath ();
- Context. strokeStyle = '#000000 ';
- Context. strokeRect (200,50, 100,50 );
- // By default, the rotation is based on the 0, 0 center. You can use the translate function to rotate the center according to your settings.
- Context. translate (250,75 );
- Context. rotate (45 * Math. PI/180 );
- Context. translate (-250,-75 );
- Context. beginPath ();
- Context. strokeStyle = '# cccccc ';
- Context. strokeRect (200,50, 100,50 );
- // Transform matrix
- Context. beginPath ();
- Context. strokeStyle = '#000000 ';
- Context. strokeRect (10, 10, 150,100 );
- Context. transform );
- Context. beginPath ();
- Context. strokeStyle = '# cccccc ';
- Context. strokeRect (10, 10, 150,100 );
- }
Gradient, Image Combination effect, color flip
Copy XML/HTML Code to clipboard
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Var context = canvas. getContext ('2d ');
- // Linear gradient Rendering
- Var grd = context. createLinearGradient (200,100 );
- // Postion must be vertical between 0.1-1.0, indicating the relative position of the color location in the gradient, and color indicates the color
- Grd. addColorStop (0.1, "#00ff00 ");
- Grd. addColorStop (0.8, "# ff0000 ");
- Context. fillStyle = grd;
- Context. fillRect (0, 0, 200,100 );
- // Radial gradient
- Var grd = context. createRadialGradient (100,100, 10,100,100, 50 );
- Grd. addColorStop (0.1, "#00ff00 ");
- Grd. addColorStop (0.8, '# ff0000 ');
- Context. fillStyle = grd;
- Context. fillRect (0, 0, 200,200 );
- // Image Combination Effect
- Context. fillStyle = '#00ff00 ';
- Context. fillRect (10, 10, 50, 50 );
- // New Drawing
- // Context. globalCompositeOperation = "source-over ";
- // Only draw new content and delete all other content
- Context. globalCompositeOperation = 'copy ';
- // Where the image overlaps, the color value is determined after the subtraction
- Context. globalCompositeOperation = 'darker ';
- // The existing content on the canvas is retained only when it overlaps with other images.
- Context. globalCompositeOperation = 'destination-atop ';
- // Reference http://www.w3school.com.cn/htmldom/prop_canvasrenderingcontext2d_globalcompositeoperation.asp
- Context. beginPath ();
- Context. fillStyle = '# ff0000 ';
- Context. arc (50, 50, 30, 0, 2 * Math. PI );
- Context. fill ();
- // Color flip
- Var img = new Image ();
- Img. src = 'images/1.jpg ';
- Img. onload = function (){
- Context. drawImage (img, 0, 0, 1, 1 );
- Var imgData = context. getImageData (0, 0, 1, 1 );
- Var pixels = imgData. data;
- Console. log (pixels );
- For (var I = 0, n = pixels. length; I <n; I + = 4 ){
- Pixels [I] = 255-pixels [I];
- Pixels [I + 1] = 255-pixels [I + 1];
- Pixels [I + 2] = 255-pixels [I + 2];
- }
- Context. putImageData (imgData, 250, 0 );
- }
- }