useClips.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var shared = require('@vue/shared');
  4. const FontGap = 3;
  5. function prepareCanvas(width, height, ratio = 1) {
  6. const canvas = document.createElement("canvas");
  7. const ctx = canvas.getContext("2d");
  8. const realWidth = width * ratio;
  9. const realHeight = height * ratio;
  10. canvas.setAttribute("width", `${realWidth}px`);
  11. canvas.setAttribute("height", `${realHeight}px`);
  12. ctx.save();
  13. return [ctx, canvas, realWidth, realHeight];
  14. }
  15. function useClips() {
  16. function getClips(content, rotate, ratio, width, height, font, gapX, gapY) {
  17. const [ctx, canvas, contentWidth, contentHeight] = prepareCanvas(width, height, ratio);
  18. if (content instanceof HTMLImageElement) {
  19. ctx.drawImage(content, 0, 0, contentWidth, contentHeight);
  20. } else {
  21. const {
  22. color,
  23. fontSize,
  24. fontStyle,
  25. fontWeight,
  26. fontFamily,
  27. textAlign,
  28. textBaseline
  29. } = font;
  30. const mergedFontSize = Number(fontSize) * ratio;
  31. ctx.font = `${fontStyle} normal ${fontWeight} ${mergedFontSize}px/${height}px ${fontFamily}`;
  32. ctx.fillStyle = color;
  33. ctx.textAlign = textAlign;
  34. ctx.textBaseline = textBaseline;
  35. const contents = shared.isArray(content) ? content : [content];
  36. contents == null ? void 0 : contents.forEach((item, index) => {
  37. ctx.fillText(item != null ? item : "", contentWidth / 2, index * (mergedFontSize + FontGap * ratio));
  38. });
  39. }
  40. const angle = Math.PI / 180 * Number(rotate);
  41. const maxSize = Math.max(width, height);
  42. const [rCtx, rCanvas, realMaxSize] = prepareCanvas(maxSize, maxSize, ratio);
  43. rCtx.translate(realMaxSize / 2, realMaxSize / 2);
  44. rCtx.rotate(angle);
  45. if (contentWidth > 0 && contentHeight > 0) {
  46. rCtx.drawImage(canvas, -contentWidth / 2, -contentHeight / 2);
  47. }
  48. function getRotatePos(x, y) {
  49. const targetX = x * Math.cos(angle) - y * Math.sin(angle);
  50. const targetY = x * Math.sin(angle) + y * Math.cos(angle);
  51. return [targetX, targetY];
  52. }
  53. let left = 0;
  54. let right = 0;
  55. let top = 0;
  56. let bottom = 0;
  57. const halfWidth = contentWidth / 2;
  58. const halfHeight = contentHeight / 2;
  59. const points = [
  60. [0 - halfWidth, 0 - halfHeight],
  61. [0 + halfWidth, 0 - halfHeight],
  62. [0 + halfWidth, 0 + halfHeight],
  63. [0 - halfWidth, 0 + halfHeight]
  64. ];
  65. points.forEach(([x, y]) => {
  66. const [targetX, targetY] = getRotatePos(x, y);
  67. left = Math.min(left, targetX);
  68. right = Math.max(right, targetX);
  69. top = Math.min(top, targetY);
  70. bottom = Math.max(bottom, targetY);
  71. });
  72. const cutLeft = left + realMaxSize / 2;
  73. const cutTop = top + realMaxSize / 2;
  74. const cutWidth = right - left;
  75. const cutHeight = bottom - top;
  76. const realGapX = gapX * ratio;
  77. const realGapY = gapY * ratio;
  78. const filledWidth = (cutWidth + realGapX) * 2;
  79. const filledHeight = cutHeight + realGapY;
  80. const [fCtx, fCanvas] = prepareCanvas(filledWidth, filledHeight);
  81. function drawImg(targetX = 0, targetY = 0) {
  82. fCtx.drawImage(rCanvas, cutLeft, cutTop, cutWidth, cutHeight, targetX, targetY, cutWidth, cutHeight);
  83. }
  84. drawImg();
  85. drawImg(cutWidth + realGapX, -cutHeight / 2 - realGapY / 2);
  86. drawImg(cutWidth + realGapX, +cutHeight / 2 + realGapY / 2);
  87. return [fCanvas.toDataURL(), filledWidth / ratio, filledHeight / ratio];
  88. }
  89. return getClips;
  90. }
  91. exports.FontGap = FontGap;
  92. exports["default"] = useClips;
  93. //# sourceMappingURL=useClips.js.map