canvas.uvue 14.6 KB
Newer Older
H
hdx 已提交
1
<template>
H
hdx 已提交
2
  <view class="page">
H
hdx 已提交
3
    <page-head :title="title"></page-head>
H
hdx 已提交
4 5
    <canvas class="canvas-element" canvas-id="canvas" id="canvas"></canvas>
    <scroll-view class="scroll-view" :scroll-y="true">
H
hdx 已提交
6
      <view class="grid-view">
H
hdx 已提交
7
        <view class="grid-item" v-for="(name, index) in names" :key="index">
H
hdx 已提交
8
          <button class="canvas-drawing-button" @click="handleCanvasButton(name)">{{name}}</button>
H
hdx 已提交
9 10
        </view>
      </view>
H
hdx 已提交
11
      <button class="btn-to-image" @click="toTempFilePath" type="primary">toTempFilePath</button>
H
hdx 已提交
12
    </scroll-view>
H
hdx 已提交
13 14
  </view>
</template>
H
hdx 已提交
15

H
hdx 已提交
16 17 18 19 20 21 22 23 24
<script>
  export default {
    data() {
      return {
        title: 'createContext',
        names: ["rotate", "scale", "reset", "translate", "save", "restore", "drawImage", "fillText", "fill",
          "stroke", "clearRect", "beginPath", "closePath", "moveTo", "lineTo", "rect", "arc",
          "quadraticCurveTo", "bezierCurveTo", "setFillStyle", "setStrokeStyle", "setGlobalAlpha",
          "setShadow", "setFontSize", "setLineCap", "setLineJoin", "setLineWidth", "setMiterLimit"
H
hdx 已提交
25 26 27
        ],
        // TODO 缺失 CanvasContext
        canvasContext: null as any | null
H
hdx 已提交
28 29
      }
    },
H
hdx 已提交
30 31 32
    onReady() {
      // @ts-ignore
      this.canvasContext = uni.createCanvasContext('canvas', this)
H
hdx 已提交
33 34
    },
    methods: {
H
hdx 已提交
35 36 37 38 39 40 41 42 43 44 45
      toTempFilePath() {
        // TODO 缺失
        // uni.canvasToTempFilePath({
        //   canvasId: 'canvas',
        //   success: (res) => {
        //     console.log(res.tempFilePath)
        //   },
        //   fail: (err) => {
        //     console.error(JSON.stringify(err))
        //   }
        // })
H
hdx 已提交
46
      },
H
hdx 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
      handleCanvasButton(name : string) {
        switch (name) {
          case "rotate":
            this.rotate();
            break;
          case "scale":
            this.scale();
            break;
          case "reset":
            this.reset();
            break;
          case "translate":
            this.translate();
            break;
          case "save":
            this.save();
            break;
          case "restore":
            this.restore();
            break;
          case "drawImage":
            this.drawImage();
            break;
          case "fillText":
            this.fillText();
            break;
          case "fill":
            this.fill();
            break;
          case "stroke":
            this.stroke();
            break;
          case "clearRect":
            this.clearRect();
            break;
          case "beginPath":
            this.beginPath();
            break;
          case "closePath":
            this.closePath();
            break;
          case "moveTo":
            this.moveTo();
            break;
          case "lineTo":
            this.lineTo();
            break;
          case "rect":
            this.rect();
            break;
          case "arc":
            this.arc();
            break;
          case "quadraticCurveTo":
            this.quadraticCurveTo();
            break;
          case "bezierCurveTo":
            this.bezierCurveTo();
            break;
          case "setFillStyle":
            this.setFillStyle();
            break;
          case "setStrokeStyle":
            this.setStrokeStyle();
            break;
          case "setGlobalAlpha":
            this.setGlobalAlpha();
            break;
          case "setShadow":
            this.setShadow();
            break;
          case "setFontSize":
            this.setFontSize();
            break;
          case "setLineCap":
            this.setLineCap();
            break;
          case "setLineJoin":
            this.setLineJoin();
            break;
          case "setLineWidth":
            this.setLineWidth();
            break;
          case "setMiterLimit":
            this.setMiterLimit();
            break;
          default:
            break;
        }
H
hdx 已提交
136
      },
H
hdx 已提交
137 138 139 140 141 142
      rotate() {
        this.canvasContext!.beginPath()
        this.canvasContext!.rotate(10 * Math.PI / 180)
        this.canvasContext!.rect(225, 75, 20, 10)
        this.canvasContext!.fill()
        this.canvasContext!.draw()
H
hdx 已提交
143
      },
H
hdx 已提交
144 145 146 147
      scale() {
        this.canvasContext!.beginPath()
        this.canvasContext!.rect(25, 25, 50, 50)
        this.canvasContext!.stroke()
H
hdx 已提交
148

H
hdx 已提交
149
        this.canvasContext!.scale(2, 2)
H
hdx 已提交
150

H
hdx 已提交
151 152 153 154
        this.canvasContext!.beginPath()
        this.canvasContext!.rect(25, 25, 50, 50)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
155
      },
H
hdx 已提交
156 157
      reset() {
        this.canvasContext!.beginPath()
H
hdx 已提交
158

H
hdx 已提交
159 160 161 162 163
        this.canvasContext!.setFillStyle('#000000')
        this.canvasContext!.setStrokeStyle('#000000')
        this.canvasContext!.setFontSize(10)
        this.canvasContext!.setGlobalAlpha(1)
        this.canvasContext!.setShadow(0, 0, 0, 'rgba(0, 0, 0, 0)')
H
hdx 已提交
164

H
hdx 已提交
165 166 167 168 169
        this.canvasContext!.setLineCap('butt')
        this.canvasContext!.setLineJoin('miter')
        this.canvasContext!.setLineWidth(1)
        this.canvasContext!.setMiterLimit(10)
        this.canvasContext!.draw()
H
hdx 已提交
170
      },
H
hdx 已提交
171 172 173 174
      translate() {
        this.canvasContext!.beginPath()
        this.canvasContext!.rect(10, 10, 100, 50)
        this.canvasContext!.fill()
H
hdx 已提交
175

H
hdx 已提交
176
        this.canvasContext!.translate(70, 70)
H
hdx 已提交
177

H
hdx 已提交
178 179 180
        this.canvasContext!.beginPath()
        this.canvasContext!.fill()
        this.canvasContext!.draw()
H
hdx 已提交
181
      },
H
hdx 已提交
182 183 184 185
      save() {
        this.canvasContext!.beginPath()
        this.canvasContext!.setStrokeStyle('#00ff00')
        this.canvasContext!.save()
H
hdx 已提交
186

H
hdx 已提交
187 188 189 190 191
        this.canvasContext!.scale(2, 2)
        this.canvasContext!.setStrokeStyle('#ff0000')
        this.canvasContext!.rect(0, 0, 100, 100)
        this.canvasContext!.stroke()
        this.canvasContext!.restore()
H
hdx 已提交
192

H
hdx 已提交
193 194 195
        this.canvasContext!.rect(0, 0, 50, 50)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
196
      },
H
hdx 已提交
197 198 199 200 201 202 203 204
      restore() {
        [3, 2, 1].forEach((item) => {
          this.canvasContext!.beginPath()
          this.canvasContext!.save()
          this.canvasContext!.scale(item, item)
          this.canvasContext!.rect(10, 10, 100, 100)
          this.canvasContext!.stroke()
          this.canvasContext!.restore()
H
hdx 已提交
205
        });
H
hdx 已提交
206
        this.canvasContext!.draw()
H
hdx 已提交
207
      },
H
hdx 已提交
208
      drawImage() {
H
hdx 已提交
209
        // #ifdef APP-PLUS
H
hdx 已提交
210
        this.canvasContext!.drawImage('../../../static/app-plus/uni@2x.png', 0, 0)
H
hdx 已提交
211 212
        // #endif
        // #ifndef APP-PLUS
H
hdx 已提交
213
        this.canvasContext!.drawImage('../../../static/uni.png', 0, 0)
H
hdx 已提交
214
        // #endif
H
hdx 已提交
215
        this.canvasContext!.draw()
H
hdx 已提交
216
      },
H
hdx 已提交
217 218
      fillText() {
        this.canvasContext!.setStrokeStyle('#ff0000')
H
hdx 已提交
219

H
hdx 已提交
220 221 222 223 224 225 226 227 228 229 230
        this.canvasContext!.beginPath()
        this.canvasContext!.moveTo(0, 10)
        this.canvasContext!.lineTo(300, 10)
        this.canvasContext!.stroke()
        // this.canvasContext!.save()
        // this.canvasContext!.scale(1.5, 1.5)
        // this.canvasContext!.translate(20, 20)
        this.canvasContext!.setFontSize(10)
        this.canvasContext!.fillText('Hello World', 0, 30)
        this.canvasContext!.setFontSize(20)
        this.canvasContext!.fillText('Hello World', 100, 30)
H
hdx 已提交
231

H
hdx 已提交
232
        // this.canvasContext!.restore()
H
hdx 已提交
233

H
hdx 已提交
234 235 236 237 238
        this.canvasContext!.beginPath()
        this.canvasContext!.moveTo(0, 30)
        this.canvasContext!.lineTo(300, 30)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
239
      },
H
hdx 已提交
240 241 242 243 244 245
      fill() {
        this.canvasContext!.beginPath()
        this.canvasContext!.rect(20, 20, 150, 100)
        this.canvasContext!.setStrokeStyle('#00ff00')
        this.canvasContext!.fill()
        this.canvasContext!.draw()
H
hdx 已提交
246
      },
H
hdx 已提交
247 248 249 250 251 252 253 254
      stroke() {
        this.canvasContext!.beginPath()
        this.canvasContext!.moveTo(20, 20)
        this.canvasContext!.lineTo(20, 100)
        this.canvasContext!.lineTo(70, 100)
        this.canvasContext!.setStrokeStyle('#00ff00')
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
255
      },
H
hdx 已提交
256 257 258 259 260 261 262
      clearRect() {
        this.canvasContext!.setFillStyle('#ff0000')
        this.canvasContext!.beginPath()
        this.canvasContext!.rect(0, 0, 300, 150)
        this.canvasContext!.fill()
        this.canvasContext!.clearRect(20, 20, 100, 50)
        this.canvasContext!.draw()
H
hdx 已提交
263
      },
H
hdx 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276
      beginPath() {
        this.canvasContext!.beginPath()
        this.canvasContext!.setLineWidth(5)
        this.canvasContext!.setStrokeStyle('#ff0000')
        this.canvasContext!.moveTo(0, 75)
        this.canvasContext!.lineTo(250, 75)
        this.canvasContext!.stroke()
        this.canvasContext!.beginPath()
        this.canvasContext!.setStrokeStyle('#0000ff')
        this.canvasContext!.moveTo(50, 0)
        this.canvasContext!.lineTo(150, 130)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
277
      },
H
hdx 已提交
278 279 280 281 282 283 284 285 286
      closePath() {
        this.canvasContext!.beginPath()
        this.canvasContext!.setLineWidth(1)
        this.canvasContext!.moveTo(20, 20)
        this.canvasContext!.lineTo(20, 100)
        this.canvasContext!.lineTo(70, 100)
        this.canvasContext!.closePath()
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
287
      },
H
hdx 已提交
288 289 290 291 292 293
      moveTo() {
        this.canvasContext!.beginPath()
        this.canvasContext!.moveTo(0, 0)
        this.canvasContext!.lineTo(300, 150)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
294
      },
H
hdx 已提交
295 296 297 298 299 300 301
      lineTo() {
        this.canvasContext!.beginPath()
        this.canvasContext!.moveTo(20, 20)
        this.canvasContext!.lineTo(20, 100)
        this.canvasContext!.lineTo(70, 100)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
302
      },
H
hdx 已提交
303 304 305 306 307
      rect() {
        this.canvasContext!.beginPath()
        this.canvasContext!.rect(20, 20, 150, 100)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
308
      },
H
hdx 已提交
309 310 311 312 313 314 315 316 317 318 319 320
      arc() {
        this.canvasContext!.beginPath()
        this.canvasContext!.setLineWidth(2)
        this.canvasContext!.arc(75, 75, 50, 0, Math.PI * 2, true)
        this.canvasContext!.moveTo(110, 75)
        this.canvasContext!.arc(75, 75, 35, 0, Math.PI, false)
        this.canvasContext!.moveTo(65, 65)
        this.canvasContext!.arc(60, 65, 5, 0, Math.PI * 2, true)
        this.canvasContext!.moveTo(95, 65)
        this.canvasContext!.arc(90, 65, 5, 0, Math.PI * 2, true)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
321
      },
H
hdx 已提交
322 323 324 325 326 327
      quadraticCurveTo() {
        this.canvasContext!.beginPath()
        this.canvasContext!.moveTo(20, 20)
        this.canvasContext!.quadraticCurveTo(20, 100, 200, 20)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
328
      },
H
hdx 已提交
329 330 331 332 333 334
      bezierCurveTo() {
        this.canvasContext!.beginPath()
        this.canvasContext!.moveTo(20, 20)
        this.canvasContext!.bezierCurveTo(20, 100, 200, 100, 200, 20)
        this.canvasContext!.stroke()
        this.canvasContext!.draw()
H
hdx 已提交
335
      },
H
hdx 已提交
336 337 338 339 340 341
      setFillStyle() {
        ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
          this.canvasContext!.setFillStyle(item)
          this.canvasContext!.beginPath()
          this.canvasContext!.rect(0 + 75 * index, 0, 50, 50)
          this.canvasContext!.fill()
H
hdx 已提交
342
        })
H
hdx 已提交
343
        this.canvasContext!.draw()
H
hdx 已提交
344
      },
H
hdx 已提交
345 346 347 348 349 350
      setStrokeStyle() {
        ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
          this.canvasContext!.setStrokeStyle(item)
          this.canvasContext!.beginPath()
          this.canvasContext!.rect(0 + 75 * index, 0, 50, 50)
          this.canvasContext!.stroke()
H
hdx 已提交
351
        })
H
hdx 已提交
352
        this.canvasContext!.draw()
H
hdx 已提交
353
      },
H
hdx 已提交
354 355 356 357 358 359 360
      setGlobalAlpha() {
        this.canvasContext!.setFillStyle('#000000');
        [1, 0.5, 0.1].forEach((item : number, index : number) => {
          this.canvasContext!.setGlobalAlpha(item)
          this.canvasContext!.beginPath()
          this.canvasContext!.rect(0 + 75 * index, 0, 50, 50)
          this.canvasContext!.fill()
H
hdx 已提交
361
        })
H
hdx 已提交
362 363
        this.canvasContext!.draw()
        this.canvasContext!.setGlobalAlpha(1)
H
hdx 已提交
364
      },
H
hdx 已提交
365 366 367 368 369 370
      setShadow() {
        this.canvasContext!.beginPath()
        this.canvasContext!.setShadow(10, 10, 10, 'rgba(0, 0, 0, 199)')
        this.canvasContext!.rect(10, 10, 100, 100)
        this.canvasContext!.fill()
        this.canvasContext!.draw()
H
hdx 已提交
371
      },
H
hdx 已提交
372 373 374 375
      setFontSize() {
        [10, 20, 30, 40].forEach((item : number, index : number) => {
          this.canvasContext!.setFontSize(item)
          this.canvasContext!.fillText('Hello, world', 20, 20 + 40 * index)
H
hdx 已提交
376
        })
H
hdx 已提交
377
        this.canvasContext!.draw()
H
hdx 已提交
378
      },
H
hdx 已提交
379 380 381 382 383 384 385 386
      setLineCap() {
        this.canvasContext!.setLineWidth(10);
        ['butt', 'round', 'square'].forEach((item : string, index : number) => {
          this.canvasContext!.beginPath()
          this.canvasContext!.setLineCap(item)
          this.canvasContext!.moveTo(20, 20 + 20 * index)
          this.canvasContext!.lineTo(100, 20 + 20 * index)
          this.canvasContext!.stroke()
H
hdx 已提交
387
        })
H
hdx 已提交
388
        this.canvasContext!.draw()
H
hdx 已提交
389
      },
H
hdx 已提交
390 391 392 393 394 395 396 397 398
      setLineJoin() {
        this.canvasContext!.setLineWidth(10);
        ['bevel', 'round', 'miter'].forEach((item : string, index : number) => {
          this.canvasContext!.beginPath()
          this.canvasContext!.setLineJoin(item)
          this.canvasContext!.moveTo(20 + 80 * index, 20)
          this.canvasContext!.lineTo(100 + 80 * index, 50)
          this.canvasContext!.lineTo(20 + 80 * index, 100)
          this.canvasContext!.stroke()
H
hdx 已提交
399
        })
H
hdx 已提交
400
        this.canvasContext!.draw()
H
hdx 已提交
401
      },
H
hdx 已提交
402 403 404 405 406 407 408
      setLineWidth() {
        [2, 4, 6, 8, 10].forEach((item : number, index : number) => {
          this.canvasContext!.beginPath()
          this.canvasContext!.setLineWidth(item)
          this.canvasContext!.moveTo(20, 20 + 20 * index)
          this.canvasContext!.lineTo(100, 20 + 20 * index)
          this.canvasContext!.stroke()
H
hdx 已提交
409
        })
H
hdx 已提交
410
        this.canvasContext!.draw()
H
hdx 已提交
411
      },
H
hdx 已提交
412 413 414 415 416 417 418 419 420
      setMiterLimit() {
        this.canvasContext!.setLineWidth(4);
        [2, 4, 6, 8, 10].forEach((item : number, index : number) => {
          this.canvasContext!.beginPath()
          this.canvasContext!.setMiterLimit(item)
          this.canvasContext!.moveTo(20 + 80 * index, 20)
          this.canvasContext!.lineTo(100 + 80 * index, 50)
          this.canvasContext!.lineTo(20 + 80 * index, 100)
          this.canvasContext!.stroke()
H
hdx 已提交
421
        })
H
hdx 已提交
422
        this.canvasContext!.draw()
H
hdx 已提交
423 424 425 426 427 428
      }
    }
  }
</script>

<style>
H
hdx 已提交
429 430 431 432 433 434 435 436
  .page {
    flex: 1;
    height: 100%;
    overflow: hidden;
  }

  .scroll-view {
    flex: 1;
H
hdx 已提交
437
    padding-bottom: 50px;
H
hdx 已提交
438 439 440 441 442 443 444 445
  }

  .canvas-element {
    width: 100%;
    height: 500rpx;
    background-color: #ffffff;
  }

H
hdx 已提交
446 447
  .grid-view {
    padding: 10px;
H
hdx 已提交
448 449
    flex-direction: row;
    flex-wrap: wrap;
H
hdx 已提交
450 451
  }

H
hdx 已提交
452 453 454
  .grid-item {
    width: 50%;
    padding: 5px;
H
hdx 已提交
455
  }
H
hdx 已提交
456 457 458 459

  .btn-to-image {
    margin: 10px;
  }
H
hdx 已提交
460
</style>