Watermark.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. var __defProp = Object.defineProperty;
  2. var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
  3. var __getOwnPropNames = Object.getOwnPropertyNames;
  4. var __hasOwnProp = Object.prototype.hasOwnProperty;
  5. var __export = (target, all) => {
  6. for (var name2 in all)
  7. __defProp(target, name2, { get: all[name2], enumerable: true });
  8. };
  9. var __copyProps = (to, from, except, desc) => {
  10. if (from && typeof from === "object" || typeof from === "function") {
  11. for (let key of __getOwnPropNames(from))
  12. if (!__hasOwnProp.call(to, key) && key !== except)
  13. __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  14. }
  15. return to;
  16. };
  17. var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
  18. var stdin_exports = {};
  19. __export(stdin_exports, {
  20. default: () => stdin_default,
  21. watermarkProps: () => watermarkProps
  22. });
  23. module.exports = __toCommonJS(stdin_exports);
  24. var import_vue = require("vue");
  25. var import_utils = require("../utils");
  26. const [name, bem] = (0, import_utils.createNamespace)("watermark");
  27. const watermarkProps = {
  28. gapX: (0, import_utils.makeNumberProp)(0),
  29. gapY: (0, import_utils.makeNumberProp)(0),
  30. image: String,
  31. width: (0, import_utils.makeNumberProp)(100),
  32. height: (0, import_utils.makeNumberProp)(100),
  33. rotate: (0, import_utils.makeNumericProp)(-22),
  34. zIndex: import_utils.numericProp,
  35. content: String,
  36. opacity: import_utils.numericProp,
  37. fullPage: import_utils.truthProp,
  38. textColor: (0, import_utils.makeStringProp)("#dcdee0")
  39. };
  40. var stdin_default = (0, import_vue.defineComponent)({
  41. name,
  42. props: watermarkProps,
  43. setup(props, {
  44. slots
  45. }) {
  46. const svgElRef = (0, import_vue.ref)();
  47. const watermarkUrl = (0, import_vue.ref)("");
  48. const imageBase64 = (0, import_vue.ref)("");
  49. const renderWatermark = () => {
  50. const rotateStyle = {
  51. transformOrigin: "center",
  52. transform: `rotate(${props.rotate}deg)`
  53. };
  54. const svgInner = () => {
  55. if (props.image && !slots.content) {
  56. return (0, import_vue.createVNode)("image", {
  57. "href": imageBase64.value,
  58. "xlink:href": imageBase64.value,
  59. "x": "0",
  60. "y": "0",
  61. "width": props.width,
  62. "height": props.height,
  63. "style": rotateStyle
  64. }, null);
  65. }
  66. return (0, import_vue.createVNode)("foreignObject", {
  67. "x": "0",
  68. "y": "0",
  69. "width": props.width,
  70. "height": props.height
  71. }, [(0, import_vue.createVNode)("div", {
  72. "xmlns": "http://www.w3.org/1999/xhtml",
  73. "style": rotateStyle
  74. }, [slots.content ? slots.content() : (0, import_vue.createVNode)("span", {
  75. "style": {
  76. color: props.textColor
  77. }
  78. }, [props.content])])]);
  79. };
  80. const svgWidth = props.width + props.gapX;
  81. const svgHeight = props.height + props.gapY;
  82. return (0, import_vue.createVNode)("svg", {
  83. "viewBox": `0 0 ${svgWidth} ${svgHeight}`,
  84. "width": svgWidth,
  85. "height": svgHeight,
  86. "xmlns": "http://www.w3.org/2000/svg",
  87. "xmlns:xlink": "http://www.w3.org/1999/xlink",
  88. "style": {
  89. padding: `0 ${props.gapX}px ${props.gapY}px 0`,
  90. opacity: props.opacity
  91. }
  92. }, [svgInner()]);
  93. };
  94. const makeImageToBase64 = (url) => {
  95. const canvas = document.createElement("canvas");
  96. const image = new Image();
  97. image.crossOrigin = "anonymous";
  98. image.referrerPolicy = "no-referrer";
  99. image.onload = () => {
  100. canvas.width = image.naturalWidth;
  101. canvas.height = image.naturalHeight;
  102. const ctx = canvas.getContext("2d");
  103. ctx == null ? void 0 : ctx.drawImage(image, 0, 0);
  104. imageBase64.value = canvas.toDataURL();
  105. };
  106. image.src = url;
  107. };
  108. const makeSvgToBlobUrl = (svgStr) => {
  109. const svgBlob = new Blob([svgStr], {
  110. type: "image/svg+xml"
  111. });
  112. return URL.createObjectURL(svgBlob);
  113. };
  114. (0, import_vue.watchEffect)(() => {
  115. if (props.image) {
  116. makeImageToBase64(props.image);
  117. }
  118. });
  119. (0, import_vue.watch)(() => [imageBase64.value, props.content, props.textColor, props.height, props.width, props.rotate, props.gapX, props.gapY], () => {
  120. (0, import_vue.nextTick)(() => {
  121. if (svgElRef.value) {
  122. if (watermarkUrl.value) {
  123. URL.revokeObjectURL(watermarkUrl.value);
  124. }
  125. watermarkUrl.value = makeSvgToBlobUrl(svgElRef.value.innerHTML);
  126. }
  127. });
  128. }, {
  129. immediate: true
  130. });
  131. (0, import_vue.onUnmounted)(() => {
  132. if (watermarkUrl.value) {
  133. URL.revokeObjectURL(watermarkUrl.value);
  134. }
  135. });
  136. return () => {
  137. const style = (0, import_utils.extend)({
  138. backgroundImage: `url(${watermarkUrl.value})`
  139. }, (0, import_utils.getZIndexStyle)(props.zIndex));
  140. return (0, import_vue.createVNode)("div", {
  141. "class": bem({
  142. full: props.fullPage
  143. }),
  144. "style": style
  145. }, [(0, import_vue.createVNode)("div", {
  146. "class": bem("wrapper"),
  147. "ref": svgElRef
  148. }, [renderWatermark()])]);
  149. };
  150. }
  151. });