画布canvas组件官方示例为什么要乘以设备像素比?
canvas组件下,有一段示例代码,如下:
<!-- canvas.wxml -->
<canvas type="2d" id="myCanvas"></canvas>
// canvas.js
Page({
onReady() {
const query = wx.createSelectorQuery()
query.select('#myCanvas')
.fields({ node: true, size: true })
.exec((res) => {
const canvas = res[0].node
const ctx = canvas.getContext('2d')
const dpr = wx.getSystemInfoSync().pixelRatio
canvas.width = res[0].width * dpr
canvas.height = res[0].height * dpr
ctx.scale(dpr, dpr)
ctx.fillRect(0, 0, 100, 100)
})
}
})
不太理解长宽为什么要乘以dpr,并且画布缩放同样的倍数。我把这些去掉后预览看起来和上面一样:
canvas.width = res[0].width
canvas.height = res[0].height
// ctx.scale(dpr, dpr)