child-canvas.uvue 1.0 KB
Newer Older
辛宝Otto's avatar
辛宝Otto 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<template>
  <view>
    <canvas class="child-canvas" id="canvas" style="height: 100px;border: 1px solid red;"></canvas>
    <view>isCanvasContextNull: {{isCanvasContextNull}}</view>
  </view>
</template>

<script>
  type getContext = {
    ctx : boolean,
    hasFillRect : boolean
  }

  export default {
    data() {
      return {
        isCanvasContextNull : false
      }
    },
    mounted() {
      this.$nextTick(() => {
        uni.createCanvasContextAsync({
          id: 'canvas',
          component: this,
          success: (res : CanvasContext) => {
            const context = res.getContext('2d')!;
            this.isCanvasContextNull = true
            // 若干绘制调用
            // 绘制红色正方形
            context.fillStyle = "rgb(200, 0, 0)";
            context.fillRect(10, 10, 50, 50);

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

        })
      })
    }
  }
</script>