canvas.md 1.2 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4
## canvas

<!-- UTSCOMJSON.canvas.description -->

D
DCloud_LXH 已提交
5 6
<!-- UTSCOMJSON.canvas.compatibility -->

7
<!-- UTSCOMJSON.canvas.attribute -->
D
DCloud_LXH 已提交
8 9 10

<!-- UTSCOMJSON.canvas.event -->

11 12
<!-- UTSCOMJSON.canvas.component_type-->

D
DCloud_LXH 已提交
13 14
<!-- UTSCOMJSON.canvas.children -->

15 16
<!-- UTSCOMJSON.canvas.example -->

D
DCloud_LXH 已提交
17
<!-- UTSCOMJSON.canvas.reference -->
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 44

## API

和 W3C 规范保持一致 [https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/canvas](https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/canvas)

**注意事项**@hidpi

uni-app 1 canvas 组件内部默认处理了高清逻辑,uni-app x 和 W3C 规范一致,需要开发者根据自己的场景手动处理

```html
<template>
  <canvas id="canvas"></canvas>
</template>
<script setup>
// 获取 canvas element
const canvas = uni.getElementById("canvas") as UniCanvasElement
const context = canvas.getContext("2d")!;

// 处理高清屏逻辑
const dpr = uni.getSystemInfoSync().pixelRatio;
canvas.width = canvas.offsetWidth * dpr;
canvas.height = canvas.offsetHeight * dpr;
context.scale(dpr, dpr); // 仅需调用一次,当调用 reset 方法后需要再次 scale

// 省略绘制代码,和 w3c 规范保持一致
</script>
```