返回数据:
//[io.dcloud.canvas.ImageDataImpl]
{
"data":
//[io.dcloud.uts.Uint8ClampedArray]
{},
"height":
//[Int]
1,
"width":
//[Int]
1
}
运行代码:
onReady(() => {
uni.createCanvasContextAsync({
id: 'canvas',
component: getCurrentInstance()?.proxy,
success: (context : CanvasContext) => {
const canvasContext = context.getContext('2d')!;
const canvas = canvasContext.canvas;
// 处理高清屏逻辑
const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1;
canvas.width = canvas.offsetWidth * dpr;
canvas.height = canvas.offsetHeight * dpr;
canvasContext.scale(dpr, dpr); // 仅需调用一次,当调用 reset 方法后需要再次 scale
const image = context.createImage();
image.src = "../../static/img-color-disk.png";
image.onload = () => {
console.log('image onLoad');
canvasContext.drawImage(image, 0, 0, canvas.offsetWidth, canvas.offsetHeight);
setTimeout(() => {
console.log(canvasContext.getImageData(20, 20, 1, 1))
}, 3000)
}
}
})
})
0 个回复