canvas.uvue 1009 字节
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 16 17 18
        title: 'Canvas',
        drawing: null as any | null,
        canvasElement: null as any | null,
        canvasContext: null as any | null,
H
hdx 已提交
19 20
      }
    },
21 22 23 24 25 26 27
    onReady() {
      this.drawing = this.$refs['drawing'] as any;
      // @ts-ignore
      this.canvasElement = document.createElement('canvas')
      // @ts-ignore
      this.canvasElement.className = 'canvas'
      this.drawing!.appendChild(this.canvasElement)
H
hdx 已提交
28

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

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