utils.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPixelRatio = getPixelRatio;
  6. exports.getStyleStr = getStyleStr;
  7. exports.reRendering = void 0;
  8. exports.rotateWatermark = rotateWatermark;
  9. exports.toLowercaseSeparator = toLowercaseSeparator;
  10. /** converting camel-cased strings to be lowercase and link it with Separato */
  11. function toLowercaseSeparator(key) {
  12. return key.replace(/([A-Z])/g, '-$1').toLowerCase();
  13. }
  14. function getStyleStr(style) {
  15. return Object.keys(style).map(key => `${toLowercaseSeparator(key)}: ${style[key]};`).join(' ');
  16. }
  17. /** Returns the ratio of the device's physical pixel resolution to the css pixel resolution */
  18. function getPixelRatio() {
  19. return window.devicePixelRatio || 1;
  20. }
  21. /** Rotate with the watermark as the center point */
  22. function rotateWatermark(ctx, rotateX, rotateY, rotate) {
  23. ctx.translate(rotateX, rotateY);
  24. ctx.rotate(Math.PI / 180 * Number(rotate));
  25. ctx.translate(-rotateX, -rotateY);
  26. }
  27. /** Whether to re-render the watermark */
  28. const reRendering = (mutation, watermarkElement) => {
  29. let flag = false;
  30. // Whether to delete the watermark node
  31. if (mutation.removedNodes.length) {
  32. flag = Array.from(mutation.removedNodes).some(node => node === watermarkElement);
  33. }
  34. // Whether the watermark dom property value has been modified
  35. if (mutation.type === 'attributes' && mutation.target === watermarkElement) {
  36. flag = true;
  37. }
  38. return flag;
  39. };
  40. exports.reRendering = reRendering;