this.imageUrl = this.canvas.toDataURL();
},
二.图片水印
watermark({
content = '',
container = '',
width = '',
height = '',
position = 'bottom-right',
font = '16px 微软雅黑',
fillStyle = 'rgba(255, 255, 255, 1)',
zIndex = 11000,
} = {}) {
let img = this.img
const imgRatio = img.naturalWidth / img.naturalHeight;
const ctxWidth = width || img.naturalWidth;
const ctxHeight = height || ctxWidth / imgRatio;
this.canvas.width = ctxWidth;
this.canvas.height = ctxHeight;
const ctx = this.ctx;
ctx.drawImage(img, 0, 0, ctxWidth, ctxHeight);
ctx.shadowColor = 'rgba(0, 0, 0, 0.2)';
ctx.shadowOffsetX = 2;
ctx.shadowOffsetY = 2;
ctx.shadowBlur = 2;
ctx.font = font;
ctx.fillStyle = fillStyle;
if(position == 'center') {
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(content, ctxWidth / 2, ctxHeight / 2, ctxWidth)
}else if(position == 'bottom-right') {
ctx.textAlign = 'right';
ctx.textBaseline = 'alphabetic';
ctx.fillText(content, ctxWidth-12, ctxHeight-12, ctxWidth)
}
const base64Url = this.canvas.toDataURL();
if(container) {
const div = document.createElement('div');
div.setAttribute('style', ` width: ${ctxWidth}px; height: ${ctxHeight}px; z-index: ${zIndex};
pointer-events: none; background-repeat: repeat; background-image: url('${base64Url}')`);
container.insertBefore(div, null);
}
this.imageUrl = base64Url
}参考
不能说的秘密——前端也能玩的图片隐写术
到此这篇关于vue中使用带隐藏文本信息的图片、图片水印的文章就介绍到这了,更多相关vue 隐藏文本信息图片水印内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!










