canvascontext.todata
主体: 企业支付宝小程序 、 个人支付宝小程序
获取画布指定区域的 base64 格式的 data url 数据。
object object
查看示例
属性 | 类型 | 默认值 | 必填 | 描述 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
x | number | 0 | 否 | 将要被提取的矩形区域的左上角横坐标 |
||||||||||
y | number | 0 | 否 | 将要被提取的矩形区域的左上角纵坐标 |
||||||||||
width | number | 被提取的矩形区域的左上角到画布右下角的横向距离 | 否 | 将要被提取的矩形区域的宽度 |
||||||||||
height | number | 被提取的矩形区域的左上角到画布右下角的纵向距离 | 否 | 将要被提取的矩形区域的高度 |
||||||||||
destwidth | number | 等于 width | 否 | 将要被提取的矩形区域提取后的宽度 |
||||||||||
destheight | number | 等于 height | 否 | 将要被提取的矩形区域提取后的高度 |
||||||||||
filetype | string | png | 否 | 图片格式 |
||||||||||
|
||||||||||||||
quality | number | - | 否 | 图片的质量,目前仅对 jpg 有效。取值范围为 (0, 1],不在范围内时当作 1 处理 |
||||||||||
success | function | - | 否 | 调用成功的回调函数 |
||||||||||
fail | function | - | 否 | 调用失败的回调函数 |
||||||||||
complete | function | - | 否 | 调用结束的回调函数(调用成功、失败都会执行) |
promise
属性 | 类型 | 描述 |
---|---|---|
promise返回值 | string | 提取的 base64 格式的 dataurl 字符串。 |
.js示例代码
// .js
const ctx = my.createcanvascontext('canvas');
ctx.setfillstyle('#108ee9');
ctx.arc(50, 50, 50, 0, math.pi * 2, true);
ctx.fill();
ctx.draw(false, () => {
ctx
.todataurl({
x: 50,
y: 50,
width: 50,
height: 50,
destwidth: 100,
destheight: 100,
})
.then(dataurl => {
ctx.drawimage(dataurl, 0, 0);
ctx.draw();
});
});