From 4e1e0e7ce263836bf787fdb445eed44dec27377e Mon Sep 17 00:00:00 2001 From: zouzhibing Date: Fri, 17 Jun 2022 14:27:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=B0=B4=E5=8D=B0=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/u-container-layout/index.vue | 7 +- src/router/modules/other.ts | 12 +- src/utils/waterMarker.ts | 75 ++++++++++ src/views/other/cropper/index.vue | 56 +++---- src/views/other/water-marker.vue | 153 ++++++-------------- 5 files changed, 162 insertions(+), 141 deletions(-) create mode 100644 src/utils/waterMarker.ts diff --git a/src/components/u-container-layout/index.vue b/src/components/u-container-layout/index.vue index f47452d..10b1496 100644 --- a/src/components/u-container-layout/index.vue +++ b/src/components/u-container-layout/index.vue @@ -15,11 +15,14 @@ //margin: 20px; padding: 20px; box-sizing: border-box; - + width: 100%; + height: 100%; .m-container-layout-inner{ background: white; padding: 20px; - background: white; + width: 100%; + box-sizing: border-box; + min-height: 100%; } } diff --git a/src/router/modules/other.ts b/src/router/modules/other.ts index 05b29e1..0487e0a 100644 --- a/src/router/modules/other.ts +++ b/src/router/modules/other.ts @@ -54,12 +54,12 @@ const othersRouter = { name: 'qrcode', meta: { title: '生成二维码' } }, - // { - // path: 'water-marker', - // component: () => import('@/views/other/water-marker.vue'), - // name: 'water-marker', - // meta: { title: '生成水印' } - // }, + { + path: 'water-marker', + component: () => import('@/views/other/water-marker.vue'), + name: 'water-marker', + meta: { title: '生成水印' } + }, { path: 'right-menu', component: () => import('@/views/other/right-menu.vue'), diff --git a/src/utils/waterMarker.ts b/src/utils/waterMarker.ts new file mode 100644 index 0000000..e403237 --- /dev/null +++ b/src/utils/waterMarker.ts @@ -0,0 +1,75 @@ +function watermark (options) { + const { + container = document.body, // 容器 + width = '240', // canvas元素宽 + height = '100', // canvas元素高 + textAlign = 'left', // 文字对齐 + textBaseline = 'bottom', // 基准线 + font = '16px Microsoft Yahei', // 字体大小及样式 + fillStyle = '#000', // 自定义水印的颜色 + content = 'Vue Admin Perfect', // 水印内容 + globalAlpha = 0.3, // 设置图形和图像透明度的值 + rotate = 16, // 文字旋转角度 + zIndex = 1000, // 元素堆叠顺序 + isCancel =true + } = options + + const canvas = document.createElement('canvas') + canvas.setAttribute('width', width) + canvas.setAttribute('height', height) + const ctx = canvas.getContext('2d') // 获取 canvas2d 上下文 + ctx.globalAlpha = globalAlpha + ctx.textAlign = textAlign + ctx.textBaseline = textBaseline + ctx.font = font + ctx.fillStyle = fillStyle + ctx.rotate((Math.PI * rotate) / 180) + // ctx.rotate(-10 * Math.PI / 140); + ctx.fillText(content, 50, 50) + + const base64Url = canvas.toDataURL() // 返回一个包含图片展示的 data URI + + const __wm = document.querySelector('.__wm')// 选择器 + const watermarkDiv = __wm || document.createElement('div') + const styleStr = ` + position:absolute; + top:0px; + left:0px; + width:100%; + height:100%; + z-index:${zIndex}; + pointer-events:none; + background-repeat:repeat; + background-image:url('${base64Url}')` + + watermarkDiv.setAttribute('style', styleStr) + watermarkDiv.classList.add('__wm') // 为元素添加“__wm”类名 + + container.style.position = 'relative' + if (!__wm) { + container.appendChild(watermarkDiv) // 添加元素 + } + + // const MutationObserver = window.MutationObserver || window.WebKitMutationObserver + // // 检查浏览器是否支持这个API + // if (MutationObserver) { + // const args = arguments[0] + // let mo = new MutationObserver(function () { + // const __wm = document.querySelector('.__wm') + // // 只在__wm元素变动才重新调用 + // if ((__wm && __wm.getAttribute('style') !== styleStr) || !__wm || document.body.style.position !== 'relative') { + // console.log('============',isCancel) + // // 避免一直触发 + // mo.disconnect() + // mo = null + // watermark(args) + // } + // }) + // mo.observe(document.body, { + // attributes: true, // 观察目标节点的属性节点 + // subtree: false, // 观察目标节点的所有后代节点 + // childList: true // 观察目标节点的子节点 + // }) + // } +} +export default watermark diff --git a/src/views/other/cropper/index.vue b/src/views/other/cropper/index.vue index 0afaed7..db531c6 100644 --- a/src/views/other/cropper/index.vue +++ b/src/views/other/cropper/index.vue @@ -1,8 +1,9 @@