@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/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { Canvas, Control, controlsUtils, FabricImage, Rect, FabricText, Shadow, Group, Circle } from "fabric";
1
+ import { Canvas, Control, controlsUtils, FabricImage, Rect, Textbox, Shadow, FabricText, Group, Circle } from "fabric";
2
2
  import { useState, useRef } from "react";
3
3
  const DEFAULT_TEXT_PROPS = {
4
4
  /** Font family for text elements */
@@ -91,8 +91,8 @@ const createCanvas = ({
91
91
  width: canvasSize.width,
92
92
  height: canvasSize.height,
93
93
  aspectRatio: canvasSize.width / canvasSize.height,
94
- scaleX: canvasSize.width / videoSize.width,
95
- scaleY: canvasSize.height / videoSize.height
94
+ scaleX: Number((canvasSize.width / videoSize.width).toFixed(2)),
95
+ scaleY: Number((canvasSize.height / videoSize.height).toFixed(2))
96
96
  };
97
97
  const canvas = new Canvas(canvasRef, {
98
98
  backgroundColor,
@@ -342,35 +342,40 @@ const addTextElement = ({
342
342
  canvas,
343
343
  canvasMetadata
344
344
  }) => {
345
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
345
+ 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;
346
346
  const { x, y } = convertToCanvasPosition(
347
347
  ((_a = element.props) == null ? void 0 : _a.x) || 0,
348
348
  ((_b = element.props) == null ? void 0 : _b.y) || 0,
349
349
  canvasMetadata
350
350
  );
351
- const text = new FabricText(((_c = element.props) == null ? void 0 : _c.text) || element.t || "", {
351
+ let width = ((_c = element.props) == null ? void 0 : _c.width) ? element.props.width * canvasMetadata.scaleX : canvasMetadata.width;
352
+ if ((_d = element.props) == null ? void 0 : _d.maxWidth) {
353
+ width = Math.min(width, element.props.maxWidth * canvasMetadata.scaleX);
354
+ }
355
+ const text = new Textbox(((_e = element.props) == null ? void 0 : _e.text) || element.t || "", {
352
356
  left: x,
353
357
  top: y,
354
358
  originX: "center",
355
359
  originY: "center",
356
- angle: ((_d = element.props) == null ? void 0 : _d.rotation) || 0,
357
- fontSize: Math.round(
358
- (((_e = element.props) == null ? void 0 : _e.fontSize) || DEFAULT_TEXT_PROPS.size) * canvasMetadata.scaleX
360
+ angle: ((_f = element.props) == null ? void 0 : _f.rotation) || 0,
361
+ fontSize: Math.floor(
362
+ (((_g = element.props) == null ? void 0 : _g.fontSize) || DEFAULT_TEXT_PROPS.size) * canvasMetadata.scaleX
359
363
  ),
360
- fontFamily: ((_f = element.props) == null ? void 0 : _f.fontFamily) || DEFAULT_TEXT_PROPS.family,
361
- fontStyle: ((_g = element.props) == null ? void 0 : _g.fontStyle) || "normal",
362
- fontWeight: ((_h = element.props) == null ? void 0 : _h.fontWeight) || "normal",
363
- fill: ((_i = element.props) == null ? void 0 : _i.fill) || DEFAULT_TEXT_PROPS.fill,
364
- opacity: ((_j = element.props) == null ? void 0 : _j.opacity) ?? 1,
365
- skipWrapping: false,
366
- textAlign: ((_k = element.props) == null ? void 0 : _k.textAlign) || "center",
367
- stroke: ((_l = element.props) == null ? void 0 : _l.stroke) || DEFAULT_TEXT_PROPS.stroke,
368
- strokeWidth: ((_m = element.props) == null ? void 0 : _m.lineWidth) || DEFAULT_TEXT_PROPS.lineWidth,
369
- shadow: ((_n = element.props) == null ? void 0 : _n.shadowColor) ? new Shadow({
370
- 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,
371
- 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,
372
- blur: (((_v = element.props) == null ? void 0 : _v.shadowBlur) || 2) / 2,
373
- color: (_w = element.props) == null ? void 0 : _w.shadowColor
364
+ fontFamily: ((_h = element.props) == null ? void 0 : _h.fontFamily) || DEFAULT_TEXT_PROPS.family,
365
+ fontStyle: ((_i = element.props) == null ? void 0 : _i.fontStyle) || "normal",
366
+ fontWeight: ((_j = element.props) == null ? void 0 : _j.fontWeight) || "normal",
367
+ fill: ((_k = element.props) == null ? void 0 : _k.fill) || DEFAULT_TEXT_PROPS.fill,
368
+ opacity: ((_l = element.props) == null ? void 0 : _l.opacity) ?? 1,
369
+ width,
370
+ splitByGrapheme: false,
371
+ textAlign: ((_m = element.props) == null ? void 0 : _m.textAlign) || "center",
372
+ stroke: ((_n = element.props) == null ? void 0 : _n.stroke) || DEFAULT_TEXT_PROPS.stroke,
373
+ strokeWidth: ((_o = element.props) == null ? void 0 : _o.lineWidth) || DEFAULT_TEXT_PROPS.lineWidth,
374
+ shadow: ((_p = element.props) == null ? void 0 : _p.shadowColor) ? new Shadow({
375
+ 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,
376
+ 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,
377
+ blur: (((_x = element.props) == null ? void 0 : _x.shadowBlur) || 2) / 2,
378
+ color: (_y = element.props) == null ? void 0 : _y.shadowColor
374
379
  }) : void 0
375
380
  });
376
381
  text.set("id", element.id);
@@ -782,6 +787,7 @@ const useTwickCanvas = ({
782
787
  if (twickCanvasRef.current) {
783
788
  console.log("Destroying twickCanvas");
784
789
  twickCanvasRef.current.off("mouse:up", handleMouseUp);
790
+ twickCanvasRef.current.off("text:editing:exited", onTextEdit);
785
791
  twickCanvasRef.current.dispose();
786
792
  }
787
793
  const { canvas, canvasMetadata } = createCanvas({
@@ -798,6 +804,7 @@ const useTwickCanvas = ({
798
804
  canvasMetadataRef.current = canvasMetadata;
799
805
  videoSizeRef.current = videoSize;
800
806
  canvas == null ? void 0 : canvas.on("mouse:up", handleMouseUp);
807
+ canvas == null ? void 0 : canvas.on("text:editing:exited", onTextEdit);
801
808
  canvasResolutionRef.current = canvasSize;
802
809
  setTwickCanvas(canvas);
803
810
  twickCanvasRef.current = canvas;
@@ -805,6 +812,23 @@ const useTwickCanvas = ({
805
812
  onCanvasReady(canvas);
806
813
  }
807
814
  };
815
+ const onTextEdit = (event) => {
816
+ if (event.target) {
817
+ const object = event.target;
818
+ const elementId = object.get("id");
819
+ elementMap.current[elementId] = {
820
+ ...elementMap.current[elementId],
821
+ props: {
822
+ ...elementMap.current[elementId].props,
823
+ text: object.text ?? elementMap.current[elementId].props.text
824
+ }
825
+ };
826
+ onCanvasOperation == null ? void 0 : onCanvasOperation(
827
+ CANVAS_OPERATIONS.ITEM_UPDATED,
828
+ elementMap.current[elementId]
829
+ );
830
+ }
831
+ };
808
832
  const handleMouseUp = (event) => {
809
833
  var _a, _b, _c;
810
834
  if (event.target) {