抖音开放平台Logo
控制台

Canvas.toTempFilePath
收藏
我的收藏

将当前 Canvas 保存为一个临时文件,并生成相应的临时文件路径。​

语法​

js
复制
Canvas.toTempFilePath(options);

参数说明​

options 为 object 类型,扩展属性描述如下:​
属性​
类型​
默认值​
是否必填​
说明​
x​
number​
0​
否​
截取 canvas 的左上角横坐标​
y​
number​
0​
否​
截取 canvas 的左上角纵坐标​
width​
number​
canvas 的宽度​
否​
截取 canvas 的宽度​
height​
number​
canvas 的高度​
否​
截取 canvas 的高度​
destWidth​
number​
canvas 的宽度​
否​
目标文件的宽度,会将截取的部分拉伸或压缩至该数值​
destHeight​
number​
canvas 的高度​
否​
目标文件的高度,会将截取的部分拉伸或压缩至该数值​
fileType​
string​
png​
否​
目标文件的类型​
quality​
number​
1.0​
否​
jpg 图片的质量,仅当 fileType 为 jpg 时有效。取值范围为 0.0(最低)- 1.0(最高),不含 0。不在范围内时当作 1.0​

fileType 取值说明​

值​
说明​
jpg​
jpg 文件​
png​
png 文件​

返回值​

返回值为 object 类型,详细参数描述如下:​
参数​
参数类型​
说明​
errMsg​
string​
状态信息​
toTempFilePath​
string​
canvas 生成的临时文件路径​

扫码体验​

代码示例​

js
复制
const canvas = tt.createCanvas();
canvas.getContext("2d");
// oTempFilePath 方法
canvas.toTempFilePath({
x: 20,
y: 20,
width: 200,
height: 150,
destWidth: 400,
destHeight: 300,
success: (res) => {
console.log(res.tempFilePath);
},
});
// toTempFilePathSync 方法
const tempFilePath = canvas.toTempFilePathSync({
x: 0,
y: 0,
width: canvas.width,
height: canvas.height,
destWidth: 500,
destHeight: 400,
});
console.log(tempFilePath);

Bug & Tip​

    Tip:不要在 tt.createCanvas 后立即调用该方法,在 Canvas 创建后的初始状态下调用该方法可能会导致异常。​