child-canvas.uvue 1.1 KB
Newer Older
1 2
<template>
  <view>
3
    <canvas class="child-canvas" id="canvas" style="height: 100px;border: 1px solid red;"></canvas>
    <view>isCanvasContextNull: {{isCanvasContextNull}}</view>
4 5 6 7
  </view>
</template>

<script>
8 9 10 11 12
  type getContext = {
    ctx : boolean,
    hasFillRect : boolean
  }

13 14
  export default {
    data() {
15
      return {
        isCanvasContextNull : false
16 17
      }
    },
18
  
19 20 21 22 23 24
    mounted() {
      this.$nextTick(() => {
        uni.createCanvasContextAsync({
          id: 'canvas',
          component: this,
          success: (res : CanvasContext) => {
25
            const context = res.getContext('2d')!;
            this.isCanvasContextNull = true
26 27
            // 若干绘制调用
            // 绘制红色正方形
28 29
            context.fillStyle = "rgb(200, 0, 0)";
            context.fillRect(10, 10, 50, 50);
30 31

            // 绘制蓝色半透明正方形
32 33 34
            context.fillStyle = "rgba(0, 0, 200, 0.5)";
            context.fillRect(30, 30, 50, 50);
            context.draw();
35
          }
36

37 38 39 40
        })
      })
    }
  }
41
</script>