ui-js-components-path2d.md 5.2 KB
Newer Older
H
HelloCrease 已提交
1 2 3
# Path2D


E
ester.zhou 已提交
4
**\<Path2D>** allows you to describe a path through an existing path. This path can be drawn through the **stroke** API of **Canvas**. For details, see [Path2D](../reference/arkui-js/js-components-canvas-path2d.md).
H
HelloCrease 已提交
5 6


E
ester.zhou 已提交
7
## Drawing Line Segments
H
HelloCrease 已提交
8

E
ester.zhou 已提交
9
Create a **\<Path2D>** object and use line segments to create different shapes.
H
HelloCrease 已提交
10

E
ester.zhou 已提交
11
```html
H
HelloCrease 已提交
12 13 14 15 16 17
<!-- xxx.hml --> 
<div class="container">
  <canvas ref="canvas"></canvas>
</div>
```

E
ester.zhou 已提交
18
```css
H
HelloCrease 已提交
19
/* xxx.css */
E
ester.zhou 已提交
20 21 22 23 24 25 26
.container {
    flex-direction: column;
    background-color: #F1F3F5;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
H
HelloCrease 已提交
27
}
E
ester.zhou 已提交
28 29 30 31 32 33

canvas {
    width: 600px;
    height: 600px;
    background-color: #fdfdfd;
    border: 5px solid red;
H
HelloCrease 已提交
34 35 36
}
```

E
ester.zhou 已提交
37
```js
H
HelloCrease 已提交
38 39
// xxx.js
export default {
E
ester.zhou 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    onShow() {
        let ctx = this.$refs.canvas.getContext('2d', {
            antialias: true
        });
        let path = ctx.createPath2D();
        // Roof
        path.moveTo(10, 300);
        path.lineTo(210, 100);
        path.lineTo(410, 300);
        // House
        path.moveTo(10, 300);
        path.lineTo(410, 300);
        path.lineTo(410, 600);
        path.lineTo(10, 600);
        path.closePath();
        // Window
        path.moveTo(50, 450);
        path.bezierCurveTo(70, 350, 130, 350, 150, 450);
        path.closePath();
        // Door
        path.moveTo(250, 450);
61
        path.rect(250, 450, 100, 600);
E
ester.zhou 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
        path.closePath();
        // Chimney
        path.moveTo(365, 250);
        path.ellipse(310, 215, 30, 130, 0, Math.PI * 0.04, Math.PI * 1.1, 1);
        // Tree
        path.moveTo(485, 450);
        path.quadraticCurveTo(510, 500, 485, 600);
        path.moveTo(550, 450);
        path.quadraticCurveTo(525, 500, 550, 600);
        path.moveTo(600, 535);
        path.arc(520, 450, 85, 0, 6);
        ctx.stroke(path);
    }
H
HelloCrease 已提交
75 76 77 78 79 80 81 82 83
}
```


![en-us_image_0000001232002968](figures/en-us_image_0000001232002968.png)


## Drawing Graphs

E
ester.zhou 已提交
84
Use **createPath2D** to create a path object and draw only **path1** so that only **path1** is displayed on the canvas. Click the **\<text>** component to trigger the **addPath** method. The **path2** object is passed to **path1** as a parameter. After the **stroke** operation is performed on the **path1** object, **path1** and **path2** are displayed on the canvas. Click **Change** to change the value of **setTransform** to** setTransform(2, 0.1, 0.1, 2, 0,0)**. The graph is enlarged and tilted to the left.
H
HelloCrease 已提交
85 86


E
ester.zhou 已提交
87
```html
H
HelloCrease 已提交
88 89
<!-- xxx.hml -->
<div class="container">
E
ester.zhou 已提交
90 91 92 93 94
    <canvas ref="canvas"></canvas>
    <div class="content">
        <text onclick="addPath">{{ isAdd }}</text>
        <text onclick="setTransform">{{ textName }}</text>
    </div>
H
HelloCrease 已提交
95 96 97 98
</div>
```


E
ester.zhou 已提交
99
```css
H
HelloCrease 已提交
100
/* xxx.css */
E
ester.zhou 已提交
101 102 103 104 105 106 107
.container {
    flex-direction: column;
    background-color: #F1F3F5;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
H
HelloCrease 已提交
108
}
E
ester.zhou 已提交
109 110 111 112 113 114

canvas {
    width: 600px;
    height: 600px;
    background-color: #fdfdfd;
    border: 5px solid red;
H
HelloCrease 已提交
115
}
E
ester.zhou 已提交
116 117 118 119 120 121 122 123

.content {
    width: 80%;
    margin-top: 50px;
    margin-bottom: 50px;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
H
HelloCrease 已提交
124
}
E
ester.zhou 已提交
125 126 127 128 129 130 131 132 133

text {
    width: 150px;
    height: 80px;
    color: white;
    border-radius: 20px;
    text-align: center;
    background-color: #6060e7;
    margin-bottom: 30px;
H
HelloCrease 已提交
134 135 136 137
}
```


E
ester.zhou 已提交
138
```js
H
HelloCrease 已提交
139 140
// xxx.js
export default {
E
ester.zhou 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    data: {
        ctx: null,
        path1: null,
        path2: null,
        path3: null,
        isAdd: "addPath2",
        isChange: true,
        textName: 'change'
    },
    onShow() {
        this.ctx = this.$refs.canvas.getContext('2d', {
            antialias: true
        });
        this.path1 = this.ctx.createPath2D();
        // Square
        this.path1.moveTo(200, 200);
        this.path1.lineTo(400, 200);
        this.path1.lineTo(400, 400);
        this.path1.lineTo(200, 400);
        this.path1.closePath();
        this.path2 = this.ctx.createPath2D();
        // Arc
        this.path2.arc(300, 300, 75, 0, 6.28);
        this.ctx.stroke(this.path1);
    },
    addPath() {
        if (this.isAdd == "addPath2") {
            // Clear the content in the specified area on the canvas.
            this.ctx.clearRect(0, 0, 600, 600);
            this.ctx.beginPath();
            // Add a path to the current path.
            this.path2.addPath(this.path1);
            this.ctx.stroke(this.path2);
            this.isAdd = "clearPath2";
        } else {
            this.ctx.clearRect(0, 0, 600, 600);
            this.ctx.stroke(this.path1);
            this.isAdd = "addPath2";
        }
    },
    setTransform() {
        if (this.isChange) {
            this.ctx.clearRect(0, 0, 600, 600);
            this.path3 = this.ctx.createPath2D();
            this.path3.arc(150, 150, 100, 0, 6.28);
            // Reset and create a new transformation matrix.
            this.path3.setTransform(2, 0.1, 0.1, 2, 0, 0);
            this.ctx.stroke(this.path3);
            this.isChange = !this.isChange;
            this.textName = "back"
        } else {
            this.ctx.clearRect(0, 0, 600, 600);
            this.path3.setTransform(0.5, -0.1, -0.1, 0.5, 0, 0);
            this.ctx.stroke(this.path3);
            this.isChange = !this.isChange;
            this.textName = "change";
        }
H
HelloCrease 已提交
198 199 200 201 202
    }
}
```

![en-us_image_0000001231683112](figures/en-us_image_0000001231683112.gif)