• Vue框架项目,给容器添加水印watermark


    1、在@/utils下新增一个名为waterMark.js的脚本

    具体水印样式可以在代码里自行调节style

    参数 - 水印内容, 加水印的容器, 是否显示时间

    1. let watermark = {};
    2. function getCurrentDateTime() {
    3. const now = new Date();
    4. const year = now.getFullYear();
    5. const month = String(now.getMonth() + 1).padStart(2, "0");
    6. const day = String(now.getDate()).padStart(2, "0");
    7. const hours = String(now.getHours()).padStart(2, "0");
    8. const minutes = String(now.getMinutes()).padStart(2, "0");
    9. const seconds = String(now.getSeconds()).padStart(2, "0");
    10. const dateTime = `${year}-${month}-${day}`;
    11. return dateTime;
    12. }
    13. const currentDateTime = getCurrentDateTime();
    14. let setWatermark = (text, sourceBody, isShowTime) => {
    15. if (isShowTime) {
    16. let time = getCurrentDateTime();
    17. text = `${text}\n${time}`;
    18. }
    19. // console.log("水印文本", text);
    20. let id =
    21. Math.random() * 10000 +
    22. "-" +
    23. Math.random() * 10000 +
    24. "/" +
    25. Math.random() * 10000;
    26. if (document.getElementById(id) !== null) {
    27. document.body.removeChild(document.getElementById(id));
    28. }
    29. let can = document.createElement("canvas");
    30. can.width = 180;
    31. can.height = 75;
    32. let cans = can.getContext("2d");
    33. cans.rotate((-20 * Math.PI) / 180);
    34. cans.font = "15px Vedana";
    35. cans.fillStyle = "rgba(0, 0, 0, .5)";
    36. cans.textAlign = "left";
    37. cans.textBaseline = "Middle";
    38. // let textLines = text.split("\n");
    39. // textLines.forEach((line, index) => {
    40. // cans.fillText(line, can.width / 20, can.height * (index + 1)); // 调整行高
    41. // });
    42. cans.fillText(text, can.width / 20, can.height);
    43. let water_div = document.createElement("div");
    44. water_div.id = id;
    45. water_div.style.pointerEvents = "none";
    46. water_div.style.background =
    47. "url(" + can.toDataURL("image/png") + ") left top repeat";
    48. water_div.style.zIndex = "100000";
    49. water_div.style.whiteSpace = "pre";
    50. water_div.style.opacity = "0.5";
    51. if (sourceBody) {
    52. water_div.style.width = "100%";
    53. water_div.style.height = "100%";
    54. water_div.style.position = "absolute";
    55. water_div.style.top = "3px";
    56. water_div.style.left = "0px";
    57. sourceBody.appendChild(water_div);
    58. } else {
    59. water_div.style.top = "3px";
    60. water_div.style.left = "0px";
    61. water_div.style.position = "fixed";
    62. water_div.style.width = document.documentElement.clientWidth + "px";
    63. water_div.style.height = document.documentElement.clientHeight + "px";
    64. document.body.appendChild(water_div);
    65. }
    66. return id;
    67. };
    68. /**
    69. * 该方法只允许调用一次
    70. * @param:
    71. * @text == 水印内容
    72. * @sourceBody == 水印添加在哪里,不传就是body
    73. * @isShowTime == 是否显示时间
    74. * */
    75. watermark.set = (text, sourceBody, isShowTime) => {
    76. let id = setWatermark(text, sourceBody, isShowTime);
    77. setInterval(() => {
    78. if (document.getElementById(id) === null) {
    79. id = setWatermark(text, sourceBody, isShowTime);
    80. }
    81. }, 1000);
    82. window.onresize = () => {
    83. setWatermark(text, sourceBody, isShowTime);
    84. };
    85. };
    86. export default watermark;

    2、在main.js下进行全局注册

    1. import watermark from '@/utils/waterMark.js'
    2. Vue.prototype.$watermark = watermark

    3、在vue组件中使用

    1. <el-dialog :visible.sync="openFile">
    2. <div id="fileDialog" ref="fileDialog" style="height:100%;width:100%;">div>
    3. el-dialog>
    1. handleOpenDialog(){
    2. this.openFile = true; //打开对话框
    3. this.$nextTick(()=>{
    4. let nickName = "admin";
    5. // 参数 - 水印内容, 加水印的容器, 是否显示时间
    6. this.$watermark.set(nickName, this.$refs.fileDialog, true);
    7. })
    8. }

    4、效果

  • 相关阅读:
    Android Studio 中将自己写的工程作为第三方类库的步骤
    冒泡排序(Bubble Sort)
    trick3-关于目标检测算法好坏的一些衡量指标
    【JVM】双亲委派模型
    C++ 多线程学习笔记
    kubernetes集群编排(8)
    【StableDiffusion】2024.6.4 亲测成功,无魔法 Civitai 镜像,国内下载 Civitai 模型的方法
    |行业洞察·设计|《2024年设计行业AI应用调研报告-D5渲染器&青年建筑》
    C++由于错误使用下标运算符引发的未定义错误
    CSS 笔记(十三):常用单位 & 适配方案(移动端)
  • 原文地址:https://blog.csdn.net/RumbleWx/article/details/134333167