@twick/canvas 0.0.1 → 0.14.0

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/LICENSE ADDED
@@ -0,0 +1,197 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ -------------------------------------------------------------------------------
177
+
178
+ ADDITIONAL TERMS – NO RESALE / NO STANDALONE REDISTRIBUTION
179
+
180
+ The following additional terms apply to this project:
181
+
182
+ 1. **No Standalone Resale or Licensing**
183
+ You may not sell, license, sublicense, or otherwise commercially distribute this software, or any modified version of it, as a standalone product, SDK, or developer tool/library.
184
+
185
+ 2. **Permitted Commercial Use**
186
+ You may use this software in commercial applications, platforms, or services provided that:
187
+ - It is part of a larger product or service with meaningful added value.
188
+ - It is not positioned or marketed as a competing SDK or developer tool.
189
+
190
+ 3. **Cloud Function Restrictions**
191
+ You may self-host the cloud function components within your application or product. However, you may not offer them as a hosted service, API, or commercial cloud product without obtaining a commercial license.
192
+
193
+ 4. **Trademark and Branding Restrictions**
194
+ You may not use the name, logo, or branding associated with this project to market, redistribute, or promote your own products without explicit written permission.
195
+
196
+ For commercial licensing, redistribution rights, or branding permissions, please contact: [contact@kifferai.com]
197
+
package/dist/index.js CHANGED
@@ -2,6 +2,39 @@
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const fabric = require("fabric");
4
4
  const react = require("react");
5
+ const DEFAULT_TEXT_PROPS = {
6
+ family: "Poppins",
7
+ size: 48,
8
+ fill: "#FFFFFF",
9
+ stroke: "#000000",
10
+ lineWidth: 0
11
+ };
12
+ const DEFAULT_CAPTION_PROPS = {
13
+ family: "Poppins",
14
+ size: 48,
15
+ fill: "#FFFFFF",
16
+ fontWeight: 600,
17
+ stroke: "#000000",
18
+ lineWidth: 0.2,
19
+ shadowColor: "#000000",
20
+ shadowBlur: 2,
21
+ shadowOffset: [0, 0]
22
+ };
23
+ const CANVAS_OPERATIONS = {
24
+ ITEM_SELECTED: "ITEM_SELECTED",
25
+ ITEM_UPDATED: "ITEM_UPDATED",
26
+ ITEM_DELETED: "ITEM_DELETED",
27
+ ITEM_ADDED: "ITEM_ADDED",
28
+ ITEM_GROUPED: "ITEM_GROUPED",
29
+ ITEM_UNGROUPED: "ITEM_UNGROUPED"
30
+ };
31
+ const ELEMENT_TYPES = {
32
+ TEXT: "text",
33
+ CAPTION: "caption",
34
+ IMAGE: "image",
35
+ VIDEO: "video",
36
+ RECT: "rect"
37
+ };
5
38
  const isBrowser = typeof window !== "undefined";
6
39
  const isCanvasSupported = isBrowser && !!window.HTMLCanvasElement;
7
40
  function assertBrowser() {
@@ -14,7 +47,7 @@ function assertCanvasSupport() {
14
47
  throw new Error("Canvas is not supported in this environment");
15
48
  }
16
49
  }
17
- function createCanvas({
50
+ const createCanvas = ({
18
51
  videoSize,
19
52
  canvasSize,
20
53
  canvasRef,
@@ -24,7 +57,7 @@ function createCanvas({
24
57
  uniScaleTransform = true,
25
58
  enableRetinaScaling = true,
26
59
  touchZoomThreshold = 10
27
- }) {
60
+ }) => {
28
61
  assertBrowser();
29
62
  assertCanvasSupport();
30
63
  const canvasMetadata = {
@@ -61,35 +94,36 @@ function createCanvas({
61
94
  canvas,
62
95
  canvasMetadata
63
96
  };
64
- }
65
- function reorderElementsByZIndex(canvas) {
97
+ };
98
+ const reorderElementsByZIndex = (canvas) => {
66
99
  if (!canvas) return;
67
- let backgroundColor = canvas.backgroundColor;
100
+ const backgroundColor = canvas.backgroundColor;
68
101
  const objects = canvas.getObjects();
102
+ console.log("objects", objects);
69
103
  objects.sort((a, b) => (a.zIndex || 0) - (b.zIndex || 0));
70
104
  canvas.clear();
71
105
  canvas.backgroundColor = backgroundColor;
72
106
  objects.forEach((obj) => canvas.add(obj));
73
107
  canvas.renderAll();
74
- }
75
- function clearCanvas(canvas) {
108
+ };
109
+ const clearCanvas = (canvas) => {
76
110
  if (!canvas) return;
77
111
  canvas.clear();
78
112
  canvas.renderAll();
79
- }
80
- function convertToCanvasPosition(x, y, canvasMetadata) {
113
+ };
114
+ const convertToCanvasPosition = (x, y, canvasMetadata) => {
81
115
  return {
82
116
  x: x * canvasMetadata.scaleX + canvasMetadata.width / 2,
83
117
  y: y * canvasMetadata.scaleY + canvasMetadata.height / 2
84
118
  };
85
- }
86
- function convertToVideoPosition(x, y, canvasMetadata, videoSize) {
119
+ };
120
+ const convertToVideoPosition = (x, y, canvasMetadata, videoSize) => {
87
121
  return {
88
122
  x: x / canvasMetadata.scaleX - videoSize.width / 2,
89
123
  y: y / canvasMetadata.scaleY - videoSize.height / 2
90
124
  };
91
- }
92
- function getCurrentFrameEffect(item, seekTime) {
125
+ };
126
+ const getCurrentFrameEffect = (item, seekTime) => {
93
127
  var _a;
94
128
  let currentFrameEffect;
95
129
  for (let i = 0; i < ((_a = item == null ? void 0 : item.frameEffects) == null ? void 0 : _a.length); i++) {
@@ -99,39 +133,6 @@ function getCurrentFrameEffect(item, seekTime) {
99
133
  }
100
134
  }
101
135
  return currentFrameEffect;
102
- }
103
- const DEFAULT_TEXT_PROPS = {
104
- family: "Poppins",
105
- size: 48,
106
- fill: "#FFFFFF",
107
- stroke: "#000000",
108
- lineWidth: 0
109
- };
110
- const DEFAULT_CAPTION_PROPS = {
111
- family: "Poppins",
112
- size: 48,
113
- fill: "#FFFFFF",
114
- fontWeight: 600,
115
- stroke: "#000000",
116
- lineWidth: 0.2,
117
- shadowColor: "#000000",
118
- shadowBlur: 2,
119
- shadowOffset: [0, 0]
120
- };
121
- const CANVAS_OPERATIONS = {
122
- ITEM_SELECTED: "ITEM_SELECTED",
123
- ITEM_UPDATED: "ITEM_UPDATED",
124
- ITEM_DELETED: "ITEM_DELETED",
125
- ITEM_ADDED: "ITEM_ADDED",
126
- ITEM_GROUPED: "ITEM_GROUPED",
127
- ITEM_UNGROUPED: "ITEM_UNGROUPED"
128
- };
129
- const ELEMENT_TYPES = {
130
- TEXT: "text",
131
- CAPTION: "caption",
132
- IMAGE: "image",
133
- VIDEO: "video",
134
- RECT: "rect"
135
136
  };
136
137
  const disabledControl = new fabric.Control({
137
138
  x: 0,
@@ -160,7 +161,7 @@ const rotateControl = new fabric.Control({
160
161
  actionName: "rotate",
161
162
  withConnection: true
162
163
  });
163
- async function getThumbnail(videoUrl, seekTime = 0.1, playbackRate = 1) {
164
+ const getThumbnail = async (videoUrl, seekTime = 0.1, playbackRate = 1) => {
164
165
  return new Promise((resolve, reject) => {
165
166
  const video = document.createElement("video");
166
167
  video.crossOrigin = "anonymous";
@@ -169,6 +170,14 @@ async function getThumbnail(videoUrl, seekTime = 0.1, playbackRate = 1) {
169
170
  video.autoplay = false;
170
171
  video.preload = "auto";
171
172
  video.playbackRate = playbackRate;
173
+ video.style.position = "absolute";
174
+ video.style.left = "-9999px";
175
+ video.style.top = "-9999px";
176
+ video.style.width = "1px";
177
+ video.style.height = "1px";
178
+ video.style.opacity = "0";
179
+ video.style.pointerEvents = "none";
180
+ video.style.zIndex = "-1";
172
181
  let timeoutId;
173
182
  const cleanup = () => {
174
183
  if (video.parentNode) video.remove();
@@ -236,8 +245,8 @@ async function getThumbnail(videoUrl, seekTime = 0.1, playbackRate = 1) {
236
245
  video.src = videoUrl;
237
246
  document.body.appendChild(video);
238
247
  });
239
- }
240
- function getObjectFitSize(objectFit, elementSize, containerSize) {
248
+ };
249
+ const getObjectFitSize = (objectFit, elementSize, containerSize) => {
241
250
  const elementAspectRatio = elementSize.width / elementSize.height;
242
251
  const containerAspectRatio = containerSize.width / containerSize.height;
243
252
  switch (objectFit) {
@@ -276,20 +285,20 @@ function getObjectFitSize(objectFit, elementSize, containerSize) {
276
285
  height: elementSize.height
277
286
  };
278
287
  }
279
- }
288
+ };
280
289
  const addTextElement = ({
281
290
  element,
282
291
  index,
283
292
  canvas,
284
293
  canvasMetadata
285
294
  }) => {
286
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
295
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
287
296
  const { x, y } = convertToCanvasPosition(
288
297
  ((_a = element.props) == null ? void 0 : _a.x) || 0,
289
298
  ((_b = element.props) == null ? void 0 : _b.y) || 0,
290
299
  canvasMetadata
291
300
  );
292
- const text = new fabric.FabricText(((_c = element.props) == null ? void 0 : _c.text) || "", {
301
+ const text = new fabric.FabricText(((_c = element.props) == null ? void 0 : _c.text) || element.t || "", {
293
302
  left: x,
294
303
  top: y,
295
304
  originX: "center",
@@ -302,13 +311,15 @@ const addTextElement = ({
302
311
  fontStyle: ((_g = element.props) == null ? void 0 : _g.fontStyle) || "normal",
303
312
  fontWeight: ((_h = element.props) == null ? void 0 : _h.fontWeight) || "normal",
304
313
  fill: ((_i = element.props) == null ? void 0 : _i.fill) || DEFAULT_TEXT_PROPS.fill,
305
- stroke: ((_j = element.props) == null ? void 0 : _j.stroke) || DEFAULT_TEXT_PROPS.stroke,
306
- strokeWidth: ((_k = element.props) == null ? void 0 : _k.lineWidth) || DEFAULT_TEXT_PROPS.lineWidth,
307
- shadow: ((_l = element.props) == null ? void 0 : _l.shadowColor) ? new fabric.Shadow({
308
- offsetX: ((_n = (_m = element.props) == null ? void 0 : _m.shadowOffset) == null ? void 0 : _n.length) && ((_p = (_o = element.props) == null ? void 0 : _o.shadowOffset) == null ? void 0 : _p.length) > 1 ? element.props.shadowOffset[0] / 2 : 1,
309
- offsetY: ((_r = (_q = element.props) == null ? void 0 : _q.shadowOffset) == null ? void 0 : _r.length) && ((_s = element.props) == null ? void 0 : _s.shadowOffset.length) > 1 ? element.props.shadowOffset[1] / 2 : 1,
310
- blur: (((_t = element.props) == null ? void 0 : _t.shadowBlur) || 2) / 2,
311
- color: (_u = element.props) == null ? void 0 : _u.shadowColor
314
+ skipWrapping: false,
315
+ textAlign: ((_j = element.props) == null ? void 0 : _j.textAlign) || "center",
316
+ stroke: ((_k = element.props) == null ? void 0 : _k.stroke) || DEFAULT_TEXT_PROPS.stroke,
317
+ strokeWidth: ((_l = element.props) == null ? void 0 : _l.lineWidth) || DEFAULT_TEXT_PROPS.lineWidth,
318
+ shadow: ((_m = element.props) == null ? void 0 : _m.shadowColor) ? new fabric.Shadow({
319
+ offsetX: ((_o = (_n = element.props) == null ? void 0 : _n.shadowOffset) == null ? void 0 : _o.length) && ((_q = (_p = element.props) == null ? void 0 : _p.shadowOffset) == null ? void 0 : _q.length) > 1 ? element.props.shadowOffset[0] / 2 : 1,
320
+ offsetY: ((_s = (_r = element.props) == null ? void 0 : _r.shadowOffset) == null ? void 0 : _s.length) && ((_t = element.props) == null ? void 0 : _t.shadowOffset.length) > 1 ? element.props.shadowOffset[1] / 2 : 1,
321
+ blur: (((_u = element.props) == null ? void 0 : _u.shadowBlur) || 2) / 2,
322
+ color: (_v = element.props) == null ? void 0 : _v.shadowColor
312
323
  }) : void 0
313
324
  });
314
325
  text.set("id", element.id);
@@ -363,7 +374,7 @@ const addCaptionElement = ({
363
374
  ((_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,
364
375
  canvasMetadata
365
376
  );
366
- const caption = new fabric.FabricText(((_g = element.props) == null ? void 0 : _g.text) || "", {
377
+ const caption = new fabric.FabricText(((_g = element.props) == null ? void 0 : _g.text) || element.t || "", {
367
378
  left: x,
368
379
  top: y,
369
380
  originX: "center",
@@ -527,8 +538,8 @@ const addMediaGroup = ({
527
538
  lockUniScaling: true,
528
539
  originX: "center",
529
540
  originY: "center",
530
- width: newSize.width,
531
- height: newSize.height
541
+ scaleX: newSize.width / img.width,
542
+ scaleY: newSize.height / img.height
532
543
  });
533
544
  const { x, y } = convertToCanvasPosition(
534
545
  (framePosition == null ? void 0 : framePosition.x) || 0,