A wave of HTML5Canvas Basic Drawing instance code collection _ html5 tutorial skills-

Source: Internet
Author: User
This article mainly introduces a wave of HTML5Canvas Basic Drawing instance code sets, including the drawing of basic images such as circular rectangular rounded corners. For more information, see Basic Drawing

Copy XML/HTML Code to clipboard

  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Var context = canvas. getContext ('2d ');
  4. // Line width
  5. Context. lineWidth = 4;
  6. // Paint brush color
  7. Context. strokeStyle = 'red ';
  8. // Fill color
  9. Context. fillStyle = "red ";
  10. // Line Cap Type
  11. Context. lineCap = 'butt'; // round, square
  12. // Start path
  13. Context. beginPath ();
  14. // Start point
  15. Context. moveTo (10, 10 );
  16. // End point
  17. Context. lineTo (150,50 );
  18. // Draw
  19. Context. stroke ();
  20. }


Rectangle

Copy XML/HTML Code to clipboard

  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Context. beginPath ();
  4. Context. strokeRect (10, 10, 70, 40 );
  5. // Another method of rectangle
  6. Context. rect (10, 10.70, 40 );
  7. Context. stroke ();
  8. // Solid rectangle
  9. Context. beginPath ();
  10. Context. fillRect (10, 10, 70, 40 );
  11. // Another method is solid rectangle.
  12. Context. beginPath ();
  13. Context. rect (10, 10, 70, 40 );
  14. Context. fill ();
  15. }


Circle

Copy XML/HTML Code to clipboard

  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Context. beginPath ();
  4. // Center coordinate x, Center Coordinate Y, arc radius, start angle, end angle, and counter-clockwise
  5. // The Second and Fifth parameters are radians to be passed in. If you draw 30 degrees, you need to convert them to radians 30 * Math. PI/4th.
  6. Context. arc (100,100, 130, 180 * Math. PI/, true );
  7. Context. stroke ();
  8. Context. fill ();
  9. }


Rounded corner

Copy XML/HTML Code to clipboard

  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Context. beginPath ();
  4. Context. moveTo (20, 20 );
  5. Context. lineTo (70, 20 );
  6. // Draw a radian p1.x p1.y p2.x and a p2.y Arc Radius for a path,
  7. Context. arcTo (, 30, 70, 50 );
  8. Context. lineTo (120,120 );
  9. Context. stroke ();
  10. // Erase the canvas
  11. Context. beginPath ();
  12. Context. fillRect (10, 10, 200,100 );
  13. // Erase the region
  14. Context. clearRect (30, 30, 50, 50 );
  15. }

Secondary besell Curve

Copy XML/HTML Code to clipboard

  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Context. beginPath ();
  4. Context. moveTo (100,100 );
  5. Context. quadraticCurveTo (, 20 );
  6. Context. stroke ();
  7. }


Cubic besell Curve

Copy XML/HTML Code to clipboard

  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Context. moveTo (68,130 );
  4. Var cX1 = 20;
  5. Var cY1 = 10;
  6. Var cX2 = 268;
  7. Var cY2 = 10;
  8. Var endpoints = 268;
  9. Var endY = 170;
  10. Context. bezierCurveTo (cX1, cY1, cX2, cY2, endX, endY );
  11. Context. stroke ();
  12. // Use clip to specify the drawing area. After specifying the drawing area, you can only clean the painting in the drawing area.
  13. // Draw a circle
  14. Context. arc (100,100, 360, 180 * Math. PI/, true );
  15. // Restricted Area
  16. Context. clip ();
  17. // Start to try to draw other
  18. Context. beginPath ();
  19. Context. fillStyle = 'lightblue ';
  20. // The result rectangle is not displayed.
  21. Context. fillRect (0, 0, 300,150 );
  22. }

Advanced use of artboard

Copy XML/HTML Code to clipboard

  1. Var canvas = document. getElementById ('canvas ');
  2. If (canvas. getContext ){
  3. Var context = canvas. getContext ('2d ');
  4. /*
  5. * DrawImage (image, dx, dy)
  6. * DrawImage (image, dx, dy, dw, dh)
  7. * DrawImage (image, sx, sy, sw, sh, dx, dy, dw, dh );
  8. * Image Drawing Object
  9. * Dx dy canvas coordinates
  10. * Dw, dh indicates the position of the image to be drawn in the canvas
  11. * Sw, sh indicates the area of the image to be drawn
  12. * Sx, start position of the sy drawing
  13. */
  14. Var image = document. getElementById ('img ');
  15. Context. drawImage (image, 0, 0 );
  16. Var img = new Image ();
  17. Img. src = 'images/1.jpg ';
  18. Img. onload = function (){
  19. // DrawImage
  20. // Draw from coordinates 0, 0
  21. // Context. drawImage (img, 0, 0 );
  22. // From 0 to 0, draw the entire image to 100,100 long width
  23. // Context. drawImage (img, 0, 0,100,100 );
  24. //, Draw from 100,100 to 260,130, and place it in the 100,100 long-width Area
  25. // Context. drawImage (img, 50, 50,100,100,260,130,100,100 );
  26. // Use getImageData and putImageData to draw images
  27. Context. drawImage (img, 10, 10 );
  28. // Obtain pixel data from the canvas
  29. // Start position and end position
  30. Var imgData = context. getImageData (50, 50, 100,100 );
  31. // Draw the data to the position coordinate specified by the drawing board
  32. Context. putImageData (imgData, 10,260 );
  33. // Draw a part of the acquired pixel data to the canvas.
  34. Context. putImageData (imgData, 200,260, 50, 100,100 );
  35. // Create a pixel using createImageData
  36. Var imgData = context. getImageData (50, 50, 200,200 );
  37. // Create an empty object of the specified size
  38. Var imgData01 = context. createImageData (imgData );
  39. For (I = 0; I
  40. // Red Pixel
  41. ImgData01.data [I + 0] = 255;
  42. ImgData01.data [I + 1] = 0;
  43. ImgData01.data [I + 2] = 0;
  44. ImgData01.data [I ++ 3] = 255;
  45. }
  46. Context. putImageData (imgData01, 10,260 );
  47. }
  48. }

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.