@twick/canvas 0.14.17 → 0.14.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -20,6 +20,8 @@ npm install @twick/canvas
20
20
  pnpm add @twick/canvas
21
21
  ```
22
22
 
23
+ **Note:** All required dependencies (`@twick/media-utils`) are automatically installed with `@twick/canvas`.
24
+
23
25
  ## Quick Start
24
26
 
25
27
  ### Basic Canvas Setup
package/dist/index.js CHANGED
@@ -93,8 +93,8 @@ const createCanvas = ({
93
93
  width: canvasSize.width,
94
94
  height: canvasSize.height,
95
95
  aspectRatio: canvasSize.width / canvasSize.height,
96
- scaleX: canvasSize.width / videoSize.width,
97
- scaleY: canvasSize.height / videoSize.height
96
+ scaleX: Number((canvasSize.width / videoSize.width).toFixed(2)),
97
+ scaleY: Number((canvasSize.height / videoSize.height).toFixed(2))
98
98
  };
99
99
  const canvas = new fabric.Canvas(canvasRef, {
100
100
  backgroundColor,
@@ -344,35 +344,40 @@ const addTextElement = ({
344
344
  canvas,
345
345
  canvasMetadata
346
346
  }) => {
347
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
347
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y;
348
348
  const { x, y } = convertToCanvasPosition(
349
349
  ((_a = element.props) == null ? void 0 : _a.x) || 0,
350
350
  ((_b = element.props) == null ? void 0 : _b.y) || 0,
351
351
  canvasMetadata
352
352
  );
353
- const text = new fabric.FabricText(((_c = element.props) == null ? void 0 : _c.text) || element.t || "", {
353
+ let width = ((_c = element.props) == null ? void 0 : _c.width) ? element.props.width * canvasMetadata.scaleX : canvasMetadata.width;
354
+ if ((_d = element.props) == null ? void 0 : _d.maxWidth) {
355
+ width = Math.min(width, element.props.maxWidth * canvasMetadata.scaleX);
356
+ }
357
+ const text = new fabric.Textbox(((_e = element.props) == null ? void 0 : _e.text) || element.t || "", {
354
358
  left: x,
355
359
  top: y,
356
360
  originX: "center",
357
361
  originY: "center",
358
- angle: ((_d = element.props) == null ? void 0 : _d.rotation) || 0,
359
- fontSize: Math.round(
360
- (((_e = element.props) == null ? void 0 : _e.fontSize) || DEFAULT_TEXT_PROPS.size) * canvasMetadata.scaleX
362
+ angle: ((_f = element.props) == null ? void 0 : _f.rotation) || 0,
363
+ fontSize: Math.floor(
364
+ (((_g = element.props) == null ? void 0 : _g.fontSize) || DEFAULT_TEXT_PROPS.size) * canvasMetadata.scaleX
361
365
  ),
362
- fontFamily: ((_f = element.props) == null ? void 0 : _f.fontFamily) || DEFAULT_TEXT_PROPS.family,
363
- fontStyle: ((_g = element.props) == null ? void 0 : _g.fontStyle) || "normal",
364
- fontWeight: ((_h = element.props) == null ? void 0 : _h.fontWeight) || "normal",
365
- fill: ((_i = element.props) == null ? void 0 : _i.fill) || DEFAULT_TEXT_PROPS.fill,
366
- opacity: ((_j = element.props) == null ? void 0 : _j.opacity) ?? 1,
367
- skipWrapping: false,
368
- textAlign: ((_k = element.props) == null ? void 0 : _k.textAlign) || "center",
369
- stroke: ((_l = element.props) == null ? void 0 : _l.stroke) || DEFAULT_TEXT_PROPS.stroke,
370
- strokeWidth: ((_m = element.props) == null ? void 0 : _m.lineWidth) || DEFAULT_TEXT_PROPS.lineWidth,
371
- shadow: ((_n = element.props) == null ? void 0 : _n.shadowColor) ? new fabric.Shadow({
372
- offsetX: ((_p = (_o = element.props) == null ? void 0 : _o.shadowOffset) == null ? void 0 : _p.length) && ((_r = (_q = element.props) == null ? void 0 : _q.shadowOffset) == null ? void 0 : _r.length) > 1 ? element.props.shadowOffset[0] / 2 : 1,
373
- offsetY: ((_t = (_s = element.props) == null ? void 0 : _s.shadowOffset) == null ? void 0 : _t.length) && ((_u = element.props) == null ? void 0 : _u.shadowOffset.length) > 1 ? element.props.shadowOffset[1] / 2 : 1,
374
- blur: (((_v = element.props) == null ? void 0 : _v.shadowBlur) || 2) / 2,
375
- color: (_w = element.props) == null ? void 0 : _w.shadowColor
366
+ fontFamily: ((_h = element.props) == null ? void 0 : _h.fontFamily) || DEFAULT_TEXT_PROPS.family,
367
+ fontStyle: ((_i = element.props) == null ? void 0 : _i.fontStyle) || "normal",
368
+ fontWeight: ((_j = element.props) == null ? void 0 : _j.fontWeight) || "normal",
369
+ fill: ((_k = element.props) == null ? void 0 : _k.fill) || DEFAULT_TEXT_PROPS.fill,
370
+ opacity: ((_l = element.props) == null ? void 0 : _l.opacity) ?? 1,
371
+ width,
372
+ splitByGrapheme: false,
373
+ textAlign: ((_m = element.props) == null ? void 0 : _m.textAlign) || "center",
374
+ stroke: ((_n = element.props) == null ? void 0 : _n.stroke) || DEFAULT_TEXT_PROPS.stroke,
375
+ strokeWidth: ((_o = element.props) == null ? void 0 : _o.lineWidth) || DEFAULT_TEXT_PROPS.lineWidth,
376
+ shadow: ((_p = element.props) == null ? void 0 : _p.shadowColor) ? new fabric.Shadow({
377
+ offsetX: ((_r = (_q = element.props) == null ? void 0 : _q.shadowOffset) == null ? void 0 : _r.length) && ((_t = (_s = element.props) == null ? void 0 : _s.shadowOffset) == null ? void 0 : _t.length) > 1 ? element.props.shadowOffset[0] / 2 : 1,
378
+ offsetY: ((_v = (_u = element.props) == null ? void 0 : _u.shadowOffset) == null ? void 0 : _v.length) && ((_w = element.props) == null ? void 0 : _w.shadowOffset.length) > 1 ? element.props.shadowOffset[1] / 2 : 1,
379
+ blur: (((_x = element.props) == null ? void 0 : _x.shadowBlur) || 2) / 2,
380
+ color: (_y = element.props) == null ? void 0 : _y.shadowColor
376
381
  }) : void 0
377
382
  });
378
383
  text.set("id", element.id);
@@ -784,6 +789,7 @@ const useTwickCanvas = ({
784
789
  if (twickCanvasRef.current) {
785
790
  console.log("Destroying twickCanvas");
786
791
  twickCanvasRef.current.off("mouse:up", handleMouseUp);
792
+ twickCanvasRef.current.off("text:editing:exited", onTextEdit);
787
793
  twickCanvasRef.current.dispose();
788
794
  }
789
795
  const { canvas, canvasMetadata } = createCanvas({
@@ -800,6 +806,7 @@ const useTwickCanvas = ({
800
806
  canvasMetadataRef.current = canvasMetadata;
801
807
  videoSizeRef.current = videoSize;
802
808
  canvas == null ? void 0 : canvas.on("mouse:up", handleMouseUp);
809
+ canvas == null ? void 0 : canvas.on("text:editing:exited", onTextEdit);
803
810
  canvasResolutionRef.current = canvasSize;
804
811
  setTwickCanvas(canvas);
805
812
  twickCanvasRef.current = canvas;
@@ -807,6 +814,23 @@ const useTwickCanvas = ({
807
814
  onCanvasReady(canvas);
808
815
  }
809
816
  };
817
+ const onTextEdit = (event) => {
818
+ if (event.target) {
819
+ const object = event.target;
820
+ const elementId = object.get("id");
821
+ elementMap.current[elementId] = {
822
+ ...elementMap.current[elementId],
823
+ props: {
824
+ ...elementMap.current[elementId].props,
825
+ text: object.text ?? elementMap.current[elementId].props.text
826
+ }
827
+ };
828
+ onCanvasOperation == null ? void 0 : onCanvasOperation(
829
+ CANVAS_OPERATIONS.ITEM_UPDATED,
830
+ elementMap.current[elementId]
831
+ );
832
+ }
833
+ };
810
834
  const handleMouseUp = (event) => {
811
835
  var _a, _b, _c;
812
836
  if (event.target) {