(function () {
// svg 实现 watermark
function __svgWM({
container = document.body,
content = '请勿外传',
width = '300px',
height = '200px',
fillStyle = 'rgba(184, 184, 184, 0.6)',
fontSize = '12px',
zIndex = 1000,
rotate='0'
} = {}) {
const args = arguments[0];
const svgStr = `
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${width}">
<text x="0%" y="0%" dy="${fontSize}"
text-anchor="left"
stroke="none"
stroke-width="1"
fill="${fillStyle}"
style="font-size: ${fontSize};">
${content}
</text>
<text x="50%" y="50%" dy="${fontSize}"
text-anchor="left"
stroke="none"
stroke-width="1"
fill="${fillStyle}"
style="font-size: ${fontSize};">
${content}
</text>
</svg>`;
const base64Url = `data:image/svg+xml;base64,${window.btoa(unescape(encodeURIComponent(svgStr)))}`;
const __wm = document.querySelector('.__wm');
const watermarkDiv = __wm || document.createElement("div");
const styleStr = `
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
z-index:${zIndex};
pointer-events:none;
background-repeat:repeat;
transform: rotate(${rotate}deg);
background-image:url('${base64Url}')
`;
watermarkDiv.setAttribute('style', styleStr);
watermarkDiv.classList.add('__wm');
if (!__wm) {
container.style.position = 'relative';
container.insertBefore(watermarkDiv, container.firstChild);
}
}
if (typeof module != 'undefined' && module.exports) {
//CMD
module.exports = __svgWM;
} else if (typeof define == 'function' && define.amd) {
// AMD
define(function() {
return __svgWM;
});
} else {
window.__svgWM = __svgWM;
}
})();