canvas.uvue 5.2 KB
Newer Older
X
xty 已提交
1
<template>
2 3
  <view class="page" id="page-canvas">
    <canvas id="canvas" class="canvas-element"></canvas>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    <scroll-view class="scroll-view">
      <button class="canvas-drawing-button" id="useAsync" @click="useAsync">使用异步方式设置(跨平台推荐)</button>
      <button class="canvas-drawing-button" id="useSync" @click="useSync">使用同步方式设置</button>
      <!-- #ifdef WEB -->
      <button class="canvas-drawing-button" @click="canvasToBlob">canvasToBlob</button>
      <view>
        <text>testToBlobResult: {{testToBlobResult}}</text>
      </view>
      <!-- #endif -->
      <button class="canvas-drawing-button" id="toDataURL" @click="canvasToDataURL">canvasToDataURL</button>
      <view v-if='dataBase64.length>0' :style="{
        width: canvasWidth,
        height: canvasHeight,
        padding:'10px',
      }">
        <text>canvasToDataURL 返回结果:</text>
        <text>{{dataBase64.slice(0,35)}}...</text>
        <view>
          <text>canvasToDataURL 转图片预览:</text>
        </view>
        <image mode='aspectFit' :src="dataBase64"></image>
      </view>
26 27 28 29 30 31 32
    </scroll-view>
  </view>
</template>

<script>
  function hidpi(canvas : UniCanvasElement) {
    const context = canvas.getContext("2d")!;
33
    const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1;
34 35 36 37 38 39 40 41
    canvas.width = canvas.offsetWidth * dpr;
    canvas.height = canvas.offsetHeight * dpr;
    context.scale(dpr, dpr);
  }

  export default {
    data() {
      return {
42 43 44
        title: 'Context2D',
        // canvas: null as UniCanvasElement | null,
        // canvasContext: null as CanvasRenderingContext2D | null,
45
        canvasWidth: 0,
46
        canvasHeight: 0,
47
        dataBase64: '',
48
        // 仅测试
49
        testCanvasContext: false,
50 51
        testToBlobResult: false,
        testToDataURLResult: false
52 53 54
      }
    },
    onReady() {
55 56 57
      // this.useAsync()
    },
    methods: {
58
      // #ifdef WEB
59 60 61 62 63
      canvasToBlob() {
        this.canvas!.toBlob((blob : Blob) => {
          this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg')
        }, 'image/jpeg', 0.95)
      },
64
      // #endif
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
      canvasToDataURL() {
        this.dataBase64 = this.canvas!.toDataURL()
      },
      useAsync() {

        // HBuilderX 4.25+
        // 异步调用方式, 跨平台写法
        uni.createCanvasContextAsync({
          id: 'canvas',
          component: this,
          success: (context : CanvasContext) => {
            const canvasContext = context.getContext('2d')!;
            this.canvasContext = canvasContext
            this.testCanvasContext = true
            const canvas = canvasContext.canvas;
            canvasContext.save()
            hidpi(canvas);
            this.canvasWidth = canvas.width;
            this.canvasHeight = canvas.height;
            this.drawImage()

            this.testToDataURLResult = canvas.toDataURL().startsWith('data:image/png;base64')

            this.canvas = canvas;
          }
        })

      },
      useSync() {

        let canvas = uni.getElementById("canvas") as UniCanvasElement
        this.canvasContext = canvas.getContext("2d")
        this.testCanvasContext = true
        this.canvasContext!.save()
        hidpi(canvas);
        this.canvasWidth = canvas.width;
        this.canvasHeight = canvas.height;
        this.arc()

        // #ifdef WEB
        canvas.toBlob((blob : Blob) => {
          this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg')
        }, 'image/jpeg', 0.95)
        // #endif
        this.testToDataURLResult = canvas.toDataURL().startsWith('data:image/png;base64')

        this.canvas = canvas;

      },
      arc() {
        const context = this.canvasContext!

        this.clearCanvasRect()
        context.beginPath()
        context.lineWidth = 2
        context.arc(75, 75, 50, 0, Math.PI * 2, true)
        context.moveTo(110, 75)
        context.arc(75, 75, 35, 0, Math.PI, false)
        context.moveTo(65, 65)
        context.arc(60, 65, 5, 0, Math.PI * 2, true)
        context.moveTo(95, 65)
        context.arc(90, 65, 5, 0, Math.PI * 2, true)
        context.stroke()

        context.restore()
      },
      drawImage() {
        const context = this.canvasContext!

        this.clearCanvasRect()
        const text = "uni-app x,是下一代 uni-app,是一个跨平台应用开发引擎"

        context.font = "20px 宋体"
        context.fillStyle = "red"
        context.fillText(text, 0, 60)
        const textMetrics = context.measureText(text)
        context.strokeText(text, 40, 100)
        context.fillText("measure text width:" + textMetrics.width, 40, 80)

        context.restore()
      },
      clearCanvasRect() {
        this.canvasContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
148 149 150
      }
    }
  }
X
xty 已提交
151 152 153
</script>

<style>
154 155 156 157 158
  .page {
    flex: 1;
    height: 100%;
    overflow: hidden;
  }
X
xty 已提交
159

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  .scroll-view {
    flex: 1;
  }

  .canvas-element {
    width: 100%;
    height: 250px;
    background-color: #ffffff;
  }

  .grid-view {
    padding: 10px;
    flex-direction: row;
    flex-wrap: wrap;
  }

  .btn-to-image {
    margin: 10px;
  }
X
xty 已提交
179
</style>