useClips.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import React from 'react';
  2. import toList from '../_util/toList';
  3. export const FontGap = 3;
  4. const prepareCanvas = (width, height, ratio = 1) => {
  5. const canvas = document.createElement('canvas');
  6. const ctx = canvas.getContext('2d');
  7. const realWidth = width * ratio;
  8. const realHeight = height * ratio;
  9. canvas.setAttribute('width', `${realWidth}px`);
  10. canvas.setAttribute('height', `${realHeight}px`);
  11. ctx.save();
  12. return [ctx, canvas, realWidth, realHeight];
  13. };
  14. // Get boundary of rotated text
  15. const getRotatePos = (x, y, angle) => {
  16. const targetX = x * Math.cos(angle) - y * Math.sin(angle);
  17. const targetY = x * Math.sin(angle) + y * Math.cos(angle);
  18. return [targetX, targetY];
  19. };
  20. /**
  21. * Get the clips of text content.
  22. * This is a lazy hook function since SSR no need this
  23. */
  24. const useClips = () => {
  25. // Get single clips
  26. const getClips = (content, rotate, ratio, width, height, font, gapX, gapY) => {
  27. // ================= Text / Image =================
  28. const [ctx, canvas, contentWidth, contentHeight] = prepareCanvas(width, height, ratio);
  29. if (content instanceof HTMLImageElement) {
  30. // Image
  31. ctx.drawImage(content, 0, 0, contentWidth, contentHeight);
  32. } else {
  33. // Text
  34. const {
  35. color,
  36. fontSize,
  37. fontStyle,
  38. fontWeight,
  39. fontFamily,
  40. textAlign
  41. } = font;
  42. const mergedFontSize = Number(fontSize) * ratio;
  43. ctx.font = `${fontStyle} normal ${fontWeight} ${mergedFontSize}px/${height}px ${fontFamily}`;
  44. ctx.fillStyle = color;
  45. ctx.textAlign = textAlign;
  46. ctx.textBaseline = 'top';
  47. const contents = toList(content);
  48. contents === null || contents === void 0 ? void 0 : contents.forEach((item, index) => {
  49. ctx.fillText(item !== null && item !== void 0 ? item : '', contentWidth / 2, index * (mergedFontSize + FontGap * ratio));
  50. });
  51. }
  52. // ==================== Rotate ====================
  53. const angle = Math.PI / 180 * Number(rotate);
  54. const maxSize = Math.max(width, height);
  55. const [rCtx, rCanvas, realMaxSize] = prepareCanvas(maxSize, maxSize, ratio);
  56. // Copy from `ctx` and rotate
  57. rCtx.translate(realMaxSize / 2, realMaxSize / 2);
  58. rCtx.rotate(angle);
  59. if (contentWidth > 0 && contentHeight > 0) {
  60. rCtx.drawImage(canvas, -contentWidth / 2, -contentHeight / 2);
  61. }
  62. let left = 0;
  63. let right = 0;
  64. let top = 0;
  65. let bottom = 0;
  66. const halfWidth = contentWidth / 2;
  67. const halfHeight = contentHeight / 2;
  68. const points = [[0 - halfWidth, 0 - halfHeight], [0 + halfWidth, 0 - halfHeight], [0 + halfWidth, 0 + halfHeight], [0 - halfWidth, 0 + halfHeight]];
  69. points.forEach(([x, y]) => {
  70. const [targetX, targetY] = getRotatePos(x, y, angle);
  71. left = Math.min(left, targetX);
  72. right = Math.max(right, targetX);
  73. top = Math.min(top, targetY);
  74. bottom = Math.max(bottom, targetY);
  75. });
  76. const cutLeft = left + realMaxSize / 2;
  77. const cutTop = top + realMaxSize / 2;
  78. const cutWidth = right - left;
  79. const cutHeight = bottom - top;
  80. // ================ Fill Alternate ================
  81. const realGapX = gapX * ratio;
  82. const realGapY = gapY * ratio;
  83. const filledWidth = (cutWidth + realGapX) * 2;
  84. const filledHeight = cutHeight + realGapY;
  85. const [fCtx, fCanvas] = prepareCanvas(filledWidth, filledHeight);
  86. const drawImg = (targetX = 0, targetY = 0) => {
  87. fCtx.drawImage(rCanvas, cutLeft, cutTop, cutWidth, cutHeight, targetX, targetY, cutWidth, cutHeight);
  88. };
  89. drawImg();
  90. drawImg(cutWidth + realGapX, -cutHeight / 2 - realGapY / 2);
  91. drawImg(cutWidth + realGapX, +cutHeight / 2 + realGapY / 2);
  92. return [fCanvas.toDataURL(), filledWidth / ratio, filledHeight / ratio];
  93. };
  94. return React.useCallback(getClips, []);
  95. };
  96. export default useClips;