<canvas
id="myCanvas"
canvas-id="myCanvas"
type="2d"
:style="{width: canvasSize.width+'px',
height: canvasSize.height+'px',
position:'absolute',
top:'-1000px',
right:'1000px'}"
/>
data() {
return {
canvasSize: {
width: 200,
height: 200
},
}
}
uni.chooseImage({
count: 1, //默认9
sizeType: ['compressed'], // ['original', 'compressed'],可以指定是原图还是压缩图,默认二者都有
sourceType: ['camera'], // ['camera','album'], //这要注意,camera掉拍照,album是打开手机相册
success: (res) => {
//this.watermark(res.tempFilePaths[0])
}
});
uni.getImageInfo({
src: path,//图片路径
success: (image) => {
this.canvasSize.width = image.width;
this.canvasSize.height = image.height;
//尺寸重置没生效,故做延迟
setTimeout(() => {
let ctx = uni.createCanvasContext('myCanvas', this);
ctx.drawImage(path, 0, 0, image.width, image.height); // image.width, image.height
// 设置文字水印
ctx.setFillStyle('white');//颜色
ctx.setFontSize(30);//大小
ctx.fillText('哈哈哈', 10, 10, 500);//(text,x轴,y,width)
// 添加图片 drawImage(图片路径, x, y,图像宽, 图像高)
ctx.drawImage(path, 550, 1000, 120, 120);
// ctx.draw()
ctx.draw(false, () => {
// 导出图片
uni.canvasToTempFilePath({
canvasId: 'myCanvas',
destWidth: image.width,
destHeight: image.height,
fileType: 'png',
success: (res) => {
// this.savePhoto(res.tempFilePath, 'name');
//你的操作
}
},
this
);
});
}, 500)
}
});