- 隐藏canvas,position:fixed;left:100%
<canvas
:style="{width:canvasWidth,height:canvasHeight}"
style="position:fixed;left:100%;"
canvas-id="myCanvas"
></canvas>
- 上传图片添加水印
uni.getImageInfo({
src: filePath,
success: (res) => {
this.canvasWidth = `${res.width}px`
this.canvasHeight = `${res.height}px`
const ctx = uni.createCanvasContext('myCanvas', this)
ctx.clearRect(0, 0, res.width, res.height)
ctx.beginPath()
ctx.drawImage(filePath, 0, 0, res.width, res.height)
ctx.translate(res.width / 2, res.height / 2)
ctx.rotate((-30 * Math.PI) / 180)
const horizontal = res.width / 6
const vertical = res.height / 3
const fonsize = res.width / 30
for (let i = 0; i <= 8; i++) {
for (let j = 0; j <= 6; j++) {
ctx.beginPath()
ctx.setFontSize(`${fonsize}px serif`)
ctx.setFillStyle('rgba(0, 0, 0, 0.5)')
ctx.setStrokeStyle('rgba(0, 0, 0, 0.5)')
ctx.fillText('仅用于****小程序实名认证使用', (i * horizontal - res.width / 4) * 4, j * vertical - res.height, fonsize * 30)
}
}
ctx.draw(false, () => {
setTimeout(() => {
uni.canvasToTempFilePath({
canvasId: 'myCanvas',
destWidth: res.width,
destHeight: res.height,
fileType: 'jpg',
quality: 0.8,
success: (res) => {
this.uploadImg(res.tempFilePath, prop)
},
fail: (err) => {
console.log(err)
this.formData[`${prop}Loading`] = false
},
}, this)
}, 500)
})
},
fail: (err) => {
console.log(err)
},
})
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律