前端水印实现方式 All In One
前端水印实现方式 All In One
watermark
canvas to image
canvas.toDataURL(type, encoderOptions);
SVG & base64
function drawInlineSVG(ctx, rawSVG, callback) {
var svg = new Blob([rawSVG], {type:"image/svg+xml;charset=utf-8"}),
domURL = self.URL || self.webkitURL || self,
url = domURL.createObjectURL(svg),
img = new Image;
img.onload = function () {
ctx.drawImage(this, 0, 0);
domURL.revokeObjectURL(url);
callback(this);
};
img.src = url;
}
// usage:
drawInlineSVG(ctxt, svgText, function() {
console.log(canvas.toDataURL()); // -> PNG data-uri
});
https://stackoverflow.com/questions/27230293/how-to-draw-an-inline-svg-in-dom-to-a-canvas
Canvas toDataURL All In One
https://www.cnblogs.com/xgqfrms/p/15652208.html
convert image to base64 in javascript
https://www.cnblogs.com/xgqfrms/p/12903214.html
demo
solution ✅
const userName = "xgqfrms";
const watermark = `
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="svg" viewBox="0 0 100 100" width="100" height="100">
<text x="25" y="50" fill="#00ff00">${userName}</text>
</svg>
`
// const watermark = `
// <svg xmlns="http://www.w3.org/2000/svg" version="1.1" id="svg" viewBox="0 0 200 200" width="100" height="100">
// <text x="0" y="0" fill="#00ff00">${userName}</text>
// </svg>
// `
const app = document.querySelector(`#app`);
app.insertAdjacentHTML('beforeend', watermark);
const svgElement = document.querySelector(`#svg`);
console.log(`svgElement =`, svgElement);
const svgURL = new XMLSerializer().serializeToString(svgElement);
console.log(`svgURL =`, svgURL);
// const imgSrc = `data:image/svg+xml; charset=utf8, ${encodeURIComponent(svgURL)}`;
// const imgSrc = `data:image/svg+xml; charset=utf8, ${window.btoa(svgURL)}`;
const imgSrc = `data:image/svg+xml;base64,${window.btoa(svgURL)}`;
console.log(`imgSrc =`, imgSrc);
// app.style.background = `left 5% / 15% 60% repeat url(${imgSrc})`;
app.style.background = `left 5% / 15% 60% repeat url(${imgSrc})`;
// app.style.background = `url(${imgSrc}) repeat`;
// app.style.backgroundImage = `url(${imgSrc})`;
// app.style.backgroundRepeat = `repeat`;
// app.style.backgroundPosition = `0 0`;
const img = document.querySelector(`#img`);
img.src = imgSrc;
const pre = document.querySelector(`#pre`);
pre.insertAdjacentText('beforeend', imgSrc);
// const canvas = document.getElementById("canvas");
// const ctx = canvas.getContext("2d");
// const dataURL = canvas.toDataURL();
// console.log(`dataURL =`, dataURL);
refs
https://stackoverflow.com/a/70863414/5934465
https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/15847231.html
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2021-01-26 邮件撤回功能实现原来剖析
2021-01-26 js arrow function return object
2019-01-26 macOS & SVN