Canvas

canvas.toDataURL()

该方法将画布上的内容以支持图片展示的 data URI 的格式返回。

canvas.toDataURL();

参数值:

参数 类型 默认值 是否必填 描述
type string 'image/png' 图片的格式,默认值为 image/png
encoderOptions number 在指定图片格式为 image/jpeg 时,可以从 0 到 1 的区间内选择图片的质量。如果超出取值范围,将会使用默认值 0.92

返回值:

类型 描述
string data URI 格式的字符串

示例:

let dataURL = canvas.toDataURL();
console.log(dataURL);

canvas.toTempFilePath()

该方法将当前 canvas 保存为一个临时图像文件。注意,当参数中的 x, y, width, height 组成的截取矩形,与当前 canvas 无交集,那么截取的内容为空,会执行 fail 回调函数,并且回调函数的参数 res.tempFilePath 为 null。

canvas.toTempFilePath(opts);

参数值:

属性 类型 默认值 是否必填 描述
opts Object 调用该方法时,传入的对象参数
opts.x number 0 截取保存的 canvas 左上角横坐标
opts.y number 0 截取保存的 canvas 左上角纵坐标
opts.width number canvas 的宽度 截取保存的 canvas 宽度。宽度为 0 或负值等异常值时,会执行 fail 回调,返回的 res.tempFilePath 为 null
opts.height number canvas 的高度 截取保存的 canvas 高度。 高度为 0 或负值等异常值时,会执行 fail 回调,返回的 res.tempFilePath 为 null
opts.destWidth number canvas 的宽度 目标文件的宽度,会将截取的 canvas 部分,拉伸或压缩至该数值。宽度为 0 或负值等异常值时,会执行 fail 回调,返回的 res.tempFilePath 为 null
opts.destHeight number canvas 的高度 目标文件的高度,会将截取的 canvas 部分,拉伸或压缩至该数值。高度为 0 或负值等异常值时,会执行 fail 回调,返回的 res.tempFilePath 为 null
opts.fileType string png 目标文件的类型,只能为 jpg 或 png 两种类型
opts.quality number 1.0 jpg 图像的质量,仅当 fileType 为 jpg 时才有效。取值范围为 0.0(最低)~ 1.0(最高),不含 0,不在范围内时当作 1.0
opts.success function 接口调用成功的回调函数
opts.fail function 接口调用失败的回调函数
opts.complete function 接口调用完成的回调函数(接口成功、失败都会执行)

success 回调函数:

形如 function (res) {...}, 其中:

属性 类型 描述
res Object 回调函数参数对象
res.tempFilePath string 生成的临时文件路径

fail 回调函数:

形如 function (res) {...}, 其中:

属性 类型 描述
res Object 回调函数参数对象
res.errMsg string 报错信息
res.tempFilePath string 一般 fail 回调里面,该项值都为 null

complete 回调函数:

当接口调用成功时,其返回值与 success 回调函数的返回值相同;
当接口调用失败时,其返回值与 fail 回调函数的返回值相同。

示例:

canvas.toTempFilePath({
    x: 10,
    y: 10,
    width: 40,
    height: 30,
    destWidth: 200,
    destHeight: 200,
    success: res => {
        const image = swan.createImage();
        image.src = res.tempFilePath;
        image.onload = () => {
            ctx.drawImage(image, 50, 80);
        };
    }
});

canvas.toTempFilePathSync()

canvas.toTempFilePath() 的同步版本。在异步版本中,执行 fail 回调的,在同步版本中都会抛异常。

canvas.toTempFilePathSync(opts);

参数值:

属性 类型 默认值 是否必填 描述
opts Object 调用该方法时,传入的对象参数
opts.x number 0 截取保存的 canvas 左上角横坐标
opts.y number 0 截取保存的 canvas 左上角纵坐标
opts.width number canvas 的宽度 截取保存的 canvas 宽度。宽度为 0 或负值等异常值时,会抛异常
opts.height number canvas 的高度 截取保存的 canvas 高度。高度为 0 或负值等异常值时,会抛异常
opts.destWidth number canvas 的宽度 目标文件的宽度,会将截取的 canvas 部分拉伸或压缩至该数值。宽度为 0 或负值等异常值时,会抛异常
opts.destHeight number canvas 的高度 目标文件的高度,会将截取的 canvas 部分拉伸或压缩至该数值。高度为 0 或负值等异常值时,会抛异常
opts.fileType string png 目标文件的类型,只能为 jpg 或 png 两种类型
opts.quality number 1.0 jpg 图像的质量,仅当 fileType 为 jpg 时才有效。取值范围为 0.0(最低)~ 1.0(最高),不含 0,不在范围内时当作 1.0

返回值:

类型 描述
string 生成的临时文件路径

示例:

const tempFilePath = canvas.toTempFilePathSync({
    x: 10,
    y: 10,
    width: 40,
    height: 30,
    destWidth: 200,
    destHeight: 300,
    fileType: 'jpg',
    quality: 0.2
});
const image = swan.createImage();
image.src = tempFilePath;
image.onload = () => {
    ctx.drawImage(image, 80, 50);
};