当前位置:   article > 正文

【sgWatermark.js】自定义组件:基于Vue2+html2canvas实现全局水印效果_vue html2canvas 水印

vue html2canvas 水印

 

sgWatermark.js源码 

  1. import html2canvas from 'html2canvas'; // npm install --save html2canvas (官网:https://html2canvas.hertzen.com)
  2. export default {
  3. addWatermark({
  4. container = document.body, // 水印添加到的容器
  5. innerHTML = "水印文字内容", // 水印文字的内容
  6. rotate = -30,//默认文字旋转角度
  7. cssText = `
  8. box-sizing: border-box;
  9. width: max-content;
  10. height: max-content;
  11. padding: 30px;
  12. font-size: 12px;
  13. font-family: 'Microsoft YaHei';
  14. color: #00000022;
  15. transform-origin:center;
  16. transform: rotate(-30deg);
  17. z-index: 999999;
  18. pointer-events: none;
  19. display: flex;
  20. justify-content: center;
  21. align-items: center;
  22. `,//水印模板文本的默认样式
  23. className = '__sgWatermark__',//水印类名
  24. } = {}) {
  25. let dom = document.createElement("div");
  26. dom.innerHTML = innerHTML;
  27. dom.style.cssText = cssText;//默认样式
  28. dom.style.transform = `rotate(${rotate}deg)`;//旋转角度
  29. container.appendChild(dom);//添加模板文本
  30. // ----------------------------------------
  31. html2canvas(dom, { backgroundColor: 'transparent', logging: false, }).then(canvas => {
  32. dom.parentNode.removeChild(dom);//移除模板文本
  33. let base64Url = canvas.toDataURL('image/png', 1.0);
  34. const __sgWatermark__ = document.querySelector(`.${className}`);
  35. const watermarkDiv = __sgWatermark__ || document.createElement("div");
  36. watermarkDiv.style.cssText = `transition: .618s ease;opacity: 0;position: fixed; top: 0; left: 0; bottom: 0; right: 0; pointer-events: none; z-index: 999999999; background: url(${base64Url}); `;
  37. setTimeout(() => {
  38. watermarkDiv.style.opacity = 1;//缓慢出现
  39. }, 0);
  40. watermarkDiv.className = className;
  41. __sgWatermark__ || container.insertBefore(watermarkDiv, container.firstChild);
  42. });
  43. },
  44. removeWatermark(
  45. className = '__sgWatermark__',//水印类名
  46. ) {
  47. const dom = document.querySelector(`.${className}`);
  48. dom.parentNode.removeChild(dom);
  49. },
  50. };

示例

  1. <template>
  2. <div :class="$options.name">
  3. <el-button type="primary" style="width: 300px; height: 100px">主要按钮</el-button>
  4. <div
  5. ref="div"
  6. style="
  7. width: 600px;
  8. height: 300px;
  9. background-color: #00000011;
  10. display: flex;
  11. justify-content: center;
  12. align-items: center;
  13. "
  14. >
  15. 局部div
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. import sgWatermark from "@/js/sgWatermark";
  21. export default {
  22. mounted() {
  23. // 示例1
  24. sgWatermark.addWatermark();
  25. // 示例2
  26. // sgWatermark.addWatermark({ container: this.$el, innerHTML: `高启强(No:201314)` });
  27. // 示例3
  28. // sgWatermark.addWatermark({ container: this.$refs.div, innerHTML: `<b style="color: #F56C6C88;font-weight: bold;font-size: 24px;" >你挚爱的强哥</b>` });
  29. },
  30. };
  31. </script>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/985831
推荐阅读
相关标签
  

闽ICP备14008679号