From 65894066b5f829c047c1be0f1fa3f42d12e595fe Mon Sep 17 00:00:00 2001 From: a0049873 <79py69t9wb@privaterelay.appleid.com> Date: Wed, 16 Nov 2022 16:25:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B0=B4=E5=8D=B0=E7=9B=B8=E5=85=B3=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/App.vue | 176 +++++++++++++++++++++++++---------- front/src/utils/waterMark.js | 69 ++++++++++++++ 2 files changed, 198 insertions(+), 47 deletions(-) create mode 100644 front/src/utils/waterMark.js 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