watermark.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import Ids from 'ids'
  2. class waterMark {
  3. constructor(opt = {}) {
  4. this.CONTAINERID = opt.id || new Ids([32, 36, 1]).next()
  5. this.drawCanvas = this.drawCanvas.bind(this)
  6. this.parentObserver = this.parentObserver.bind(this)
  7. this.isOberserve = false
  8. this.repaint(opt)
  9. this.parentObserver()
  10. }
  11. init(opt) {
  12. this.option = {
  13. text: opt.text || '深圳市金源信通科技有限公司',
  14. font: opt.font || '30px 黑体',
  15. canvasWidth: opt.canvasWidth || 500,
  16. canvasHeight: opt.canvasHeight || 200,
  17. textAlign: opt.textAlign || 'center',
  18. textStyle: opt.textStyle || 'rgba(100,100,100,0.15)',
  19. degree: opt.degree || -20
  20. }
  21. }
  22. drawCanvas() {
  23. this.isOberserve = true
  24. const divContainer = document.createElement('div')
  25. const canvas = document.createElement('canvas')
  26. const context = canvas.getContext('2d')
  27. divContainer.id = this.CONTAINERID
  28. canvas.width = this.option.canvasWidth
  29. canvas.height = this.option.canvasHeight
  30. context.font = this.option.font
  31. context.textAlign = this.option.textAlign
  32. context.fillStyle = this.option.textStyle
  33. context.translate(canvas.width / 2, canvas.height / 2)
  34. context.rotate(this.option.degree * Math.PI / 180)
  35. context.fillText(this.option.text, 0, 0)
  36. const backgroundUrl = canvas.toDataURL('image/png')
  37. this.styleStr = `
  38. position:fixed;
  39. top:0;
  40. left:0;
  41. width:100%;
  42. height:100%;
  43. z-index:9999;
  44. pointer-events:none;
  45. background-repeat:repeat;
  46. background-image:url('${backgroundUrl}')`
  47. divContainer.setAttribute('style', this.styleStr)
  48. document.body.appendChild(divContainer)
  49. this.wmObserver(divContainer)
  50. this.isOberserve = false
  51. }
  52. wmObserver(divContainer) {
  53. const wmConf = { attributes: true, childList: true, characterData: true }
  54. const wmObserver = new MutationObserver((mo) => {
  55. if (!this.isOberserve) {
  56. const _obj = mo[0].target
  57. _obj.setAttribute('style', this.styleStr)
  58. _obj.setAttribute('id', this.CONTAINERID)
  59. wmObserver.takeRecords()
  60. }
  61. })
  62. wmObserver.observe(divContainer, wmConf)
  63. }
  64. parentObserver() {
  65. const bodyObserver = new MutationObserver(() => {
  66. if (!this.isOberserve) {
  67. const __wm = document.querySelector(`#${this.CONTAINERID}`)
  68. if (!__wm) {
  69. this.drawCanvas()
  70. } else if (__wm.getAttribute('style') !== this.styleStr) {
  71. __wm.setAttribute('style', this.styleStr)
  72. }
  73. }
  74. })
  75. bodyObserver.observe(document.querySelector(`#${this.CONTAINERID}`).parentNode, { childList: true })
  76. }
  77. /**
  78. * 重画
  79. * @param {*} opt
  80. */
  81. repaint(opt = {}) {
  82. this.isOberserve = true
  83. this.init(opt)
  84. const _wm = document.querySelector(`#${this.CONTAINERID}`)
  85. if (_wm) {
  86. _wm.parentNode.removeChild(_wm)
  87. }
  88. this.drawCanvas()
  89. }
  90. }
  91. export default waterMark