Please add the author's WeChat official account. If there are new articles in the future, they will be pushed to you directly on the WeChat official account. Thank you very much.
0. Preface
1. Use path
-
Call the beginPath() method of the CanvasRenderingContext2D object to start defining the path. -
Call various methods of CanvasRenderingContext2D to add sub paths. -
Call the closePath method of CanvasRenderingContext2D to close the path. -
Call the fill () or stroke () method of CanvasRenderingContext2D to fill the path or draw the path border.
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
1.1 Draw circle
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <h2>Draw Circle</h2> <canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;"> The current browser does not support canvas </canvas> </div> </body> <script type="text/javascript"> //Get the DOM object corresponding to the canvas element var canvas_1 = document.getElementById("canvas_1"); //Get the canvas RenderingContent2D object drawn on canvas var ctx = canvas_1.getContext("2d"); //Let's draw more circles and use the for loop for(var i = 0; i < 10 ; i++){ //Start defining path ctx.beginPath(); //Add an arc ctx.arc(i * 25,i * 25,(i + 1) * 8, 0, Math. PI * 2, true); //Close Path ctx.closePath(); //Set Fill Color ctx.fillStyle = 'rgba(255,0,255,'+ (10 - i) * 0.1 + ')'; //Fill current path ctx.fill(); } </script> </html>
|
|
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
1.2 Draw rounded rectangle
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <h2>Draw Circle</h2> <canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;"> The current browser does not support canvas </canvas> </div> </body> <script type="text/javascript"> /* This method is responsible for drawing a rounded rectangle X1, y2: coordinates of the upper left corner of the rounded rectangle Width, height: control the width and height of the rounded rectangle Radius: control the radius of the four fillets of the rounded rectangle */ function creatRoundRect(ctx, x1, y1, width, height, radius){ //Move to the start point in the upper left corner ctx.moveTo(x1 + radius, y1); //Add a line segment connecting the start point to the upper right corner ctx.lineTo(x1 + width - radius, y1); //Add an arc in the upper right corner ctx.arcTo(x1 + width, y1, x1 + width, y1 + radius, radius); //Add a line segment connected to the lower right corner ctx.lineTo(x1 + width, y1 + height - radius); //Add an arc in the lower right corner ctx.arcTo(x1 + width, y1 + height, x1 + width - radius, y1 + height, radius); //Add a line segment from the lower right corner to the lower left corner ctx.lineTo(x1 + radius, y1 + height); //Add lower left arc ctx.arcTo(x1, y1 + height, x1, y1 + height - radius,radius); //Add a line segment from the lower left corner to the upper left corner ctx.lineTo(x1, y1 + radius); //Add an arc ctx.arcTo(x1, y1, x1 + radius, y1, radius); ctx.closePath(); } //Get the DOM object corresponding to the canvas element var canvas_1 = document.getElementById("canvas_1"); //Get the canvas RenderingContent2D object drawn on canvas var ctx = canvas_1.getContext("2d"); ctx.lineWidth = 3; creatRoundRect(ctx, 30, 30, 200, 100, 20); ctx.stroke(); </script> </html>
1.3 Drawing a polygon
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <h2>Draw a polygon</h2> <canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;"> The current browser does not support canvas </canvas> </div> </body> <script type="text/javascript"> /* This method is used to draw a polygon n: This parameter is usually set to an odd number to control the drawing of N-point stars Dx, dy: control the position of star N Size: control the size of the star with N angles */ function creatStar(context, n, dx, dy, size){ //Start creating path context.beginPath(); var dig = Math. PI / n * 4; context.moveTo(dx, y + dy); for(var i = 0; i <= n; i++){ var x = Math.sin(i * dig); var y = Math.cos(i * dig); //Draw a line from the current point to the specified point context.lineTo(x * size + dx, y * size + dy); } //Close Path context.closePath(); } //Get the DOM object corresponding to the canvas element var canvas_1 = document.getElementById("canvas_1"); //Get the canvas RenderingContent2D object drawn on canvas var ctx = canvas_1.getContext("2d"); //Draw Triangle Star creatStar(ctx, 3, 60, 60, 50); ctx.fillStyle = '#f00'; ctx.fill(); //Draw a five pointed star creatStar(ctx, 5, 160, 60, 50); ctx.fillStyle = '#0f0'; ctx.fill(); //Draw a seven pointed star creatStar(ctx, 7, 260, 60, 50); ctx.fillStyle = '#00f'; ctx.fill(); //Draw Nine pointed Star creatStar(ctx, 9, 360, 60, 50); ctx.fillStyle = '#f0f'; ctx.fill(); </script> </html>
1.4 Drawing curves
|
|
---|---|
|
|
|
|
-
Bottom left Start point -
pinkish First control point (anchor) -
Navy Second control point (anchor) -
Upper right End point
-
Bottom left Start point -
Navy control point -
Upper right End point
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <h2>Draw flowers</h2> <canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;"> The current browser does not support canvas </canvas> </div> </body> <script type="text/javascript"> /* This method is responsible for drawing flowers n: This parameter controls the number of petals of the flower Dx, dy: control the position of flowers Size: control the size of flowers Length: control the length of petals */ function creatFlower(context, n, dx, dy, size, length){ //Start creating path context.beginPath(); context.moveTo(dx, dy + size); var dig = 2 * Math. PI / n; for(var i = 1; i < n + 1; i++){ //Calculate the coordinates of control points var ctrlX = Math.sin((i - 0.5) * dig) * length + dx; var ctrlY = Math.cos((i - 0.5) * dig) * length + dy; //Calculate the coordinates of the end point var x = Math.sin(i * dig) * size + dx; var y = Math.cos(i * dig) * size + dy; //Draw conic context.quadraticCurveTo(ctrlX, ctrlY, x, y); } context.closePath(); } //Get the DOM object corresponding to the canvas element var canvas_1 = document.getElementById("canvas_1"); //Get the canvas RenderingContent2D object drawn on canvas var ctx = canvas_1.getContext("2d"); //Draw a five petal flower creatFlower(ctx, 5, 70, 100, 30, 80); ctx.fillStyle = "#f00"; ctx.fill(); //Draw a six petaled flower creatFlower(ctx, 6, 220, 100, 30, 80); ctx.fillStyle = "#ff0"; ctx.fill(); //Draw a seven petal flower creatFlower(ctx, 7, 370, 100, 30, 80); ctx.fillStyle = "#f0f"; ctx.fill(); </script> </html>
1.5 Drawing Bitmaps
var image = new Image(); image.src = "lipeng.png";
-
Our program only creates and loads images, so we don't need to pass in the width and height when calling image, so the created image will maintain the same width and height as the image specified by the src attribute. -
Src loads images asynchronously. If the image data is too large or comes directly from the network, when our network speed is slow, we will find that our image drawing will be very slow, which will affect users' use. We can use onload to ensure that we can draw after the image is loaded.
var image = new Image(); image.src = "lipeng.png"; image.onload = function(){ //Draw Picture }
|
|
---|---|
|
|
|
|
|
|
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <div> <h2>Draw Bitmap</h2> <canvas id="canvas_1" width="1000" height="500" style="box-shadow: 0px 0px 20px black;"> The current browser does not support canvas </canvas> </div> </body> <script type="text/javascript"> //Get the DOM object corresponding to the canvas element var canvas_1 = document.getElementById("canvas_1"); //Get the canvas RenderingContent2D object drawn on canvas var ctx = canvas_1.getContext("2d"); //Create an image object var image = new Image(); //Create an image object to load pictures image.src = "lipeng.png"; //This function is triggered when the picture is loaded image.onload = function(){ //Draw the picture at its original size ctx.drawImage(image, 20, 10); //Zoom when drawing pictures ctx.drawImage(image, 600, 10, 76, 110); //Dig out a piece from the original picture, enlarge it three times and draw it on canvas var sd = 50; var sh = 50; /* Parameter Description Image image, integer sx, integer sy, integer sw, integer sh, float dx, float dy,float dw, float dh The two parameters, sx and sy, control where to start digging from the original image, Two parameters, sw and sh, control the width and height of the digging ball from the original image; Two parameters, dx and dy, control where the excavated image is drawn on the canvas Dw and dh control the scaling of the drawn picture. The drawn width is dw and the height is dh */ ctx.drawImage(image, 230, 220,sd, sh, 765, 10, sd * 3, sh * 3); } </script> </html>