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
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Var context = canvas. getContext ('2d ');
- // Line width
- Context. lineWidth = 4;
- // Paint brush color
- Context. strokeStyle = 'red ';
- // Fill color
- Context. fillStyle = "red ";
- // Line Cap Type
- Context. lineCap = 'butt'; // round, square
- // Start path
- Context. beginPath ();
- // Start point
- Context. moveTo (10, 10 );
- // End point
- Context. lineTo (150,50 );
- // Draw
- Context. stroke ();
- }
Rectangle
Copy XML/HTML Code to clipboard
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Context. beginPath ();
- Context. strokeRect (10, 10, 70, 40 );
- // Another method of rectangle
- Context. rect (10, 10.70, 40 );
- Context. stroke ();
-
- // Solid rectangle
- Context. beginPath ();
- Context. fillRect (10, 10, 70, 40 );
- // Another method is solid rectangle.
- Context. beginPath ();
- Context. rect (10, 10, 70, 40 );
- Context. fill ();
- }
Circle
Copy XML/HTML Code to clipboard
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Context. beginPath ();
- // Center coordinate x, Center Coordinate Y, arc radius, start angle, end angle, and counter-clockwise
- // 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.
- Context. arc (100,100, 130, 180 * Math. PI/, true );
- Context. stroke ();
- Context. fill ();
- }
Rounded corner
Copy XML/HTML Code to clipboard
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Context. beginPath ();
- Context. moveTo (20, 20 );
- Context. lineTo (70, 20 );
- // Draw a radian p1.x p1.y p2.x and a p2.y Arc Radius for a path,
- Context. arcTo (, 30, 70, 50 );
- Context. lineTo (120,120 );
- Context. stroke ();
-
- // Erase the canvas
- Context. beginPath ();
- Context. fillRect (10, 10, 200,100 );
-
- // Erase the region
- Context. clearRect (30, 30, 50, 50 );
- }
Secondary besell Curve
Copy XML/HTML Code to clipboard
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Context. beginPath ();
- Context. moveTo (100,100 );
- Context. quadraticCurveTo (, 20 );
- Context. stroke ();
- }
Cubic besell Curve
Copy XML/HTML Code to clipboard
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Context. moveTo (68,130 );
- Var cX1 = 20;
- Var cY1 = 10;
- Var cX2 = 268;
- Var cY2 = 10;
- Var endpoints = 268;
- Var endY = 170;
- Context. bezierCurveTo (cX1, cY1, cX2, cY2, endX, endY );
- Context. stroke ();
-
- // Use clip to specify the drawing area. After specifying the drawing area, you can only clean the painting in the drawing area.
- // Draw a circle
- Context. arc (100,100, 360, 180 * Math. PI/, true );
- // Restricted Area
- Context. clip ();
- // Start to try to draw other
- Context. beginPath ();
- Context. fillStyle = 'lightblue ';
- // The result rectangle is not displayed.
- Context. fillRect (0, 0, 300,150 );
- }
Advanced use of artboard
Copy XML/HTML Code to clipboard
- Var canvas = document. getElementById ('canvas ');
- If (canvas. getContext ){
- Var context = canvas. getContext ('2d ');
- /*
- * DrawImage (image, dx, dy)
- * DrawImage (image, dx, dy, dw, dh)
- * DrawImage (image, sx, sy, sw, sh, dx, dy, dw, dh );
- * Image Drawing Object
- * Dx dy canvas coordinates
- * Dw, dh indicates the position of the image to be drawn in the canvas
- * Sw, sh indicates the area of the image to be drawn
- * Sx, start position of the sy drawing
- */
- Var image = document. getElementById ('img ');
- Context. drawImage (image, 0, 0 );
- Var img = new Image ();
- Img. src = 'images/1.jpg ';
- Img. onload = function (){
-
- // DrawImage
- // Draw from coordinates 0, 0
- // Context. drawImage (img, 0, 0 );
- // From 0 to 0, draw the entire image to 100,100 long width
- // Context. drawImage (img, 0, 0,100,100 );
- //, Draw from 100,100 to 260,130, and place it in the 100,100 long-width Area
- // Context. drawImage (img, 50, 50,100,100,260,130,100,100 );
-
- // Use getImageData and putImageData to draw images
-
- Context. drawImage (img, 10, 10 );
- // Obtain pixel data from the canvas
- // Start position and end position
- Var imgData = context. getImageData (50, 50, 100,100 );
- // Draw the data to the position coordinate specified by the drawing board
- Context. putImageData (imgData, 10,260 );
- // Draw a part of the acquired pixel data to the canvas.
- Context. putImageData (imgData, 200,260, 50, 100,100 );
-
- // Create a pixel using createImageData
- Var imgData = context. getImageData (50, 50, 200,200 );
- // Create an empty object of the specified size
- Var imgData01 = context. createImageData (imgData );
-
- For (I = 0; I
- // Red Pixel
- ImgData01.data [I + 0] = 255;
- ImgData01.data [I + 1] = 0;
- ImgData01.data [I + 2] = 0;
- ImgData01.data [I ++ 3] = 255;
-
- }
- Context. putImageData (imgData01, 10,260 );
- }
- }