HTML5 Canvas code example for scaling, turning, and color gradient of images,

Source: Internet
Author: User
Tags translate function

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
  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Var context = canvas. getContext ('2d ');
  4. // Zoom in and out
  5. Context. beginPath ();
  6. Context. strokeStyle = "#000000 ";
  7. Context. strokeRect (10, 10, 150,100 );
  8. // Zoom in 3 times
  9. Context. scale (3, 3 );
  10. Context. beginPath ();
  11. Context. strokeStyle = '# cccccc ';
  12. Context. strokeRect (10, 10, 150,100)
  13. // Zoom out
  14. Context. scale (0.5, 0.5 );
  15. Context. beginPath ();
  16. Context. strokeStyle = '# cccccc ';
  17. Context. strokeRect (10, 10, 150,100)
  18. // Flip
  19. Var img = new Image ();
  20. Img. src = 'images/1.jpg ';
  21. Img. onload = function (){
  22. Context. drawImage (img, 10, 10 );
  23. Context. scale (1,-1 );
  24. Context. drawImage (img, 0,-500 );
  25. }
  26. // Translation
  27. Context. beginPath ();
  28. Context. strokeStyle = '#000000 ';
  29. Context. strokeRect (10,101,150,100 );
  30. // X move 50 y move 100.
  31. Context. translate (50,100 );
  32. Context. beginPath ();
  33. Context. strokeStyle = '# cccccc ';
  34. Context. strokeRect (10, 10, 150,100 );
  35. // Rotate
  36. Context. beginPath ();
  37. Context. strokeStyle = '#000000 ';
  38. Context. strokeRect (200,50, 100,50 );
  39. // 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.
  40. Context. translate (250,75 );
  41. Context. rotate (45 * Math. PI/180 );
  42. Context. translate (-250,-75 );
  43. Context. beginPath ();
  44. Context. strokeStyle = '# cccccc ';
  45. Context. strokeRect (200,50, 100,50 );
  46. // Transform matrix
  47. Context. beginPath ();
  48. Context. strokeStyle = '#000000 ';
  49. Context. strokeRect (10, 10, 150,100 );
  50. Context. transform );
  51. Context. beginPath ();
  52. Context. strokeStyle = '# cccccc ';
  53. Context. strokeRect (10, 10, 150,100 );
  54. }

Gradient, Image Combination effect, color flip

Copy XML/HTML Code to clipboard
  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Var context = canvas. getContext ('2d ');
  4. // Linear gradient Rendering
  5. Var grd = context. createLinearGradient (200,100 );
  6. // 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
  7. Grd. addColorStop (0.1, "#00ff00 ");
  8. Grd. addColorStop (0.8, "# ff0000 ");
  9. Context. fillStyle = grd;
  10. Context. fillRect (0, 0, 200,100 );
  11. // Radial gradient
  12. Var grd = context. createRadialGradient (100,100, 10,100,100, 50 );
  13. Grd. addColorStop (0.1, "#00ff00 ");
  14. Grd. addColorStop (0.8, '# ff0000 ');
  15. Context. fillStyle = grd;
  16. Context. fillRect (0, 0, 200,200 );
  17. // Image Combination Effect
  18. Context. fillStyle = '#00ff00 ';
  19. Context. fillRect (10, 10, 50, 50 );
  20. // New Drawing
  21. // Context. globalCompositeOperation = "source-over ";
  22. // Only draw new content and delete all other content
  23. Context. globalCompositeOperation = 'copy ';
  24. // Where the image overlaps, the color value is determined after the subtraction
  25. Context. globalCompositeOperation = 'darker ';
  26. // The existing content on the canvas is retained only when it overlaps with other images.
  27. Context. globalCompositeOperation = 'destination-atop ';
  28. // Reference http://www.w3school.com.cn/htmldom/prop_canvasrenderingcontext2d_globalcompositeoperation.asp
  29. Context. beginPath ();
  30. Context. fillStyle = '# ff0000 ';
  31. Context. arc (50, 50, 30, 0, 2 * Math. PI );
  32. Context. fill ();
  33. // Color flip
  34. Var img = new Image ();
  35. Img. src = 'images/1.jpg ';
  36. Img. onload = function (){
  37. Context. drawImage (img, 0, 0, 1, 1 );
  38. Var imgData = context. getImageData (0, 0, 1, 1 );
  39. Var pixels = imgData. data;
  40. Console. log (pixels );
  41. For (var I = 0, n = pixels. length; I <n; I + = 4 ){
  42. Pixels [I] = 255-pixels [I];
  43. Pixels [I + 1] = 255-pixels [I + 1];
  44. Pixels [I + 2] = 255-pixels [I + 2];
  45. }
  46. Context. putImageData (imgData, 250, 0 );
  47. }
  48. }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.