diff --git a/front/src/App.vue b/front/src/App.vue index 82c2265f..4926fa93 100644 --- a/front/src/App.vue +++ b/front/src/App.vue @@ -1,8 +1,8 @@ diff --git a/front/src/utils/waterMark.js b/front/src/utils/waterMark.js new file mode 100644 index 00000000..edc0c21e --- /dev/null +++ b/front/src/utils/waterMark.js @@ -0,0 +1,69 @@ +/* + * @Author: Light + * @Date: 2022-11-14 15:01:54 + * @LastEditors: Light + * @LastEditTime: 2022-11-16 09:57:28 + * @Description: 水印 + */ +const watermark = { + timer: null, + set: () => {}, +} +const setWatermark = (str, width, height, size, top, index) => { + console.log('生成水印') + const id = str.charCodeAt() + if (document.getElementById(id) !== null) { + document.body.removeChild(document.getElementById(id)) + } + const can = document.createElement('canvas') + can.width = width + can.height = height + + const cans = can.getContext('2d') + cans.rotate((-20 * Math.PI) / 180) + cans.font = size + 'px Microsoft YaHei' + cans.fillStyle = 'rgba(200, 200, 200, 0.4)' + cans.textAlign = 'left' + cans.textBaseline = 'middle' + cans.fillText(str, can.width / 3, can.height / 2) + + const WATER_MARK = document.createElement('div') + const ALL_WIDTH = document.documentElement.clientWidth + 'px' + const ALL_HEIGHT = document.documentElement.clientHeight + 'px' + + WATER_MARK.id = id + WATER_MARK.style.cssText = ` + position: fixed; + top: ${index * top}px; + left: 0; + zIndex: 99999999; + pointer-events: none; + width: ${ALL_WIDTH}; + height: ${ALL_HEIGHT}; + background: url(${can.toDataURL('image/png')}) left top repeat; + opacity: 0; + ` + document.body.appendChild(WATER_MARK) + return id +} + +// 该方法只允许调用一次 +watermark.set = (str, width, height, size, top, index) => { + let id = setWatermark(str, width, height, size, top, index) + watermark.timer && clearTimeout(watermark.timer) + watermark.timer = setTimeout(() => { + if (document.getElementById(id) === null) { + id = setWatermark(str, width, height, size, top, index) + } + }, 300) + window.onresize = () => { + setWatermark(str, width, height, size, top, index) + } +} + +const onWholeWaterMark = (arr, width, height, size, top) => { + arr.map((str, index) => { + watermark.set(str, width, height, size, top, index) + }) +} +export default onWholeWaterMark