canvas.uvue 1.0 KB
Newer Older
H
hdx 已提交
1 2
<template>
  <view>
H
hdx 已提交
3
    <page-head :title="title"></page-head>
H
hdx 已提交
4
    <view class="page-body">
5 6
      <!-- 如何使用浏览器内置 canvas -->
      <view ref="drawing" class="drawing"></view>
H
hdx 已提交
7 8 9 10 11 12 13 14
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
15
        title: 'Canvas',
H
hdx 已提交
16 17 18
        drawing: null as UniElement | null,
        canvasElement: null as HTMLCanvasElement | null,
        canvasContext: null as CanvasRenderingContext2D | null,
H
hdx 已提交
19 20
      }
    },
21
    onReady() {
H
hdx 已提交
22 23
      this.drawing = this.$refs['drawing'] as UniElement;
      this.canvasElement = document.createElement('canvas') as HTMLCanvasElement;
24 25
      this.canvasElement.className = 'canvas'
      this.drawing!.appendChild(this.canvasElement)
H
hdx 已提交
26

27 28
      this.canvasContext = this.canvasElement!.getContext('2d')
      this.canvasContext!.fillRect(100, 50, 100, 50)
H
hdx 已提交
29 30 31 32 33
    }
  }
</script>

<style>
H
hdx 已提交
34
  .drawing>>>.canvas {
35 36
    width: 300px;
    height: 150px;
H
hdx 已提交
37 38
    margin-left: auto;
    margin-right: auto;
39
    border: 1px dashed #000;
H
hdx 已提交
40 41
  }
</style>