We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Path2D

A path object allows you to describe a path through its APIs and render it using the Canvas stroke API. For details, see Path2D.

Drawing Line Segments

Create a Path2D object and use line segments to create different shapes.

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <canvas ref="canvas"></canvas>
  4. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* xxx.css */
  2. .container {
  3. flex-direction: column;
  4. background-color: #F1F3F5;
  5. align-items: center;
  6. justify-content: center;
  7. width: 100%;
  8. height: 100%;
  9. }
  10. canvas {
  11. width: 600px;
  12. height: 600px;
  13. background-color: #fdfdfd;
  14. border: 5px solid red;
  15. }
Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.js
  2. export default {
  3. onShow() {
  4. let ctx = this.$refs.canvas.getContext('2d', {
  5. antialias: true
  6. });
  7. let path = ctx.createPath2D();
  8. // Roof
  9. path.moveTo(10, 300);
  10. path.lineTo(210, 100);
  11. path.lineTo(410, 300);
  12. // House
  13. path.moveTo(10, 300);
  14. path.lineTo(410, 300);
  15. path.lineTo(410, 600);
  16. path.lineTo(10, 600);
  17. path.closePath();
  18. // Window
  19. path.moveTo(50, 450);
  20. path.bezierCurveTo(70, 350, 130, 350, 150, 450);
  21. path.closePath();
  22. // Door
  23. path.moveTo(250, 450);
  24. path.rect(250, 450, 100, 600);
  25. path.closePath();
  26. // Chimney
  27. path.moveTo(365, 250);
  28. path.ellipse(310, 215, 30, 130, 0, Math.PI * 0.04, Math.PI * 1.1, 1);
  29. // Tree
  30. path.moveTo(485, 450);
  31. path.quadraticCurveTo(510, 500, 485, 600);
  32. path.moveTo(550, 450);
  33. path.quadraticCurveTo(525, 500, 550, 600);
  34. path.moveTo(600, 535);
  35. path.arc(520, 450, 85, 0, 6);
  36. ctx.stroke(path);
  37. }
  38. }

Drawing Graphs

First, use createPath2D to create path objects. Only the path1 path is stroked, so only the path1 graphic appears on the canvas. When you tap the text component, the addPath method is triggered, which passes the path1 path object as a parameter into path2. After the path2 object is stroked, both the path1 and path2 graphics appear on the canvas. When you tap the change text, the setTransform attribute value is changed to setTransform(2, 0.1, 0.1, 2, 0, 0), causing the graphic to enlarge and tilt to the left.

Collapse
Word wrap
Dark theme
Copy code
  1. <!-- xxx.hml -->
  2. <div class="container">
  3. <canvas ref="canvas"></canvas>
  4. <div class="content">
  5. <text onclick="addPath">{{ isAdd }}</text>
  6. <text onclick="setTransform">{{ textName }}</text>
  7. </div>
  8. </div>
Collapse
Word wrap
Dark theme
Copy code
  1. /* xxx.css */
  2. .container {
  3. flex-direction: column;
  4. background-color: #F1F3F5;
  5. align-items: center;
  6. justify-content: center;
  7. width: 100%;
  8. height: 100%;
  9. }
  10. canvas {
  11. width: 600px;
  12. height: 600px;
  13. background-color: #fdfdfd;
  14. border: 5px solid red;
  15. }
  16. .content {
  17. width: 80%;
  18. margin-top: 50px;
  19. margin-bottom: 50px;
  20. display: flex;
  21. flex-wrap: wrap;
  22. justify-content: space-around;
  23. }
  24. text {
  25. width: 150px;
  26. height: 80px;
  27. color: white;
  28. border-radius: 20px;
  29. text-align: center;
  30. background-color: #6060e7;
  31. margin-bottom: 30px;
  32. }
Collapse
Word wrap
Dark theme
Copy code
  1. // xxx.js
  2. export default {
  3. data: {
  4. ctx: null,
  5. path1: null,
  6. path2: null,
  7. path3: null,
  8. isAdd: 'addPath2',
  9. isChange: true,
  10. textName: 'change'
  11. },
  12. onShow() {
  13. this.ctx = this.$refs.canvas.getContext('2d', {
  14. antialias: true
  15. });
  16. this.path1 = this.ctx.createPath2D();
  17. // Square
  18. this.path1.moveTo(200, 200);
  19. this.path1.lineTo(400, 200);
  20. this.path1.lineTo(400, 400);
  21. this.path1.lineTo(200, 400);
  22. this.path1.closePath();
  23. this.path2 = this.ctx.createPath2D();
  24. // Arc
  25. this.path2.arc(300, 300, 75, 0, 6.28);
  26. this.ctx.stroke(this.path1);
  27. },
  28. addPath() {
  29. if (this.isAdd == 'addPath2') {
  30. // Clear the specified area on the canvas.
  31. this.ctx.clearRect(0, 0, 600, 600);
  32. this.ctx.beginPath();
  33. // Add a path to the current path.
  34. this.path2.addPath(this.path1);
  35. this.ctx.stroke(this.path2);
  36. this.isAdd = 'clearPath2';
  37. } else {
  38. this.ctx.clearRect(0, 0, 600, 600);
  39. this.ctx.stroke(this.path1);
  40. this.isAdd = 'addPath2';
  41. }
  42. },
  43. setTransform() {
  44. if (this.isChange) {
  45. this.ctx.clearRect(0, 0, 600, 600);
  46. this.path3 = this.ctx.createPath2D();
  47. this.path3.arc(150, 150, 100, 0, 6.28);
  48. // Reset and create a new transformation matrix.
  49. this.path3.setTransform(2, 0.1, 0.1, 2, 0, 0);
  50. this.ctx.stroke(this.path3);
  51. this.isChange = !this.isChange;
  52. this.textName = 'back';
  53. } else {
  54. this.ctx.clearRect(0, 0, 600, 600);
  55. this.path3.setTransform(0.5, -0.1, -0.1, 0.5, 0, 0);
  56. this.ctx.stroke(this.path3);
  57. this.isChange = !this.isChange;
  58. this.textName = 'change';
  59. }
  60. }
  61. }

Search in Guides
Enter a keyword.