customized-fabric 1.3.1 → 1.3.3

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.
@@ -7,3 +7,4 @@ export declare const IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES: {
7
7
  strokeWidth: number;
8
8
  };
9
9
  };
10
+ export declare const IMAGE_FILTER_TYPES: any;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES = void 0;
3
+ exports.IMAGE_FILTER_TYPES = exports.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES = void 0;
4
4
  exports.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES = {
5
5
  name: "Image placeholder",
6
6
  type: "IMAGE_PLACEHOLDER",
@@ -10,3 +10,8 @@ exports.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES = {
10
10
  strokeWidth: 2,
11
11
  },
12
12
  };
13
+ exports.IMAGE_FILTER_TYPES = {
14
+ blendColor: "Blend color",
15
+ fullColor: "Full color",
16
+ grayscale: "Grayscale",
17
+ };
@@ -1,5 +1,5 @@
1
1
  import { fabric } from "fabric";
2
- import { IImageOptions, IImagePlaceholderOptions } from "./interfaces";
2
+ import { IImageOptions, IImagePlaceholderOptions, ImageFilterType } from "./interfaces";
3
3
  import { ObjectId } from "../utils/objectId";
4
4
  export declare const toImagePlaceholderObject: (imagePlaceholderObject: ImagePlaceholder) => {
5
5
  _id: ObjectId | undefined;
@@ -10,6 +10,8 @@ export declare const toImagePlaceholderObject: (imagePlaceholderObject: ImagePla
10
10
  locked: boolean | undefined;
11
11
  isAdditional: boolean | undefined;
12
12
  maskUrl: string | undefined;
13
+ imageType: string | undefined;
14
+ imageColor: string | undefined;
13
15
  };
14
16
  export default class ImagePlaceholder extends fabric.Group {
15
17
  _id?: ObjectId;
@@ -19,6 +21,8 @@ export default class ImagePlaceholder extends fabric.Group {
19
21
  maskFile?: File;
20
22
  maskUrl?: string;
21
23
  isAdditional?: boolean;
24
+ imageColor?: string;
25
+ imageType?: string;
22
26
  getSettings?: (attribute: string) => any;
23
27
  setSizes?: (options: {
24
28
  width?: number;
@@ -31,5 +35,8 @@ export default class ImagePlaceholder extends fabric.Group {
31
35
  loadMask?: (maskInput: File | string) => void;
32
36
  removeMask?: () => void;
33
37
  loadCustomizedImage?: (image: IImageOptions) => Promise<void>;
38
+ setImageType?: (type: ImageFilterType) => void;
39
+ setImageColor?: (color: string) => void;
40
+ applyImageFilters?: (image: fabric.Image) => void;
34
41
  constructor(options?: IImagePlaceholderOptions);
35
42
  }
@@ -30,6 +30,7 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
30
30
  type: constants_1.IMAGE_PLACEHOLDER_OBJECT_ATTRIBUTES.type,
31
31
  ...options,
32
32
  layerId: options?.personalizeId ?? options?.layerId,
33
+ imageType: "Full color",
33
34
  objectCaching: false,
34
35
  });
35
36
  if (options?.imageFile) {
@@ -43,6 +44,9 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
43
44
  this.loadMask(options?.maskFile ?? options?.maskUrl);
44
45
  }
45
46
  }
47
+ if (options?.imageType) {
48
+ this.setImageType(options?.imageType, { color: options?.imageColor });
49
+ }
46
50
  },
47
51
  loadImage: async function (image) {
48
52
  const loadedImage = await (0, utils_1.loadImage)(image);
@@ -55,6 +59,7 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
55
59
  this.imageObject = loadedImage;
56
60
  this.add(this.imageObject);
57
61
  this.fitImage(this.imageObject);
62
+ this.applyImageFilters(this.imageObject);
58
63
  this.canvas?.renderAll();
59
64
  }
60
65
  },
@@ -222,6 +227,22 @@ const ImagePlaceholderClass = fabric_1.fabric.util.createClass(fabric_1.fabric.G
222
227
  }
223
228
  }
224
229
  },
230
+ setImageType: function (type, options) {
231
+ this.imageType = type;
232
+ this.imageFilters = (0, utils_1.getImageFilters)(type, options);
233
+ this.imageObject && this.applyImageFilters(this.imageObject);
234
+ },
235
+ setImageColor: function (color) {
236
+ this.imageColor = color;
237
+ this.imageFilters = (0, utils_1.getImageFilters)(this.imageType, { color });
238
+ this.imageObject && this.applyImageFilters(this.imageObject);
239
+ },
240
+ applyImageFilters: function (image) {
241
+ const imageFilters = this.imageFilters ?? [];
242
+ image.filters = imageFilters;
243
+ image.applyFilters();
244
+ this.canvas.renderAll();
245
+ },
225
246
  });
226
247
  const toImagePlaceholderObject = (imagePlaceholderObject) => {
227
248
  return {
@@ -233,6 +254,8 @@ const toImagePlaceholderObject = (imagePlaceholderObject) => {
233
254
  locked: imagePlaceholderObject?.locked,
234
255
  isAdditional: imagePlaceholderObject?.isAdditional,
235
256
  maskUrl: imagePlaceholderObject?.maskUrl,
257
+ imageType: imagePlaceholderObject?.imageType,
258
+ imageColor: imagePlaceholderObject?.imageColor,
236
259
  };
237
260
  };
238
261
  exports.toImagePlaceholderObject = toImagePlaceholderObject;
@@ -41,9 +41,12 @@ export interface IImagePlaceholderOptions extends fabric.IGroupOptions {
41
41
  personalizeId?: number;
42
42
  layerId: number;
43
43
  imageFile?: File;
44
+ imageType?: string;
45
+ imageColor?: string;
44
46
  isOriginal?: boolean;
45
47
  isAdditional?: boolean;
46
48
  image?: IImageOptions;
47
49
  maskUrl?: string;
48
50
  maskFile?: File;
49
51
  }
52
+ export declare type ImageFilterType = "Full color" | "Grayscale" | "Blend color";
package/lib/index.d.ts CHANGED
@@ -3,4 +3,5 @@ import Clipart from "./ClipartObject";
3
3
  import ImagePlaceholder from "./ImagePlaceholderObject";
4
4
  import fabric, { lockObject, lockAllObjects, loadImageFromFile, loadImageFromUrl, getObject, asyncGetObject } from "./utils";
5
5
  import { OBJECT_TYPES } from "./constants";
6
- export { fabric, TextInput, Clipart, ImagePlaceholder, lockObject, lockAllObjects, loadImageFromFile, loadImageFromUrl, getObject, asyncGetObject, OBJECT_TYPES, };
6
+ import { IMAGE_FILTER_TYPES } from "./ImagePlaceholderObject/constants";
7
+ export { fabric, TextInput, Clipart, ImagePlaceholder, lockObject, lockAllObjects, loadImageFromFile, loadImageFromUrl, getObject, asyncGetObject, OBJECT_TYPES, IMAGE_FILTER_TYPES, };
package/lib/index.js CHANGED
@@ -26,7 +26,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.OBJECT_TYPES = exports.asyncGetObject = exports.getObject = exports.loadImageFromUrl = exports.loadImageFromFile = exports.lockAllObjects = exports.lockObject = exports.ImagePlaceholder = exports.Clipart = exports.TextInput = exports.fabric = void 0;
29
+ exports.IMAGE_FILTER_TYPES = exports.OBJECT_TYPES = exports.asyncGetObject = exports.getObject = exports.loadImageFromUrl = exports.loadImageFromFile = exports.lockAllObjects = exports.lockObject = exports.ImagePlaceholder = exports.Clipart = exports.TextInput = exports.fabric = void 0;
30
30
  const TextInputObject_1 = __importDefault(require("./TextInputObject"));
31
31
  exports.TextInput = TextInputObject_1.default;
32
32
  const ClipartObject_1 = __importDefault(require("./ClipartObject"));
@@ -43,3 +43,5 @@ Object.defineProperty(exports, "getObject", { enumerable: true, get: function ()
43
43
  Object.defineProperty(exports, "asyncGetObject", { enumerable: true, get: function () { return utils_1.asyncGetObject; } });
44
44
  const constants_1 = require("./constants");
45
45
  Object.defineProperty(exports, "OBJECT_TYPES", { enumerable: true, get: function () { return constants_1.OBJECT_TYPES; } });
46
+ const constants_2 = require("./ImagePlaceholderObject/constants");
47
+ Object.defineProperty(exports, "IMAGE_FILTER_TYPES", { enumerable: true, get: function () { return constants_2.IMAGE_FILTER_TYPES; } });
@@ -2,11 +2,15 @@ import { fabric } from "fabric";
2
2
  import Clipart from "../ClipartObject";
3
3
  import ImagePlaceholder from "../ImagePlaceholderObject";
4
4
  import TextInput from "../TextInputObject";
5
+ import { ImageFilterType } from "../ImagePlaceholderObject/interfaces";
5
6
  export declare const loadFontFromUrl: (url: string, name: string) => Promise<void>;
6
7
  export declare const isFontLoaded: (name: string) => boolean;
7
8
  export declare const loadImageFromFile: (image: File) => Promise<fabric.Image>;
8
9
  export declare const loadImageFromUrl: (url: string) => Promise<fabric.Image>;
9
10
  export declare const loadImage: (imageInput: File | string) => Promise<fabric.Image>;
11
+ export declare const getImageFilters: (type: ImageFilterType, options?: {
12
+ color?: string;
13
+ }) => fabric.IGrayscaleFilter[];
10
14
  export declare const lockObject: (object: fabric.Object | any, locked: boolean, selectable?: boolean) => void;
11
15
  export declare const lockAllObjects: (canvas: fabric.Canvas, locked: boolean, selectable?: boolean) => void;
12
16
  export declare const getObject: (object: any, options?: {
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.asyncGetObject = exports.getObject = exports.lockAllObjects = exports.lockObject = exports.loadImage = exports.loadImageFromUrl = exports.loadImageFromFile = exports.isFontLoaded = exports.loadFontFromUrl = void 0;
26
+ exports.asyncGetObject = exports.getObject = exports.lockAllObjects = exports.lockObject = exports.getImageFilters = exports.loadImage = exports.loadImageFromUrl = exports.loadImageFromFile = exports.isFontLoaded = exports.loadFontFromUrl = void 0;
27
27
  const fabric_1 = require("fabric");
28
28
  const constants_1 = require("../constants");
29
29
  const ClipartObject_1 = __importStar(require("../ClipartObject"));
@@ -86,6 +86,22 @@ const loadImage = async (imageInput) => {
86
86
  return loadedImage;
87
87
  };
88
88
  exports.loadImage = loadImage;
89
+ const getImageFilters = (type, options = {}) => {
90
+ const filters = fabric_1.fabric.Image.filters;
91
+ const { color = "#ffffff" } = options;
92
+ switch (type) {
93
+ case "Blend color": {
94
+ return [new filters.BlendColor({ color })];
95
+ }
96
+ case "Full color": {
97
+ return [];
98
+ }
99
+ case "Grayscale": {
100
+ return [new filters.Grayscale()];
101
+ }
102
+ }
103
+ };
104
+ exports.getImageFilters = getImageFilters;
89
105
  const lockObject = (object, locked, selectable) => {
90
106
  object.set({
91
107
  hasControls: !locked,
@@ -123,6 +139,9 @@ const getObject = (object, options) => {
123
139
  ...object,
124
140
  isOriginal: options?.isOriginal,
125
141
  });
142
+ if (object?.clipartUrl) {
143
+ clipartObject?.loadImage?.(object?.clipartUrl);
144
+ }
126
145
  return clipartObject;
127
146
  }
128
147
  case constants_1.OBJECT_TYPES.imagePlaceHolder: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "customized-fabric",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Customized fabric",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",