@twick/canvas 0.14.7 → 0.14.8

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 } from "fabric";
1
+ import { Canvas, Control, controlsUtils, FabricImage, Rect, FabricText, Shadow, Group, Circle } from "fabric";
2
2
  import { useState, useRef } from "react";
3
3
  const DEFAULT_TEXT_PROPS = {
4
4
  /** Font family for text elements */
@@ -44,7 +44,9 @@ const CANVAS_OPERATIONS = {
44
44
  /** Items have been grouped together */
45
45
  ITEM_GROUPED: "ITEM_GROUPED",
46
46
  /** Items have been ungrouped */
47
- ITEM_UNGROUPED: "ITEM_UNGROUPED"
47
+ ITEM_UNGROUPED: "ITEM_UNGROUPED",
48
+ /** Caption properties have been updated */
49
+ CAPTION_PROPS_UPDATED: "CAPTION_PROPS_UPDATED"
48
50
  };
49
51
  const ELEMENT_TYPES = {
50
52
  /** Text element type */
@@ -56,7 +58,9 @@ const ELEMENT_TYPES = {
56
58
  /** Video element type */
57
59
  VIDEO: "video",
58
60
  /** Rectangle element type */
59
- RECT: "rect"
61
+ RECT: "rect",
62
+ /** Circle element type */
63
+ CIRCLE: "circle"
60
64
  };
61
65
  const isBrowser = typeof window !== "undefined";
62
66
  const isCanvasSupported = isBrowser && !!window.HTMLCanvasElement;
@@ -416,33 +420,33 @@ const addCaptionElement = ({
416
420
  captionProps,
417
421
  canvasMetadata
418
422
  }) => {
419
- 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, _z, _A;
423
+ 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, _z, _A, _B, _C, _D, _E, _F, _G;
420
424
  const { x, y } = convertToCanvasPosition(
421
- ((_b = (_a = element.props) == null ? void 0 : _a.pos) == null ? void 0 : _b.x) || ((_c = captionProps == null ? void 0 : captionProps.pos) == null ? void 0 : _c.x) || 0,
422
- ((_e = (_d = element.props) == null ? void 0 : _d.pos) == null ? void 0 : _e.y) || ((_f = captionProps == null ? void 0 : captionProps.pos) == null ? void 0 : _f.y) || 0,
425
+ ((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.x : (_a = element.props) == null ? void 0 : _a.x) ?? 0,
426
+ ((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.y : (_b = element.props) == null ? void 0 : _b.y) ?? 0,
423
427
  canvasMetadata
424
428
  );
425
- const caption = new FabricText(((_g = element.props) == null ? void 0 : _g.text) || element.t || "", {
429
+ const caption = new FabricText(((_c = element.props) == null ? void 0 : _c.text) || element.t || "", {
426
430
  left: x,
427
431
  top: y,
428
432
  originX: "center",
429
433
  originY: "center",
430
- angle: ((_h = element.props) == null ? void 0 : _h.rotation) || 0,
434
+ angle: ((_d = element.props) == null ? void 0 : _d.rotation) || 0,
431
435
  fontSize: Math.round(
432
- (((_j = (_i = element.props) == null ? void 0 : _i.font) == null ? void 0 : _j.size) || ((_k = captionProps.font) == null ? void 0 : _k.size) || DEFAULT_CAPTION_PROPS.size) * canvasMetadata.scaleX
436
+ (((captionProps == null ? void 0 : captionProps.applyToAll) ? (_e = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _e.size : ((_g = (_f = element.props) == null ? void 0 : _f.font) == null ? void 0 : _g.size) ?? ((_h = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _h.size)) ?? DEFAULT_CAPTION_PROPS.size) * canvasMetadata.scaleX
433
437
  ),
434
- fontFamily: ((_m = (_l = element.props) == null ? void 0 : _l.font) == null ? void 0 : _m.family) || ((_n = captionProps.font) == null ? void 0 : _n.family) || DEFAULT_CAPTION_PROPS.family,
435
- fill: ((_o = element.props) == null ? void 0 : _o.fill) || ((_p = captionProps.color) == null ? void 0 : _p.text) || DEFAULT_CAPTION_PROPS.fill,
436
- fontWeight: DEFAULT_CAPTION_PROPS.fontWeight,
437
- stroke: ((_q = element.props) == null ? void 0 : _q.stroke) || DEFAULT_CAPTION_PROPS.stroke,
438
- opacity: ((_r = element.props) == null ? void 0 : _r.opacity) ?? 1,
438
+ fontFamily: ((captionProps == null ? void 0 : captionProps.applyToAll) ? (_i = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _i.family : ((_k = (_j = element.props) == null ? void 0 : _j.font) == null ? void 0 : _k.family) ?? ((_l = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _l.family)) ?? DEFAULT_CAPTION_PROPS.family,
439
+ fill: ((captionProps == null ? void 0 : captionProps.applyToAll) ? (_m = captionProps.color) == null ? void 0 : _m.text : ((_n = element.props) == null ? void 0 : _n.fill) ?? ((_o = captionProps.color) == null ? void 0 : _o.text)) ?? DEFAULT_CAPTION_PROPS.fill,
440
+ fontWeight: ((captionProps == null ? void 0 : captionProps.applyToAll) ? (_p = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _p.weight : ((_q = element.props) == null ? void 0 : _q.fontWeight) ?? ((_r = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _r.weight)) ?? DEFAULT_CAPTION_PROPS.fontWeight,
441
+ stroke: ((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.stroke : ((_s = element.props) == null ? void 0 : _s.stroke) ?? (captionProps == null ? void 0 : captionProps.stroke)) ?? DEFAULT_CAPTION_PROPS.stroke,
442
+ opacity: ((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.opacity : ((_t = element.props) == null ? void 0 : _t.opacity) ?? (captionProps == null ? void 0 : captionProps.opacity)) ?? 1,
439
443
  shadow: new Shadow({
440
- offsetX: ((_t = (_s = element.props) == null ? void 0 : _s.shadowOffset) == null ? void 0 : _t[0]) || ((_u = DEFAULT_CAPTION_PROPS.shadowOffset) == null ? void 0 : _u[0]),
441
- offsetY: ((_w = (_v = element.props) == null ? void 0 : _v.shadowOffset) == null ? void 0 : _w[1]) || ((_x = DEFAULT_CAPTION_PROPS.shadowOffset) == null ? void 0 : _x[1]),
442
- blur: ((_y = element.props) == null ? void 0 : _y.shadowBlur) || DEFAULT_CAPTION_PROPS.shadowBlur,
443
- color: ((_z = element.props) == null ? void 0 : _z.shadowColor) || DEFAULT_CAPTION_PROPS.shadowColor
444
+ offsetX: ((captionProps == null ? void 0 : captionProps.applyToAll) ? (_u = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _u[0] : ((_w = (_v = element.props) == null ? void 0 : _v.shadowOffset) == null ? void 0 : _w[0]) ?? ((_x = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _x[0])) ?? ((_y = DEFAULT_CAPTION_PROPS.shadowOffset) == null ? void 0 : _y[0]),
445
+ offsetY: ((captionProps == null ? void 0 : captionProps.applyToAll) ? (_z = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _z[1] : ((_B = (_A = element.props) == null ? void 0 : _A.shadowOffset) == null ? void 0 : _B[1]) ?? ((_C = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _C[1])) ?? ((_D = DEFAULT_CAPTION_PROPS.shadowOffset) == null ? void 0 : _D[1]),
446
+ blur: ((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.shadowBlur : ((_E = element.props) == null ? void 0 : _E.shadowBlur) ?? (captionProps == null ? void 0 : captionProps.shadowBlur)) ?? DEFAULT_CAPTION_PROPS.shadowBlur,
447
+ color: ((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.shadowColor : ((_F = element.props) == null ? void 0 : _F.shadowColor) ?? (captionProps == null ? void 0 : captionProps.shadowColor)) ?? DEFAULT_CAPTION_PROPS.shadowColor
444
448
  }),
445
- strokeWidth: ((_A = element.props) == null ? void 0 : _A.lineWidth) || DEFAULT_CAPTION_PROPS.lineWidth
449
+ strokeWidth: ((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.lineWidth : ((_G = element.props) == null ? void 0 : _G.lineWidth) ?? (captionProps == null ? void 0 : captionProps.lineWidth)) ?? DEFAULT_CAPTION_PROPS.lineWidth
446
450
  });
447
451
  caption.set("id", element.id);
448
452
  caption.set("zIndex", index);
@@ -669,6 +673,40 @@ const addRectElement = ({
669
673
  canvas.add(rect);
670
674
  return rect;
671
675
  };
676
+ const addCircleElement = ({
677
+ element,
678
+ index,
679
+ canvas,
680
+ canvasMetadata
681
+ }) => {
682
+ var _a, _b, _c, _d, _e, _f;
683
+ const { x, y } = convertToCanvasPosition(
684
+ ((_a = element.props) == null ? void 0 : _a.x) || 0,
685
+ ((_b = element.props) == null ? void 0 : _b.y) || 0,
686
+ canvasMetadata
687
+ );
688
+ const circle = new Circle({
689
+ left: x,
690
+ // X-coordinate on the canvas
691
+ top: y,
692
+ // Y-coordinate on the canvas
693
+ radius: (((_c = element.props) == null ? void 0 : _c.radius) || 0) * canvasMetadata.scaleX,
694
+ fill: ((_d = element.props) == null ? void 0 : _d.fill) || "#000000",
695
+ stroke: ((_e = element.props) == null ? void 0 : _e.stroke) || "#000000",
696
+ strokeWidth: (((_f = element.props) == null ? void 0 : _f.lineWidth) || 0) * canvasMetadata.scaleX,
697
+ originX: "center",
698
+ originY: "center"
699
+ });
700
+ circle.controls.mt = disabledControl;
701
+ circle.controls.mb = disabledControl;
702
+ circle.controls.ml = disabledControl;
703
+ circle.controls.mr = disabledControl;
704
+ circle.controls.mtr = disabledControl;
705
+ circle.set("id", element.id);
706
+ circle.set("zIndex", index);
707
+ canvas.add(circle);
708
+ return circle;
709
+ };
672
710
  const addBackgroundColor = ({
673
711
  element,
674
712
  index,
@@ -710,6 +748,7 @@ const useTwickCanvas = ({
710
748
  const twickCanvasRef = useRef(null);
711
749
  const videoSizeRef = useRef({ width: 1, height: 1 });
712
750
  const canvasResolutionRef = useRef({ width: 1, height: 1 });
751
+ const captionPropsRef = useRef(null);
713
752
  const canvasMetadataRef = useRef({
714
753
  width: 0,
715
754
  height: 0,
@@ -767,7 +806,7 @@ const useTwickCanvas = ({
767
806
  }
768
807
  };
769
808
  const handleMouseUp = (event) => {
770
- var _a, _b;
809
+ var _a, _b, _c;
771
810
  if (event.target) {
772
811
  const object = event.target;
773
812
  const elementId = object.get("id");
@@ -794,20 +833,29 @@ const useTwickCanvas = ({
794
833
  videoSizeRef.current
795
834
  );
796
835
  if (elementMap.current[elementId].type === "caption") {
797
- elementMap.current[elementId] = {
798
- ...elementMap.current[elementId],
799
- props: {
800
- ...elementMap.current[elementId].props,
801
- pos: {
836
+ if ((_c = captionPropsRef.current) == null ? void 0 : _c.applyToAll) {
837
+ onCanvasOperation == null ? void 0 : onCanvasOperation(CANVAS_OPERATIONS.CAPTION_PROPS_UPDATED, {
838
+ element: elementMap.current[elementId],
839
+ props: {
840
+ ...captionPropsRef.current,
802
841
  x,
803
842
  y
804
843
  }
805
- }
806
- };
807
- onCanvasOperation == null ? void 0 : onCanvasOperation(
808
- CANVAS_OPERATIONS.ITEM_UPDATED,
809
- elementMap.current[elementId]
810
- );
844
+ });
845
+ } else {
846
+ elementMap.current[elementId] = {
847
+ ...elementMap.current[elementId],
848
+ props: {
849
+ ...elementMap.current[elementId].props,
850
+ x,
851
+ y
852
+ }
853
+ };
854
+ onCanvasOperation == null ? void 0 : onCanvasOperation(
855
+ CANVAS_OPERATIONS.ITEM_UPDATED,
856
+ elementMap.current[elementId]
857
+ );
858
+ }
811
859
  } else {
812
860
  if ((object == null ? void 0 : object.type) === "group") {
813
861
  const currentFrameEffect = elementFrameMap.current[elementId];
@@ -871,6 +919,22 @@ const useTwickCanvas = ({
871
919
  y
872
920
  }
873
921
  };
922
+ } else if ((object == null ? void 0 : object.type) === "circle") {
923
+ const radius = Number(
924
+ (elementMap.current[elementId].props.radius * object.scaleX).toFixed(2)
925
+ );
926
+ elementMap.current[elementId] = {
927
+ ...elementMap.current[elementId],
928
+ props: {
929
+ ...elementMap.current[elementId].props,
930
+ rotation: object.angle,
931
+ radius,
932
+ height: radius * 2,
933
+ width: radius * 2,
934
+ x,
935
+ y
936
+ }
937
+ };
874
938
  } else {
875
939
  elementMap.current[elementId] = {
876
940
  ...elementMap.current[elementId],
@@ -913,6 +977,7 @@ const useTwickCanvas = ({
913
977
  twickCanvas.renderAll();
914
978
  }
915
979
  }
980
+ captionPropsRef.current = captionProps;
916
981
  await Promise.all(
917
982
  elements.map(async (element, index) => {
918
983
  try {
@@ -951,7 +1016,10 @@ const useTwickCanvas = ({
951
1016
  }
952
1017
  switch (element.type) {
953
1018
  case ELEMENT_TYPES.VIDEO:
954
- const currentFrameEffect = getCurrentFrameEffect(element, seekTime || 0);
1019
+ const currentFrameEffect = getCurrentFrameEffect(
1020
+ element,
1021
+ seekTime || 0
1022
+ );
955
1023
  elementFrameMap.current[element.id] = currentFrameEffect;
956
1024
  const snapTime = ((seekTime || 0) - ((element == null ? void 0 : element.s) || 0)) * (((_a = element == null ? void 0 : element.props) == null ? void 0 : _a.playbackRate) || 1) + (((_b = element == null ? void 0 : element.props) == null ? void 0 : _b.time) || 0);
957
1025
  await addVideoElement({
@@ -995,6 +1063,14 @@ const useTwickCanvas = ({
995
1063
  canvasMetadata: canvasMetadataRef.current
996
1064
  });
997
1065
  break;
1066
+ case ELEMENT_TYPES.CIRCLE:
1067
+ await addCircleElement({
1068
+ element,
1069
+ index,
1070
+ canvas: twickCanvas,
1071
+ canvasMetadata: canvasMetadataRef.current
1072
+ });
1073
+ break;
998
1074
  case ELEMENT_TYPES.TEXT:
999
1075
  await addTextElement({
1000
1076
  element,