waterCom.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <template>
  2. <div id="water" >
  3. <canvas id="canvas"></canvas>
  4. </div>
  5. </template>
  6. <script>
  7. import { newDateDay } from '@/utils/date.js'
  8. export default {
  9. name: 'SocAppWaterCom',
  10. data() {
  11. return {
  12. name:'',
  13. userName:'',
  14. date:newDateDay()
  15. }
  16. },
  17. created(){
  18. this.name=JSON.parse(window.sessionStorage.getItem('SET_USER_ORGNAME'))
  19. this.userName=JSON.parse(window.sessionStorage.getItem('SET_USER_NAME'))
  20. },
  21. mounted() {
  22. this.getWater()
  23. },
  24. methods: {
  25. getWater(type=0) {
  26. let orgName=JSON.parse(sessionStorage.getItem('SET_USER_ORGNAME'))
  27. let userName=JSON.parse(sessionStorage.getItem('SET_USER_NAME'))
  28. const canvas = document.getElementById('canvas') //获取canvas
  29. canvas.width = 210 //设置画布宽度
  30. canvas.height = 150 //设置画布高度
  31. canvas.style.display = 'none' //隐藏画布本身
  32. //加层级权重
  33. const ctx = canvas.getContext('2d') //获取画笔
  34. ctx.font = '18px -apple-system'
  35. // 设置文字大小
  36. ctx.fillStyle = 'rgba(0,0,0,.1)' //设置文字颜色及透明度
  37. ctx.rotate(-0.4) //设置文字旋转角度
  38. ctx.fillText(`${userName||''}${orgName||''}${this.date}`, -10, 120) //设置显示文字内容
  39. const img = canvas.toDataURL('image/png') //参数默认为 image/png,可以是其他image/jpeg等,该方法返回值是一个url,是base64组成的图片的源数据、可以直接赋值给图片的src属性
  40. const style = `background-image:url(${img});width:1200px;z-index:111;display:${type==1? 'block':'none'};` //定义样式
  41. water.setAttribute('style', style) //给要添加水印的元素设置样式
  42. }
  43. }
  44. }
  45. </script>
  46. <style lang="scss" scoped>
  47. #water {
  48. width: 100%;
  49. height: 100%;
  50. position: absolute;
  51. left: 0;
  52. top: 0;
  53. // z-index: -1;
  54. pointer-events: none;
  55. }
  56. </style>