@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.d.ts +1 -1
- package/dist/index.js +107 -31
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +108 -32
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -46,7 +46,9 @@ const CANVAS_OPERATIONS = {
|
|
|
46
46
|
/** Items have been grouped together */
|
|
47
47
|
ITEM_GROUPED: "ITEM_GROUPED",
|
|
48
48
|
/** Items have been ungrouped */
|
|
49
|
-
ITEM_UNGROUPED: "ITEM_UNGROUPED"
|
|
49
|
+
ITEM_UNGROUPED: "ITEM_UNGROUPED",
|
|
50
|
+
/** Caption properties have been updated */
|
|
51
|
+
CAPTION_PROPS_UPDATED: "CAPTION_PROPS_UPDATED"
|
|
50
52
|
};
|
|
51
53
|
const ELEMENT_TYPES = {
|
|
52
54
|
/** Text element type */
|
|
@@ -58,7 +60,9 @@ const ELEMENT_TYPES = {
|
|
|
58
60
|
/** Video element type */
|
|
59
61
|
VIDEO: "video",
|
|
60
62
|
/** Rectangle element type */
|
|
61
|
-
RECT: "rect"
|
|
63
|
+
RECT: "rect",
|
|
64
|
+
/** Circle element type */
|
|
65
|
+
CIRCLE: "circle"
|
|
62
66
|
};
|
|
63
67
|
const isBrowser = typeof window !== "undefined";
|
|
64
68
|
const isCanvasSupported = isBrowser && !!window.HTMLCanvasElement;
|
|
@@ -418,33 +422,33 @@ const addCaptionElement = ({
|
|
|
418
422
|
captionProps,
|
|
419
423
|
canvasMetadata
|
|
420
424
|
}) => {
|
|
421
|
-
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;
|
|
425
|
+
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;
|
|
422
426
|
const { x, y } = convertToCanvasPosition(
|
|
423
|
-
((
|
|
424
|
-
((
|
|
427
|
+
((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.x : (_a = element.props) == null ? void 0 : _a.x) ?? 0,
|
|
428
|
+
((captionProps == null ? void 0 : captionProps.applyToAll) ? captionProps == null ? void 0 : captionProps.y : (_b = element.props) == null ? void 0 : _b.y) ?? 0,
|
|
425
429
|
canvasMetadata
|
|
426
430
|
);
|
|
427
|
-
const caption = new fabric.FabricText(((
|
|
431
|
+
const caption = new fabric.FabricText(((_c = element.props) == null ? void 0 : _c.text) || element.t || "", {
|
|
428
432
|
left: x,
|
|
429
433
|
top: y,
|
|
430
434
|
originX: "center",
|
|
431
435
|
originY: "center",
|
|
432
|
-
angle: ((
|
|
436
|
+
angle: ((_d = element.props) == null ? void 0 : _d.rotation) || 0,
|
|
433
437
|
fontSize: Math.round(
|
|
434
|
-
(((
|
|
438
|
+
(((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
|
|
435
439
|
),
|
|
436
|
-
fontFamily: ((
|
|
437
|
-
fill: ((
|
|
438
|
-
fontWeight: DEFAULT_CAPTION_PROPS.fontWeight,
|
|
439
|
-
stroke: ((
|
|
440
|
-
opacity: ((
|
|
440
|
+
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,
|
|
441
|
+
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,
|
|
442
|
+
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,
|
|
443
|
+
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,
|
|
444
|
+
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,
|
|
441
445
|
shadow: new fabric.Shadow({
|
|
442
|
-
offsetX: ((
|
|
443
|
-
offsetY: ((
|
|
444
|
-
blur: ((
|
|
445
|
-
color: ((
|
|
446
|
+
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]),
|
|
447
|
+
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]),
|
|
448
|
+
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,
|
|
449
|
+
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
|
|
446
450
|
}),
|
|
447
|
-
strokeWidth: ((
|
|
451
|
+
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
|
|
448
452
|
});
|
|
449
453
|
caption.set("id", element.id);
|
|
450
454
|
caption.set("zIndex", index);
|
|
@@ -671,6 +675,40 @@ const addRectElement = ({
|
|
|
671
675
|
canvas.add(rect);
|
|
672
676
|
return rect;
|
|
673
677
|
};
|
|
678
|
+
const addCircleElement = ({
|
|
679
|
+
element,
|
|
680
|
+
index,
|
|
681
|
+
canvas,
|
|
682
|
+
canvasMetadata
|
|
683
|
+
}) => {
|
|
684
|
+
var _a, _b, _c, _d, _e, _f;
|
|
685
|
+
const { x, y } = convertToCanvasPosition(
|
|
686
|
+
((_a = element.props) == null ? void 0 : _a.x) || 0,
|
|
687
|
+
((_b = element.props) == null ? void 0 : _b.y) || 0,
|
|
688
|
+
canvasMetadata
|
|
689
|
+
);
|
|
690
|
+
const circle = new fabric.Circle({
|
|
691
|
+
left: x,
|
|
692
|
+
// X-coordinate on the canvas
|
|
693
|
+
top: y,
|
|
694
|
+
// Y-coordinate on the canvas
|
|
695
|
+
radius: (((_c = element.props) == null ? void 0 : _c.radius) || 0) * canvasMetadata.scaleX,
|
|
696
|
+
fill: ((_d = element.props) == null ? void 0 : _d.fill) || "#000000",
|
|
697
|
+
stroke: ((_e = element.props) == null ? void 0 : _e.stroke) || "#000000",
|
|
698
|
+
strokeWidth: (((_f = element.props) == null ? void 0 : _f.lineWidth) || 0) * canvasMetadata.scaleX,
|
|
699
|
+
originX: "center",
|
|
700
|
+
originY: "center"
|
|
701
|
+
});
|
|
702
|
+
circle.controls.mt = disabledControl;
|
|
703
|
+
circle.controls.mb = disabledControl;
|
|
704
|
+
circle.controls.ml = disabledControl;
|
|
705
|
+
circle.controls.mr = disabledControl;
|
|
706
|
+
circle.controls.mtr = disabledControl;
|
|
707
|
+
circle.set("id", element.id);
|
|
708
|
+
circle.set("zIndex", index);
|
|
709
|
+
canvas.add(circle);
|
|
710
|
+
return circle;
|
|
711
|
+
};
|
|
674
712
|
const addBackgroundColor = ({
|
|
675
713
|
element,
|
|
676
714
|
index,
|
|
@@ -712,6 +750,7 @@ const useTwickCanvas = ({
|
|
|
712
750
|
const twickCanvasRef = react.useRef(null);
|
|
713
751
|
const videoSizeRef = react.useRef({ width: 1, height: 1 });
|
|
714
752
|
const canvasResolutionRef = react.useRef({ width: 1, height: 1 });
|
|
753
|
+
const captionPropsRef = react.useRef(null);
|
|
715
754
|
const canvasMetadataRef = react.useRef({
|
|
716
755
|
width: 0,
|
|
717
756
|
height: 0,
|
|
@@ -769,7 +808,7 @@ const useTwickCanvas = ({
|
|
|
769
808
|
}
|
|
770
809
|
};
|
|
771
810
|
const handleMouseUp = (event) => {
|
|
772
|
-
var _a, _b;
|
|
811
|
+
var _a, _b, _c;
|
|
773
812
|
if (event.target) {
|
|
774
813
|
const object = event.target;
|
|
775
814
|
const elementId = object.get("id");
|
|
@@ -796,20 +835,29 @@ const useTwickCanvas = ({
|
|
|
796
835
|
videoSizeRef.current
|
|
797
836
|
);
|
|
798
837
|
if (elementMap.current[elementId].type === "caption") {
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
838
|
+
if ((_c = captionPropsRef.current) == null ? void 0 : _c.applyToAll) {
|
|
839
|
+
onCanvasOperation == null ? void 0 : onCanvasOperation(CANVAS_OPERATIONS.CAPTION_PROPS_UPDATED, {
|
|
840
|
+
element: elementMap.current[elementId],
|
|
841
|
+
props: {
|
|
842
|
+
...captionPropsRef.current,
|
|
804
843
|
x,
|
|
805
844
|
y
|
|
806
845
|
}
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
846
|
+
});
|
|
847
|
+
} else {
|
|
848
|
+
elementMap.current[elementId] = {
|
|
849
|
+
...elementMap.current[elementId],
|
|
850
|
+
props: {
|
|
851
|
+
...elementMap.current[elementId].props,
|
|
852
|
+
x,
|
|
853
|
+
y
|
|
854
|
+
}
|
|
855
|
+
};
|
|
856
|
+
onCanvasOperation == null ? void 0 : onCanvasOperation(
|
|
857
|
+
CANVAS_OPERATIONS.ITEM_UPDATED,
|
|
858
|
+
elementMap.current[elementId]
|
|
859
|
+
);
|
|
860
|
+
}
|
|
813
861
|
} else {
|
|
814
862
|
if ((object == null ? void 0 : object.type) === "group") {
|
|
815
863
|
const currentFrameEffect = elementFrameMap.current[elementId];
|
|
@@ -873,6 +921,22 @@ const useTwickCanvas = ({
|
|
|
873
921
|
y
|
|
874
922
|
}
|
|
875
923
|
};
|
|
924
|
+
} else if ((object == null ? void 0 : object.type) === "circle") {
|
|
925
|
+
const radius = Number(
|
|
926
|
+
(elementMap.current[elementId].props.radius * object.scaleX).toFixed(2)
|
|
927
|
+
);
|
|
928
|
+
elementMap.current[elementId] = {
|
|
929
|
+
...elementMap.current[elementId],
|
|
930
|
+
props: {
|
|
931
|
+
...elementMap.current[elementId].props,
|
|
932
|
+
rotation: object.angle,
|
|
933
|
+
radius,
|
|
934
|
+
height: radius * 2,
|
|
935
|
+
width: radius * 2,
|
|
936
|
+
x,
|
|
937
|
+
y
|
|
938
|
+
}
|
|
939
|
+
};
|
|
876
940
|
} else {
|
|
877
941
|
elementMap.current[elementId] = {
|
|
878
942
|
...elementMap.current[elementId],
|
|
@@ -915,6 +979,7 @@ const useTwickCanvas = ({
|
|
|
915
979
|
twickCanvas.renderAll();
|
|
916
980
|
}
|
|
917
981
|
}
|
|
982
|
+
captionPropsRef.current = captionProps;
|
|
918
983
|
await Promise.all(
|
|
919
984
|
elements.map(async (element, index) => {
|
|
920
985
|
try {
|
|
@@ -953,7 +1018,10 @@ const useTwickCanvas = ({
|
|
|
953
1018
|
}
|
|
954
1019
|
switch (element.type) {
|
|
955
1020
|
case ELEMENT_TYPES.VIDEO:
|
|
956
|
-
const currentFrameEffect = getCurrentFrameEffect(
|
|
1021
|
+
const currentFrameEffect = getCurrentFrameEffect(
|
|
1022
|
+
element,
|
|
1023
|
+
seekTime || 0
|
|
1024
|
+
);
|
|
957
1025
|
elementFrameMap.current[element.id] = currentFrameEffect;
|
|
958
1026
|
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);
|
|
959
1027
|
await addVideoElement({
|
|
@@ -997,6 +1065,14 @@ const useTwickCanvas = ({
|
|
|
997
1065
|
canvasMetadata: canvasMetadataRef.current
|
|
998
1066
|
});
|
|
999
1067
|
break;
|
|
1068
|
+
case ELEMENT_TYPES.CIRCLE:
|
|
1069
|
+
await addCircleElement({
|
|
1070
|
+
element,
|
|
1071
|
+
index,
|
|
1072
|
+
canvas: twickCanvas,
|
|
1073
|
+
canvasMetadata: canvasMetadataRef.current
|
|
1074
|
+
});
|
|
1075
|
+
break;
|
|
1000
1076
|
case ELEMENT_TYPES.TEXT:
|
|
1001
1077
|
await addTextElement({
|
|
1002
1078
|
element,
|