waterCom.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <div id="water">
  3. <canvas id="canvas"></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. import Router from "vue-router";
  8. import { mapGetters } from "vuex";
  9. export default {
  10. name: "SocAppWaterCom",
  11. data() {
  12. return {
  13. // userName: this.$store.getters.name,
  14. // orgName: "",
  15. date: "",
  16. };
  17. },
  18. computed: {
  19. userName() {
  20. return this.$store.getters.name;
  21. },
  22. },
  23. watch: {
  24. userName(val) {
  25. if (val) {
  26. this.getWater();
  27. }
  28. },
  29. },
  30. created() {
  31. this.date = this.newDateDay();
  32. console.log(this.$store.getters.name);
  33. // this.name = JSON.parse(window.sessionStorage.getItem("SET_USER_ORGNAME"));
  34. // this.userName = JSON.parse(window.sessionStorage.getItem("SET_USER_NAME"));
  35. },
  36. mounted() {},
  37. methods: {
  38. getWater() {
  39. // console.log(this);
  40. let text = this.userName;
  41. let orgName = this.$store.getters.orgShortName;
  42. const canvas = document.getElementById("canvas"); //获取canvas
  43. canvas.width = 600; //设置画布宽度
  44. canvas.height = 600; //设置画布高度
  45. canvas.style.display = "none"; //隐藏画布本身
  46. //加层级权重
  47. const ctx = canvas.getContext("2d"); //获取画笔
  48. ctx.font = "30px -apple-system";
  49. // 设置文字大小
  50. ctx.fillStyle = "rgba(0,0,0,.1)"; //设置文字颜色及透明度
  51. ctx.rotate(-0.4); //设置文字旋转角度
  52. ctx.fillText(`${text}${orgName}`, -20,180,); //设置显示文字内容
  53. const img = canvas.toDataURL("image/png"); //参数默认为 image/png,可以是其他image/jpeg等,该方法返回值是一个url,是base64组成的图片的源数据、可以直接赋值给图片的src属性
  54. const style = `background-image:url(${img});z-index:100000;display:none;`; //定义样式
  55. water.setAttribute("style", style); //给要添加水印的元素设置样式
  56. },
  57. newDateDay(time) {
  58. var date = time ? new Date(time) : new Date();
  59. var y = date.getFullYear();
  60. var m = date.getMonth() + 1;
  61. m = m < 10 ? "0" + m : m;
  62. var d = date.getDate();
  63. d = d < 10 ? "0" + d : d;
  64. return y + "-" + m + "-" + d;
  65. },
  66. },
  67. };
  68. </script>
  69. <style lang="scss" scoped>
  70. #water {
  71. width: 100%;
  72. height: 100%;
  73. position: absolute;
  74. left: 0;
  75. top: 0;
  76. // z-index: -1;
  77. pointer-events: none;
  78. }
  79. </style>