|
2 | 2 | <view> |
3 | 3 | <page-head :title="title"></page-head> |
4 | 4 | <view class="uni-common-mt"> |
5 | | - <canvas class="canvas-element" canvas-id="canvas"></canvas> |
| 5 | + <canvas class="canvas-element" canvas-id="canvas" id="canvas"></canvas> |
6 | 6 | <scroll-view class="canvas-buttons" scroll-y="true"> |
7 | 7 | <block v-for="(name, index) in names" :key="index"> |
8 | 8 | <button class="canvas-button" @tap="handleCanvasButton(name)">{{name}}</button> |
|
25 | 25 | ] |
26 | 26 | } |
27 | 27 | }, |
28 | | - onReady: function () { |
| 28 | + onReady: function() { |
29 | 29 | context = uni.createCanvasContext('canvas') |
30 | 30 | }, |
31 | 31 | methods: { |
32 | | - toTempFilePath: function () { |
| 32 | + toTempFilePath: function() { |
33 | 33 | uni.canvasToTempFilePath({ |
34 | 34 | canvasId: 'canvas', |
35 | | - success: function (res) { |
| 35 | + success: function(res) { |
36 | 36 | console.log(res.tempFilePath) |
37 | 37 | }, |
38 | | - fail: function (err) { |
| 38 | + fail: function(err) { |
39 | 39 | console.error(JSON.stringify(err)) |
40 | 40 | } |
41 | 41 | }) |
42 | 42 | }, |
43 | | - handleCanvasButton: function (name) { |
44 | | - this[name]() |
| 43 | + handleCanvasButton: function(name) { |
| 44 | + this[name] && this[name](); |
45 | 45 | }, |
46 | | - rotate: function () { |
| 46 | + rotate: function() { |
47 | 47 | context.beginPath() |
48 | 48 | context.rotate(10 * Math.PI / 180) |
49 | 49 | context.rect(225, 75, 20, 10) |
50 | 50 | context.fill() |
51 | 51 | context.draw() |
52 | 52 | }, |
53 | | - scale: function () { |
| 53 | + scale: function() { |
54 | 54 | context.beginPath() |
55 | 55 | context.rect(25, 25, 50, 50) |
56 | 56 | context.stroke() |
|
62 | 62 | context.stroke() |
63 | 63 | context.draw() |
64 | 64 | }, |
65 | | - reset: function () { |
| 65 | + reset: function() { |
66 | 66 | context.beginPath() |
67 | 67 |
|
68 | 68 | context.setFillStyle('#000000') |
|
77 | 77 | context.setMiterLimit(10) |
78 | 78 | context.draw() |
79 | 79 | }, |
80 | | - translate: function () { |
| 80 | + translate: function() { |
81 | 81 | context.beginPath() |
82 | 82 | context.rect(10, 10, 100, 50) |
83 | 83 | context.fill() |
|
88 | 88 | context.fill() |
89 | 89 | context.draw() |
90 | 90 | }, |
91 | | - save: function () { |
| 91 | + save: function() { |
92 | 92 | context.beginPath() |
93 | 93 | context.setStrokeStyle('#00ff00') |
94 | 94 | context.save() |
|
103 | 103 | context.stroke() |
104 | 104 | context.draw() |
105 | 105 | }, |
106 | | - restore: function () { |
107 | | - [3, 2, 1].forEach(function (item) { |
| 106 | + restore: function() { |
| 107 | + [3, 2, 1].forEach(function(item) { |
108 | 108 | context.beginPath() |
109 | 109 | context.save() |
110 | 110 | context.scale(item, item) |
|
114 | 114 | }); |
115 | 115 | context.draw() |
116 | 116 | }, |
117 | | - drawImage: function () { |
| 117 | + drawImage: function() { |
118 | 118 | context.drawImage('../../../static/uni@2x.png', 0, 0) |
119 | 119 | context.draw() |
120 | 120 | }, |
121 | | - fillText: function () { |
| 121 | + fillText: function() { |
122 | 122 | context.setStrokeStyle('#ff0000') |
123 | 123 |
|
124 | 124 | context.beginPath() |
|
141 | 141 | context.stroke() |
142 | 142 | context.draw() |
143 | 143 | }, |
144 | | - fill: function () { |
| 144 | + fill: function() { |
145 | 145 | context.beginPath() |
146 | 146 | context.rect(20, 20, 150, 100) |
147 | 147 | context.setStrokeStyle('#00ff00') |
148 | 148 | context.fill() |
149 | 149 | context.draw() |
150 | 150 | }, |
151 | | - stroke: function () { |
| 151 | + stroke: function() { |
152 | 152 | context.beginPath() |
153 | 153 | context.moveTo(20, 20) |
154 | 154 | context.lineTo(20, 100) |
|
157 | 157 | context.stroke() |
158 | 158 | context.draw() |
159 | 159 | }, |
160 | | - clearRect: function () { |
| 160 | + clearRect: function() { |
161 | 161 | context.setFillStyle('#ff0000') |
162 | 162 | context.beginPath() |
163 | 163 | context.rect(0, 0, 300, 150) |
164 | 164 | context.fill() |
165 | 165 | context.clearRect(20, 20, 100, 50) |
166 | 166 | context.draw() |
167 | 167 | }, |
168 | | - beginPath: function () { |
| 168 | + beginPath: function() { |
169 | 169 | context.beginPath() |
170 | 170 | context.setLineWidth(5) |
171 | 171 | context.setStrokeStyle('#ff0000') |
|
179 | 179 | context.stroke() |
180 | 180 | context.draw() |
181 | 181 | }, |
182 | | - closePath: function () { |
| 182 | + closePath: function() { |
183 | 183 | context.beginPath() |
184 | 184 | context.setLineWidth(1) |
185 | 185 | context.moveTo(20, 20) |
|
189 | 189 | context.stroke() |
190 | 190 | context.draw() |
191 | 191 | }, |
192 | | - moveTo: function () { |
| 192 | + moveTo: function() { |
193 | 193 | context.beginPath() |
194 | 194 | context.moveTo(0, 0) |
195 | 195 | context.lineTo(300, 150) |
196 | 196 | context.stroke() |
197 | 197 | context.draw() |
198 | 198 | }, |
199 | | - lineTo: function () { |
| 199 | + lineTo: function() { |
200 | 200 | context.beginPath() |
201 | 201 | context.moveTo(20, 20) |
202 | 202 | context.lineTo(20, 100) |
203 | 203 | context.lineTo(70, 100) |
204 | 204 | context.stroke() |
205 | 205 | context.draw() |
206 | 206 | }, |
207 | | - rect: function () { |
| 207 | + rect: function() { |
208 | 208 | context.beginPath() |
209 | 209 | context.rect(20, 20, 150, 100) |
210 | 210 | context.stroke() |
211 | 211 | context.draw() |
212 | 212 | }, |
213 | | - arc: function () { |
| 213 | + arc: function() { |
214 | 214 | context.beginPath() |
215 | 215 | context.setLineWidth(2) |
216 | 216 | context.arc(75, 75, 50, 0, Math.PI * 2, true) |
|
223 | 223 | context.stroke() |
224 | 224 | context.draw() |
225 | 225 | }, |
226 | | - quadraticCurveTo: function () { |
| 226 | + quadraticCurveTo: function() { |
227 | 227 | context.beginPath() |
228 | 228 | context.moveTo(20, 20) |
229 | 229 | context.quadraticCurveTo(20, 100, 200, 20) |
230 | 230 | context.stroke() |
231 | 231 | context.draw() |
232 | 232 | }, |
233 | | - bezierCurveTo: function () { |
| 233 | + bezierCurveTo: function() { |
234 | 234 | context.beginPath() |
235 | 235 | context.moveTo(20, 20) |
236 | 236 | context.bezierCurveTo(20, 100, 200, 100, 200, 20) |
237 | 237 | context.stroke() |
238 | 238 | context.draw() |
239 | 239 | }, |
240 | | - setFillStyle: function () { |
241 | | - ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach(function (item, index) { |
| 240 | + setFillStyle: function() { |
| 241 | + ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach(function(item, index) { |
242 | 242 | context.setFillStyle(item) |
243 | 243 | context.beginPath() |
244 | 244 | context.rect(0 + 75 * index, 0, 50, 50) |
245 | 245 | context.fill() |
246 | 246 | }) |
247 | 247 | context.draw() |
248 | 248 | }, |
249 | | - setStrokeStyle: function () { |
250 | | - ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach(function (item, index) { |
| 249 | + setStrokeStyle: function() { |
| 250 | + ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach(function(item, index) { |
251 | 251 | context.setStrokeStyle(item) |
252 | 252 | context.beginPath() |
253 | 253 | context.rect(0 + 75 * index, 0, 50, 50) |
254 | 254 | context.stroke() |
255 | 255 | }) |
256 | 256 | context.draw() |
257 | 257 | }, |
258 | | - setGlobalAlpha: function () { |
| 258 | + setGlobalAlpha: function() { |
259 | 259 | context.setFillStyle('#000000'); |
260 | | - [1, 0.5, 0.1].forEach(function (item, index) { |
| 260 | + [1, 0.5, 0.1].forEach(function(item, index) { |
261 | 261 | context.setGlobalAlpha(item) |
262 | 262 | context.beginPath() |
263 | 263 | context.rect(0 + 75 * index, 0, 50, 50) |
|
266 | 266 | context.draw() |
267 | 267 | context.setGlobalAlpha(1) |
268 | 268 | }, |
269 | | - setShadow: function () { |
| 269 | + setShadow: function() { |
270 | 270 | context.beginPath() |
271 | 271 | context.setShadow(10, 10, 10, 'rgba(0, 0, 0, 199)') |
272 | 272 | context.rect(10, 10, 100, 100) |
273 | 273 | context.fill() |
274 | 274 | context.draw() |
275 | 275 | }, |
276 | | - setFontSize: function () { |
277 | | - [10, 20, 30, 40].forEach(function (item, index) { |
| 276 | + setFontSize: function() { |
| 277 | + [10, 20, 30, 40].forEach(function(item, index) { |
278 | 278 | context.setFontSize(item) |
279 | 279 | context.fillText('Hello, world', 20, 20 + 40 * index) |
280 | 280 | }) |
281 | 281 | context.draw() |
282 | 282 | }, |
283 | | - setLineCap: function () { |
| 283 | + setLineCap: function() { |
284 | 284 | context.setLineWidth(10); |
285 | | - ['butt', 'round', 'square'].forEach(function (item, index) { |
| 285 | + ['butt', 'round', 'square'].forEach(function(item, index) { |
286 | 286 | context.beginPath() |
287 | 287 | context.setLineCap(item) |
288 | 288 | context.moveTo(20, 20 + 20 * index) |
|
291 | 291 | }) |
292 | 292 | context.draw() |
293 | 293 | }, |
294 | | - setLineJoin: function () { |
| 294 | + setLineJoin: function() { |
295 | 295 | context.setLineWidth(10); |
296 | | - ['bevel', 'round', 'miter'].forEach(function (item, index) { |
| 296 | + ['bevel', 'round', 'miter'].forEach(function(item, index) { |
297 | 297 | context.beginPath() |
298 | 298 | context.setLineJoin(item) |
299 | 299 | context.moveTo(20 + 80 * index, 20) |
|
303 | 303 | }) |
304 | 304 | context.draw() |
305 | 305 | }, |
306 | | - setLineWidth: function () { |
307 | | - [2, 4, 6, 8, 10].forEach(function (item, index) { |
| 306 | + setLineWidth: function() { |
| 307 | + [2, 4, 6, 8, 10].forEach(function(item, index) { |
308 | 308 | context.beginPath() |
309 | 309 | context.setLineWidth(item) |
310 | 310 | context.moveTo(20, 20 + 20 * index) |
|
313 | 313 | }) |
314 | 314 | context.draw() |
315 | 315 | }, |
316 | | - setMiterLimit: function () { |
| 316 | + setMiterLimit: function() { |
317 | 317 | context.setLineWidth(4); |
318 | | - [2, 4, 6, 8, 10].forEach(function (item, index) { |
| 318 | + [2, 4, 6, 8, 10].forEach(function(item, index) { |
319 | 319 | context.beginPath() |
320 | 320 | context.setMiterLimit(item) |
321 | 321 | context.moveTo(20 + 80 * index, 20) |
|
354 | 354 | width: 300upx; |
355 | 355 | margin: 15upx 12upx; |
356 | 356 | } |
357 | | -</style> |
| 357 | +</style> |
0 commit comments