Path Drawing Process in Canvas
Canvas path drawing is a multi-step process, mainly including the following steps:
-
Start path: Call the
beginPath()method to clear the current path and prepare to start a new path drawing. -
Set path start point: Use the
moveTo(x, y)method to set the start coordinate of the path. -
Draw path segments: Use various path drawing methods as needed, such as:
lineTo(x, y): Draw a straight line to the specified pointarc(x, y, radius, startAngle, endAngle, anticlockwise): Draw an arcarcTo(x1, y1, x2, y2, radius): Draw an arc tangentquadraticCurveTo(cpx, cpy, x, y): Draw a quadratic Bezier curvebezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y): Draw a cubic Bezier curve
-
Close path: Call the
closePath()method to connect the end point of the path with the start point to form a closed path. This step is optional and only needed when drawing closed figures. -
Fill or stroke path: Use the
fill()method to fill the interior of the path, or use thestroke()method to draw the path outline.
Path Drawing Example
javascriptconst canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); // Draw a triangle ctx.beginPath(); ctx.moveTo(100, 50); // Start point ctx.lineTo(150, 150); // First side ctx.lineTo(50, 150); // Second side ctx.closePath(); // Close path, connect to start point ctx.fillStyle = 'blue'; ctx.fill(); // Fill path // Draw a heart shape ctx.beginPath(); ctx.moveTo(200, 100); ctx.bezierCurveTo(250, 50, 300, 100, 300, 150); ctx.bezierCurveTo(300, 200, 250, 250, 200, 220); ctx.bezierCurveTo(150, 250, 100, 200, 100, 150); ctx.bezierCurveTo(100, 100, 150, 50, 200, 100); ctx.fillStyle = 'red'; ctx.fill();
Graphics Transformation Methods in Canvas
Canvas provides the following basic graphics transformation methods:
-
Translation:
- Method:
translate(x, y) - Function: Move the coordinate system origin to the specified (x, y) position
- Application: Used to move graphics as a whole and simplify coordinate calculations
- Method:
-
Rotation:
- Method:
rotate(angle) - Function: Rotate clockwise around the current origin by the specified angle (radians)
- Application: Used to draw rotated graphics
- Method:
-
Scaling:
- Method:
scale(x, y) - Function: Scale graphics along the x-axis and y-axis directions
- Application: Used to enlarge or reduce graphics
- Method:
-
Transformation:
-
Method:
transform(a, b, c, d, e, f) -
Function: Use matrix transformation to perform more complex transformations on graphics
-
Parameter explanation:
- a: Horizontal scaling factor
- b: Horizontal tilt factor
- c: Vertical tilt factor
- d: Vertical scaling factor
- e: Horizontal translation amount
- f: Vertical translation amount
-
-
Set transformation matrix:
- Method:
setTransform(a, b, c, d, e, f) - Function: Reset the current transformation matrix and set it to a new matrix
- Difference: Unlike
transform(),setTransform()will clear previous transformations
- Method:
-
Save and restore transformation state:
- Methods:
save()andrestore() - Function: Save the current transformation state to the stack, or restore the previous transformation state from the stack
- Application: Used to manage transformation states in complex drawing
- Methods:
Graphics Transformation Example
javascriptconst canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); // Draw a rectangle ctx.fillStyle = 'red'; ctx.fillRect(50, 50, 100, 100); // Save current state ctx.save(); // Translation, rotation and scaling transformation ctx.translate(300, 100); ctx.rotate(Math.PI / 4); // 45 degrees ctx.scale(1.2, 1.2); // Draw transformed rectangle ctx.fillStyle = 'blue'; ctx.fillRect(-50, -50, 100, 100); // Restore previous state ctx.restore(); // Draw another rectangle (not affected by transformation) ctx.fillStyle = 'green'; ctx.fillRect(50, 200, 100, 100);
Application Scenarios of Transformations
- Complex graphics drawing: Transformations can simplify the drawing of complex graphics, such as symmetrical graphics, repeated patterns, etc.
- Animation effects: By continuously updating transformation parameters (such as rotation angle, position), various animation effects can be achieved.
- Coordinate system management: Through transformations, local coordinate systems can be created to make graphics drawing more intuitive and convenient.
- Interactive effects: Transformations are very useful tools when implementing interactive effects such as dragging and rotating.
Notes
- Transformations are cumulative, and subsequent drawing will be affected by all previous transformations.
- Use
save()andrestore()methods to manage transformation states to avoid transformations affecting drawing that does not need to be transformed. - Transformations will affect all drawing operations in Canvas, including paths, text, images, etc.
- For complex transformations, it is recommended to use matrix transformations or combine multiple basic transformations to achieve.