Now we summarize the knowledge points of canvas as follows for easy reference at any time.
1. Filled rectangle fillRect(x,y,width,height);
2. Draw a rectangular border strokeRect(x,y,width,height);
3. Erase rectangle clearRect(x,y,width,height);
4. Fill style fillStyle="red"; The style can be color, gradient and image.
5. Stroke style strokeStyle="red";
6. The width of the stroke line lineWidth=4;
7. Line end shape lineCap="butt"; butt/round/square, by default it is butt;
8. Line intersection style lineJoin="miter"; miter( sharp corner)/round(rounded corner)/bevel(bevel), default sharp corner;
9. Start drawing the path beginPath();
10. End the path closePath(); After creating the path, if you want to draw a line connected to the starting point of the path, you can call closePath();
11. Draw an arc arc(x,y,radius,startAngle,endAngle,true/false);
12. Draw an arc arcTo(x1,y1,x2,y2,radius) Draw an arc starting from the previous point, ending at x2, y2, and passing through it with the given radius. x1,y1;
13. moveTO(x,y); Move the drawing cursor to (x, y) without drawing a line
14. lineTo(x,y); Draw a straight line starting from the previous point
15. Quadratic Bezier Curve: quadraticCurveTo(cx,cy,x,y); Draw a quadratic curve starting from the previous point and ending at x, y. cx, cy are used as control points .
16. Cubic Bezier Curve: bezierCurveTo(cx1,cy1,cx2,cy2,x,y); Draw a quadratic curve starting from the previous point and ending at x, y, cx1, cy1 and cx2,cy2 as control points.
17. rect(x,y,width,height);Draw a rectangle starting from point x, y. The width and height are specified by width and height respectively. This method draws a rectangular path, not an independent shape.
18. Draw text:
(1) Fill text: fillText("hello",x,y,width); width is the optional maximum pixel width. If the text is larger than the maximum width, the text will shrink to fit the maximum width.
(2) Text stroke: strokeText("hello",x,y,width);width is the optional maximum pixel width.
(3) Text style: font="bold 14px Arial";
(4) Horizontal text alignment: textAlign='start'; // start, end, left, right, center. Default value: start. Align on the vertical axis with the starting point (x, y) of the text as the base point.
(5) Vertical text alignment: textBaseline='alphabetic'; //top, hanging, middle, alphabetic, ideographic, bottom. Default value: alphabetic. Align on the horizontal axis with the starting point (x, y) of the text as the base point.
(6) Text width: var text="hello"; var length=context.measureText(text); the parameter text is the text to be drawn
19. Transformation
(1) rotate(angle): Rotate the image angle around the origin.
You can also use transform(Math.cos(angle*Math.PI/180),Math.sin(angle*Math.PI/180),-Math.sin(angle*Math.PI/180),Math. cos(angle*Math.PI/180),0,0);
(2) scale(x,y): Scale the image. You can also use transform(x,0,0,y,0,0);
(3) translate(x,y): Move the coordinate origin to x,y. After executing this transformation, the coordinates 0,0 will Becomes the point previously represented by x, y. You can also use transform(1,0,0,1,x,y);
(4) transform(
(5) setTransform(
20. Graphic combination
contex.save(); Save the current state to the stack. Note: What is saved is only the settings and transformations of the drawn graphics, not the content of the drawn graphics.
context.restore(); retrieve the previously saved graphics state from the stack Applicable occasions:
(1) Image or graphics deformation
(2) Image cropping
(3) When changing the properties of the graphics context: fillStyle, font, globalAlpha, globalComposite-Operation, lineCap, lineJoin, lineWidth, miterLimit, shadowBlur, shadowColor,
shadowOffsetX, shadowOffsetY, strokeStyle, textAlign, textBaseline