@twick/canvas 0.15.20 → 0.15.21

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.d.ts CHANGED
@@ -25,7 +25,9 @@ import { ELEMENT_TYPES } from './helpers/constants';
25
25
  import { ElementController } from './controllers/element.controller';
26
26
  import { default as elementController } from './controllers/element.controller';
27
27
  import { FrameEffect } from './types';
28
+ import { getCanvasHandler } from './controllers/element.controller';
28
29
  import { getCurrentFrameEffect } from './helpers/canvas.util';
30
+ import { registerCanvasHandler } from './controllers/element.controller';
29
31
  import { reorderElementsByZIndex } from './helpers/canvas.util';
30
32
  import { ResizeCanvasOptions } from './types';
31
33
  import { rotateControl } from './components/element-controls';
@@ -87,8 +89,12 @@ export { elementController }
87
89
 
88
90
  export { FrameEffect }
89
91
 
92
+ export { getCanvasHandler }
93
+
90
94
  export { getCurrentFrameEffect }
91
95
 
96
+ export { registerCanvasHandler }
97
+
92
98
  export { reorderElementsByZIndex }
93
99
 
94
100
  export { ResizeCanvasOptions }
package/dist/index.js CHANGED
@@ -74,8 +74,14 @@ const ELEMENT_TYPES = {
74
74
  CIRCLE: "circle",
75
75
  /** Icon element type */
76
76
  ICON: "icon",
77
+ /** Arrow annotation element type */
78
+ ARROW: "arrow",
79
+ /** Line annotation / shape element type */
80
+ LINE: "line",
77
81
  /** Background color element type */
78
- BACKGROUND_COLOR: "backgroundColor"
82
+ BACKGROUND_COLOR: "backgroundColor",
83
+ /** Global / adjustment-layer style effect element */
84
+ EFFECT: "effect"
79
85
  };
80
86
  const isBrowser = typeof window !== "undefined";
81
87
  const isCanvasSupported = isBrowser && !!window.HTMLCanvasElement;
@@ -819,7 +825,7 @@ const addCaptionElement = ({
819
825
  canvasMetadata,
820
826
  lockAspectRatio = false
821
827
  }) => {
822
- 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, _H, _I, _J, _K;
828
+ 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, _H, _I, _J, _K, _L;
823
829
  const applyToAll = (captionProps == null ? void 0 : captionProps.applyToAll) ?? false;
824
830
  const captionTextColor = ((_a = captionProps == null ? void 0 : captionProps.colors) == null ? void 0 : _a.text) ?? ((_b = captionProps == null ? void 0 : captionProps.color) == null ? void 0 : _b.text);
825
831
  const { x, y } = convertToCanvasPosition(
@@ -844,19 +850,19 @@ const addCaptionElement = ({
844
850
  ),
845
851
  fontFamily: (applyToAll ? (_o = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _o.family : ((_q = (_p = element.props) == null ? void 0 : _p.font) == null ? void 0 : _q.family) ?? ((_r = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _r.family)) ?? DEFAULT_CAPTION_PROPS.family,
846
852
  fill: resolvedFill,
847
- fontWeight: (applyToAll ? (_s = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _s.weight : ((_t = element.props) == null ? void 0 : _t.fontWeight) ?? ((_u = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _u.weight)) ?? DEFAULT_CAPTION_PROPS.fontWeight,
848
- stroke: (applyToAll ? captionProps == null ? void 0 : captionProps.stroke : ((_v = element.props) == null ? void 0 : _v.stroke) ?? (captionProps == null ? void 0 : captionProps.stroke)) ?? DEFAULT_CAPTION_PROPS.stroke,
849
- opacity: (applyToAll ? captionProps == null ? void 0 : captionProps.opacity : ((_w = element.props) == null ? void 0 : _w.opacity) ?? (captionProps == null ? void 0 : captionProps.opacity)) ?? 1,
853
+ fontWeight: (applyToAll ? (_s = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _s.weight : ((_u = (_t = element.props) == null ? void 0 : _t.font) == null ? void 0 : _u.weight) ?? ((_v = captionProps == null ? void 0 : captionProps.font) == null ? void 0 : _v.weight)) ?? DEFAULT_CAPTION_PROPS.fontWeight,
854
+ stroke: (applyToAll ? captionProps == null ? void 0 : captionProps.stroke : ((_w = element.props) == null ? void 0 : _w.stroke) ?? (captionProps == null ? void 0 : captionProps.stroke)) ?? DEFAULT_CAPTION_PROPS.stroke,
855
+ opacity: (applyToAll ? captionProps == null ? void 0 : captionProps.opacity : ((_x = element.props) == null ? void 0 : _x.opacity) ?? (captionProps == null ? void 0 : captionProps.opacity)) ?? 1,
850
856
  width,
851
857
  splitByGrapheme: false,
852
- textAlign: ((_x = element.props) == null ? void 0 : _x.textAlign) ?? "center",
858
+ textAlign: ((_y = element.props) == null ? void 0 : _y.textAlign) ?? "center",
853
859
  shadow: new fabric.Shadow({
854
- offsetX: (applyToAll ? (_y = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _y[0] : ((_A = (_z = element.props) == null ? void 0 : _z.shadowOffset) == null ? void 0 : _A[0]) ?? ((_B = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _B[0])) ?? ((_C = DEFAULT_CAPTION_PROPS.shadowOffset) == null ? void 0 : _C[0]),
855
- offsetY: (applyToAll ? (_D = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _D[1] : ((_F = (_E = element.props) == null ? void 0 : _E.shadowOffset) == null ? void 0 : _F[1]) ?? ((_G = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _G[1])) ?? ((_H = DEFAULT_CAPTION_PROPS.shadowOffset) == null ? void 0 : _H[1]),
856
- blur: (applyToAll ? captionProps == null ? void 0 : captionProps.shadowBlur : ((_I = element.props) == null ? void 0 : _I.shadowBlur) ?? (captionProps == null ? void 0 : captionProps.shadowBlur)) ?? DEFAULT_CAPTION_PROPS.shadowBlur,
857
- color: (applyToAll ? captionProps == null ? void 0 : captionProps.shadowColor : ((_J = element.props) == null ? void 0 : _J.shadowColor) ?? (captionProps == null ? void 0 : captionProps.shadowColor)) ?? DEFAULT_CAPTION_PROPS.shadowColor
860
+ offsetX: (applyToAll ? (_z = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _z[0] : ((_B = (_A = element.props) == null ? void 0 : _A.shadowOffset) == null ? void 0 : _B[0]) ?? ((_C = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _C[0])) ?? ((_D = DEFAULT_CAPTION_PROPS.shadowOffset) == null ? void 0 : _D[0]),
861
+ offsetY: (applyToAll ? (_E = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _E[1] : ((_G = (_F = element.props) == null ? void 0 : _F.shadowOffset) == null ? void 0 : _G[1]) ?? ((_H = captionProps == null ? void 0 : captionProps.shadowOffset) == null ? void 0 : _H[1])) ?? ((_I = DEFAULT_CAPTION_PROPS.shadowOffset) == null ? void 0 : _I[1]),
862
+ blur: (applyToAll ? captionProps == null ? void 0 : captionProps.shadowBlur : ((_J = element.props) == null ? void 0 : _J.shadowBlur) ?? (captionProps == null ? void 0 : captionProps.shadowBlur)) ?? DEFAULT_CAPTION_PROPS.shadowBlur,
863
+ color: (applyToAll ? captionProps == null ? void 0 : captionProps.shadowColor : ((_K = element.props) == null ? void 0 : _K.shadowColor) ?? (captionProps == null ? void 0 : captionProps.shadowColor)) ?? DEFAULT_CAPTION_PROPS.shadowColor
858
864
  }),
859
- strokeWidth: (applyToAll ? captionProps == null ? void 0 : captionProps.lineWidth : ((_K = element.props) == null ? void 0 : _K.lineWidth) ?? (captionProps == null ? void 0 : captionProps.lineWidth)) ?? DEFAULT_CAPTION_PROPS.lineWidth
865
+ strokeWidth: (applyToAll ? captionProps == null ? void 0 : captionProps.lineWidth : ((_L = element.props) == null ? void 0 : _L.lineWidth) ?? (captionProps == null ? void 0 : captionProps.lineWidth)) ?? DEFAULT_CAPTION_PROPS.lineWidth
860
866
  });
861
867
  caption.set("id", element.id);
862
868
  caption.set("zIndex", index);
@@ -1567,6 +1573,148 @@ const WatermarkElement = {
1567
1573
  };
1568
1574
  }
1569
1575
  };
1576
+ const ArrowElement = {
1577
+ name: ELEMENT_TYPES.ARROW,
1578
+ async add(params) {
1579
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
1580
+ const { element, index, canvas, canvasMetadata, lockAspectRatio } = params;
1581
+ const baseWidth = ((_a = element.props) == null ? void 0 : _a.width) ?? 220;
1582
+ const baseHeight = ((_b = element.props) == null ? void 0 : _b.height) ?? 14;
1583
+ const { x, y } = convertToCanvasPosition(
1584
+ ((_c = element.props) == null ? void 0 : _c.x) ?? 0,
1585
+ ((_d = element.props) == null ? void 0 : _d.y) ?? 0,
1586
+ canvasMetadata
1587
+ );
1588
+ const fill = ((_e = element.props) == null ? void 0 : _e.fill) || "#f59e0b";
1589
+ const radius = (((_f = element.props) == null ? void 0 : _f.radius) ?? 4) * canvasMetadata.scaleX;
1590
+ const barWidth = baseWidth * canvasMetadata.scaleX;
1591
+ const barHeight = baseHeight * canvasMetadata.scaleY;
1592
+ const headSize = barHeight * 1.8;
1593
+ const barLength = barWidth - headSize * 0.5;
1594
+ const bar = new fabric.Rect({
1595
+ left: -barWidth / 2,
1596
+ top: -barHeight / 2,
1597
+ originX: "left",
1598
+ originY: "top",
1599
+ width: barLength,
1600
+ height: barHeight,
1601
+ rx: radius,
1602
+ ry: radius,
1603
+ fill
1604
+ });
1605
+ const arrowHead = new fabric.Triangle({
1606
+ left: barWidth / 2 - headSize * 0.25,
1607
+ top: 0,
1608
+ originX: "center",
1609
+ originY: "center",
1610
+ width: headSize,
1611
+ height: headSize,
1612
+ fill,
1613
+ angle: 90
1614
+ });
1615
+ const opacity = ((_g = element.props) == null ? void 0 : _g.opacity) ?? 1;
1616
+ const group = new fabric.Group([bar, arrowHead], {
1617
+ left: x,
1618
+ top: y,
1619
+ originX: "center",
1620
+ originY: "center",
1621
+ angle: ((_h = element.props) == null ? void 0 : _h.rotation) || 0,
1622
+ opacity,
1623
+ selectable: true,
1624
+ hasControls: true
1625
+ });
1626
+ group.set(
1627
+ "lockUniScaling",
1628
+ lockAspectRatio ?? ((_i = element.props) == null ? void 0 : _i.lockAspectRatio) ?? true
1629
+ );
1630
+ group.set("id", element.id);
1631
+ group.set("zIndex", index);
1632
+ canvas.add(group);
1633
+ },
1634
+ updateFromFabricObject(object, element, context) {
1635
+ var _a, _b, _c;
1636
+ const canvasCenter = getObjectCanvasCenter(object);
1637
+ const { x, y } = convertToVideoPosition(
1638
+ canvasCenter.x,
1639
+ canvasCenter.y,
1640
+ context.canvasMetadata,
1641
+ context.videoSize
1642
+ );
1643
+ const baseWidth = ((_a = element.props) == null ? void 0 : _a.width) ?? 220;
1644
+ const baseHeight = ((_b = element.props) == null ? void 0 : _b.height) ?? 14;
1645
+ const opacity = object.opacity ?? ((_c = element.props) == null ? void 0 : _c.opacity) ?? 1;
1646
+ return {
1647
+ element: {
1648
+ ...element,
1649
+ props: {
1650
+ ...element.props,
1651
+ rotation: getObjectCanvasAngle(object),
1652
+ width: baseWidth * object.scaleX,
1653
+ height: baseHeight * object.scaleY,
1654
+ x,
1655
+ y,
1656
+ opacity
1657
+ }
1658
+ }
1659
+ };
1660
+ }
1661
+ };
1662
+ const LineElement = {
1663
+ name: ELEMENT_TYPES.LINE,
1664
+ async add(params) {
1665
+ var _a;
1666
+ const { element, index, canvas, canvasMetadata, lockAspectRatio } = params;
1667
+ const lineProps = element.props ?? {};
1668
+ const lineElement = {
1669
+ ...element,
1670
+ props: {
1671
+ ...lineProps,
1672
+ // Use fill as stroke color when a stroke is desired; otherwise rely
1673
+ // on fill-only rendering. Avoid the generic "#000000" fallback.
1674
+ stroke: lineProps.stroke ?? lineProps.fill,
1675
+ // If a specific lineWidth is provided, keep it; otherwise default to 0
1676
+ // so the stroke does not override the visual fill color.
1677
+ lineWidth: lineProps.lineWidth ?? 0
1678
+ }
1679
+ };
1680
+ await addRectElement({
1681
+ element: lineElement,
1682
+ index,
1683
+ canvas,
1684
+ canvasMetadata,
1685
+ lockAspectRatio: lockAspectRatio ?? ((_a = lineElement.props) == null ? void 0 : _a.lockAspectRatio)
1686
+ });
1687
+ },
1688
+ updateFromFabricObject(object, element, context) {
1689
+ var _a, _b;
1690
+ const canvasCenter = getObjectCanvasCenter(object);
1691
+ const { x, y } = convertToVideoPosition(
1692
+ canvasCenter.x,
1693
+ canvasCenter.y,
1694
+ context.canvasMetadata,
1695
+ context.videoSize
1696
+ );
1697
+ return {
1698
+ element: {
1699
+ ...element,
1700
+ props: {
1701
+ ...element.props,
1702
+ rotation: getObjectCanvasAngle(object),
1703
+ width: (((_a = element.props) == null ? void 0 : _a.width) ?? 0) * object.scaleX,
1704
+ height: (((_b = element.props) == null ? void 0 : _b.height) ?? 0) * object.scaleY,
1705
+ x,
1706
+ y
1707
+ }
1708
+ }
1709
+ };
1710
+ }
1711
+ };
1712
+ const EffectElement = {
1713
+ name: ELEMENT_TYPES.EFFECT,
1714
+ async add() {
1715
+ return;
1716
+ }
1717
+ };
1570
1718
  class ElementController {
1571
1719
  constructor() {
1572
1720
  __publicField(this, "elements", /* @__PURE__ */ new Map());
@@ -1590,8 +1738,17 @@ function registerElements() {
1590
1738
  elementController.register(TextElement);
1591
1739
  elementController.register(CaptionElement);
1592
1740
  elementController.register(WatermarkElement);
1741
+ elementController.register(ArrowElement);
1742
+ elementController.register(LineElement);
1743
+ elementController.register(EffectElement);
1593
1744
  }
1594
1745
  registerElements();
1746
+ function registerCanvasHandler(handler) {
1747
+ elementController.register(handler);
1748
+ }
1749
+ function getCanvasHandler(name) {
1750
+ return elementController.get(name);
1751
+ }
1595
1752
  const useTwickCanvas = ({
1596
1753
  onCanvasReady,
1597
1754
  onCanvasOperation,
@@ -1983,7 +2140,9 @@ exports.convertToVideoPosition = convertToVideoPosition;
1983
2140
  exports.createCanvas = createCanvas;
1984
2141
  exports.disabledControl = disabledControl;
1985
2142
  exports.elementController = elementController;
2143
+ exports.getCanvasHandler = getCanvasHandler;
1986
2144
  exports.getCurrentFrameEffect = getCurrentFrameEffect;
2145
+ exports.registerCanvasHandler = registerCanvasHandler;
1987
2146
  exports.reorderElementsByZIndex = reorderElementsByZIndex;
1988
2147
  exports.rotateControl = rotateControl;
1989
2148
  exports.useTwickCanvas = useTwickCanvas;