customized-fabric 2.0.8 → 2.0.10

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.
@@ -50,7 +50,7 @@ export default class ImagePlaceholder extends fabric.Group {
50
50
  removeUploadedImage?: () => void;
51
51
  loadMask?: (maskInput: File | string) => void;
52
52
  removeMask?: () => void;
53
- loadCustomizedImage?: (image: IImageOptions) => Promise<void>;
53
+ loadCustomImage?: (image: IImageOptions) => Promise<void>;
54
54
  setImageType?: (type: ImageFilterType) => void;
55
55
  setImageColor?: (color: string) => void;
56
56
  applyImageFilters?: (image: fabric.Image | undefined) => void;
@@ -153,17 +153,23 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
153
153
  },
154
154
  toImageObject: function () {
155
155
  if (this.uploadedImage) {
156
- const { originX, originY, left = 0, top = 0, width, height, scaleX, scaleY, angle = 0, flipX, flipY, skewX, skewY, opacity, } = this.uploadedImage;
156
+ const { originX, originY, left = 0, top = 0, width = 0, height = 0, scaleX = 0, scaleY = 0, angle = 0, flipX, flipY, skewX, skewY, opacity, } = this.uploadedImage;
157
+ const { relativeLeft, relativeTop } = (0, utils_1.getRelativePosition)(this.uploadedImage, this);
157
158
  return {
159
+ relativeLeftRatio: relativeLeft / this.width,
160
+ relativeTopRatio: relativeTop / this.height,
161
+ relativeScaleX: (width * scaleX) / this.width,
162
+ relativeScaleY: (height * scaleY) / this.height,
163
+ relativeAngle: angle - this.angle,
158
164
  originX,
159
165
  originY,
160
- left: left - this.left,
161
- top: top - this.top,
166
+ left,
167
+ top,
162
168
  width,
163
169
  height,
164
170
  scaleX,
165
171
  scaleY,
166
- angle: angle - this.angle,
172
+ angle,
167
173
  flipX,
168
174
  flipY,
169
175
  skewX,
@@ -174,6 +180,7 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
174
180
  color: this?.imageColor,
175
181
  removeBackground: this?.removeBackground,
176
182
  faceCutout: this?.faceCutout,
183
+ advancedFilter: this?.advancedFilter,
177
184
  },
178
185
  };
179
186
  }
@@ -242,23 +249,23 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
242
249
  return mask;
243
250
  }
244
251
  },
245
- loadCustomizedImage: async function (image) {
252
+ loadCustomImage: async function (image) {
246
253
  if (image) {
247
- const { url, filteredUrl, data, clipPath } = image;
254
+ const { url, filteredUrl, data } = image;
248
255
  const imageUrl = filteredUrl ?? url;
249
- if (imageUrl && data && clipPath) {
256
+ if (imageUrl && data) {
250
257
  const loadedImage = await (0, utils_1.loadImageFromUrl)(imageUrl);
251
258
  if (loadedImage) {
252
259
  const mask = await this.getMask();
253
- const { top, left, scaleX, width, angle, flipX, flipY, settings = {}, } = data;
260
+ const { relativeLeftRatio, relativeTopRatio, relativeScaleX, relativeScaleY, relativeAngle, width, height, flipX, flipY, settings = {}, } = data;
254
261
  const { color, type } = settings;
255
- const ratioWidth = (width * scaleX) / (clipPath.width * clipPath.scaleX);
256
- const ratioPosition = this.width / clipPath.width;
257
- loadedImage.scaleToWidth(ratioWidth * this.width);
262
+ const { absoluteLeft, absoluteRight } = (0, utils_1.getAbsolutePosition)(relativeLeftRatio, relativeTopRatio, this);
258
263
  loadedImage.set({
259
- top: this.top + ratioPosition * top,
260
- left: this.left + ratioPosition * left,
261
- angle: this.angle + angle,
264
+ left: absoluteLeft,
265
+ top: absoluteRight,
266
+ scaleX: (relativeScaleX * this.width) / width,
267
+ scaleY: (relativeScaleY * this.height) / height,
268
+ angle: this.angle + relativeAngle,
262
269
  originX: "center",
263
270
  originY: "center",
264
271
  flipX,
@@ -22,6 +22,11 @@ export interface IImageOptions {
22
22
  data: {
23
23
  originX: string;
24
24
  originY: string;
25
+ relativeLeftRatio: number;
26
+ relativeTopRatio: number;
27
+ relativeScaleX: number;
28
+ relativeScaleY: number;
29
+ relativeAngle: number;
25
30
  left: number;
26
31
  top: number;
27
32
  width: number;
@@ -39,7 +44,6 @@ export interface IImageOptions {
39
44
  type?: ImageFilterType;
40
45
  };
41
46
  };
42
- clipPath: IClipPathOptions;
43
47
  }
44
48
  export interface IImagePlaceholderOptions extends fabric.IGroupOptions {
45
49
  _id?: ObjectId;
package/lib/index.d.ts CHANGED
@@ -5,8 +5,8 @@ import ImagePlaceholder from "./ImagePlaceholderObject";
5
5
  import LayoutGroup from "./LayoutGroupObject";
6
6
  import Calendar from "./CalendarObject";
7
7
  import QRCode from "./QRCodeObject";
8
- import fabric, { lockObject, lockAllObjects, loadImageFromFile, loadImageFromUrl, getObject, asyncGetObject } from "./utils";
9
8
  import { OBJECT_TYPES } from "./constants";
10
9
  import { IMAGE_FILTER_TYPES } from "./ImagePlaceholderObject/constants";
11
10
  import { IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES } from "./ImagePlaceholderObject/constants";
12
- export { fabric, TextInput, Clipart, ImagePlaceholder, LayoutGroup, CurvedText, Calendar, QRCode, lockObject, lockAllObjects, loadImageFromFile, loadImageFromUrl, getObject, asyncGetObject, OBJECT_TYPES, IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES, IMAGE_FILTER_TYPES, };
11
+ export * from "./utils";
12
+ export { TextInput, Clipart, ImagePlaceholder, LayoutGroup, CurvedText, Calendar, QRCode, OBJECT_TYPES, IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES, IMAGE_FILTER_TYPES, };
package/lib/index.js CHANGED
@@ -10,23 +10,14 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
10
10
  if (k2 === undefined) k2 = k;
11
11
  o[k2] = m[k];
12
12
  }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
24
15
  };
25
16
  var __importDefault = (this && this.__importDefault) || function (mod) {
26
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
18
  };
28
19
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.IMAGE_FILTER_TYPES = exports.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES = exports.OBJECT_TYPES = exports.asyncGetObject = exports.getObject = exports.loadImageFromUrl = exports.loadImageFromFile = exports.lockAllObjects = exports.lockObject = exports.QRCode = exports.Calendar = exports.CurvedText = exports.LayoutGroup = exports.ImagePlaceholder = exports.Clipart = exports.TextInput = exports.fabric = void 0;
20
+ exports.IMAGE_FILTER_TYPES = exports.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES = exports.OBJECT_TYPES = exports.QRCode = exports.Calendar = exports.CurvedText = exports.LayoutGroup = exports.ImagePlaceholder = exports.Clipart = exports.TextInput = void 0;
30
21
  const TextInputObject_1 = __importDefault(require("./TextInputObject"));
31
22
  exports.TextInput = TextInputObject_1.default;
32
23
  const CurvedTextObject_1 = __importDefault(require("./CurvedTextObject"));
@@ -41,17 +32,10 @@ const CalendarObject_1 = __importDefault(require("./CalendarObject"));
41
32
  exports.Calendar = CalendarObject_1.default;
42
33
  const QRCodeObject_1 = __importDefault(require("./QRCodeObject"));
43
34
  exports.QRCode = QRCodeObject_1.default;
44
- const utils_1 = __importStar(require("./utils"));
45
- exports.fabric = utils_1.default;
46
- Object.defineProperty(exports, "lockObject", { enumerable: true, get: function () { return utils_1.lockObject; } });
47
- Object.defineProperty(exports, "lockAllObjects", { enumerable: true, get: function () { return utils_1.lockAllObjects; } });
48
- Object.defineProperty(exports, "loadImageFromFile", { enumerable: true, get: function () { return utils_1.loadImageFromFile; } });
49
- Object.defineProperty(exports, "loadImageFromUrl", { enumerable: true, get: function () { return utils_1.loadImageFromUrl; } });
50
- Object.defineProperty(exports, "getObject", { enumerable: true, get: function () { return utils_1.getObject; } });
51
- Object.defineProperty(exports, "asyncGetObject", { enumerable: true, get: function () { return utils_1.asyncGetObject; } });
52
35
  const constants_1 = require("./constants");
53
36
  Object.defineProperty(exports, "OBJECT_TYPES", { enumerable: true, get: function () { return constants_1.OBJECT_TYPES; } });
54
37
  const constants_2 = require("./ImagePlaceholderObject/constants");
55
38
  Object.defineProperty(exports, "IMAGE_FILTER_TYPES", { enumerable: true, get: function () { return constants_2.IMAGE_FILTER_TYPES; } });
56
39
  const constants_3 = require("./ImagePlaceholderObject/constants");
57
40
  Object.defineProperty(exports, "IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES", { enumerable: true, get: function () { return constants_3.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES; } });
41
+ __exportStar(require("./utils"), exports);
@@ -1 +1,10 @@
1
+ import { fabric } from "fabric";
1
2
  export declare const removeValueKeys: (object: any, value?: any) => any;
3
+ export declare const getRelativePosition: (relativeObject: fabric.Object, objectToRelate: fabric.Object) => {
4
+ relativeLeft: number;
5
+ relativeTop: number;
6
+ };
7
+ export declare const getAbsolutePosition: (relativeLeftRatio: number, relativeTopRatio: number, objectToApply: any) => {
8
+ absoluteLeft: number;
9
+ absoluteRight: any;
10
+ };
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeValueKeys = void 0;
3
+ exports.getAbsolutePosition = exports.getRelativePosition = exports.removeValueKeys = void 0;
4
+ const fabric_1 = require("fabric");
4
5
  const removeValueKeys = (object, value = null) => {
5
6
  const newObject = {};
6
7
  for (const key in object) {
@@ -11,3 +12,29 @@ const removeValueKeys = (object, value = null) => {
11
12
  return newObject;
12
13
  };
13
14
  exports.removeValueKeys = removeValueKeys;
15
+ const getRelativePosition = (relativeObject, objectToRelate) => {
16
+ const rLeft = relativeObject.left || 0;
17
+ const rTop = relativeObject.top || 0;
18
+ const oLeft = objectToRelate.left || 0;
19
+ const oTop = objectToRelate.top || 0;
20
+ const oAngle = fabric_1.fabric.util.degreesToRadians(objectToRelate.angle || 0);
21
+ const relativeLeft = Math.cos(-oAngle) * (rLeft - oLeft) - Math.sin(-oAngle) * (rTop - oTop);
22
+ const relativeTop = Math.sin(-oAngle) * (rLeft - oLeft) + Math.cos(-oAngle) * (rTop - oTop);
23
+ return { relativeLeft, relativeTop };
24
+ };
25
+ exports.getRelativePosition = getRelativePosition;
26
+ const getAbsolutePosition = (relativeLeftRatio, relativeTopRatio, objectToApply) => {
27
+ const targetX = objectToApply.left;
28
+ const targetY = objectToApply.top;
29
+ const targetAngle = fabric_1.fabric.util.degreesToRadians(objectToApply.angle);
30
+ const targetWidth = objectToApply.width;
31
+ const targetHeight = objectToApply.height;
32
+ const absoluteLeft = targetX +
33
+ Math.cos(targetAngle) * (relativeLeftRatio * targetWidth) -
34
+ Math.sin(targetAngle) * (relativeTopRatio * targetHeight);
35
+ const absoluteRight = targetY +
36
+ Math.sin(targetAngle) * (relativeLeftRatio * targetWidth) +
37
+ Math.cos(targetAngle) * (relativeTopRatio * targetHeight);
38
+ return { absoluteLeft, absoluteRight };
39
+ };
40
+ exports.getAbsolutePosition = getAbsolutePosition;
@@ -24,4 +24,5 @@ export declare const getObject: (object: any, options?: {
24
24
  export declare const asyncGetObject: (object: any, options?: {
25
25
  isOriginal?: boolean;
26
26
  }) => Promise<Clipart | ImagePlaceholder | CurvedText | Calendar | QRCode | TextInput | undefined>;
27
- export default fabric;
27
+ export { fabric };
28
+ export * from "./common.util";
@@ -22,9 +22,13 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
24
  };
25
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
26
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
27
+ };
25
28
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.asyncGetObject = exports.getObject = exports.lockAllObjects = exports.lockObject = exports.getImageFilters = exports.loadImage = exports.loadImageFromUrl = exports.loadImageFromFile = exports.isFontLoaded = exports.loadFontFromUrl = void 0;
29
+ exports.fabric = exports.asyncGetObject = exports.getObject = exports.lockAllObjects = exports.lockObject = exports.getImageFilters = exports.loadImage = exports.loadImageFromUrl = exports.loadImageFromFile = exports.isFontLoaded = exports.loadFontFromUrl = void 0;
27
30
  const fabric_1 = require("fabric");
31
+ Object.defineProperty(exports, "fabric", { enumerable: true, get: function () { return fabric_1.fabric; } });
28
32
  const constants_1 = require("../constants");
29
33
  const ClipartObject_1 = __importStar(require("../ClipartObject"));
30
34
  const ImagePlaceholderObject_1 = __importStar(require("../ImagePlaceholderObject"));
@@ -204,7 +208,7 @@ const asyncGetObject = async (object, options) => {
204
208
  isOriginal: options?.isOriginal,
205
209
  });
206
210
  if (object?.image) {
207
- const image = await imagePlaceHolderObject?.loadCustomizedImage?.(object?.image);
211
+ const image = await imagePlaceHolderObject?.loadCustomImage?.(object?.image);
208
212
  if (image) {
209
213
  return image;
210
214
  }
@@ -272,4 +276,4 @@ fabric_1.fabric.Object.prototype.toObject = (function (toObject) {
272
276
  }
273
277
  };
274
278
  })(fabric_1.fabric.Object.prototype.toObject);
275
- exports.default = fabric_1.fabric;
279
+ __exportStar(require("./common.util"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "customized-fabric",
3
- "version": "2.0.8",
3
+ "version": "2.0.10",
4
4
  "description": "Customized fabric",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -22,4 +22,4 @@
22
22
  "@types/fabric": "^5.3.3",
23
23
  "typescript": "^5.2.2"
24
24
  }
25
- }
25
+ }