create-canvas-context-async.uvue 1.3 KB
Newer Older
1 2 3 4
<template>
  <view>
    <view>create canvas context async</view>
    <canvas id="canvas" class="canvas-home" style="height: 100px;border: 1px solid blue;"></canvas>
5
    <view style="margin-top: 30px;"></view>
    <view>isCanvasContextNull: {{isCanvasContextNull}}</view>
6 7 8 9 10 11
    <child-canvas class="node-child-component" />
  </view>
</template>

<script>
  import childCanvas from './child-canvas.uvue';
12 13 14 15 16

  type getContext = {
    ctx : boolean,
    hasFillRect : boolean
  }
17 18 19 20 21 22
  export default {
    components: {
      childCanvas
    },
    data() {
      return {
23
        isCanvasContextNull: false
24 25 26
      }
    },
    methods: {
27

28 29 30 31 32
      drawImage() {
        uni.createCanvasContextAsync({
          id: 'canvas',
          component: null,
          success: (res : CanvasContext) => {
33 34
            const context = res.getContext('2d')!;
            this.isCanvasContextNull = true
35 36
            // 若干绘制调用
            // 绘制红色正方形
37 38
            context.fillStyle = "rgb(200, 0, 0)";
            context.fillRect(10, 10, 50, 50);
39 40

            // 绘制蓝色半透明正方形
41 42 43
            context.fillStyle = "rgba(0, 0, 200, 0.5)";
            context.fillRect(30, 30, 50, 50);
            context.draw();
44 45 46 47 48 49 50 51
          }
        })
      }
    },
    onReady() {
      this.drawImage()
    }
  }
52
</script>