@leapfrog/interactive-canvas 2.0.23-beta-6 → 2.0.23-beta-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.
@@ -202,11 +202,11 @@ export declare abstract class AbstractInteractiveCanvas implements IInteractiveC
202
202
  * @inheritDoc
203
203
  */
204
204
  sendObjectToBack(target: FabricObject): void;
205
+ addImageObject(imageUrl: string): Promise<IImage>;
205
206
  /**
206
207
  * @override
207
208
  * @inheritDoc
208
209
  */
209
- addImageObject(imageUrl: string): Promise<IImage>;
210
210
  /**
211
211
  * @override
212
212
  * @inheritDoc
@@ -1,5 +1,6 @@
1
1
  import _, { round, each, values, reduce, clone, max, isEqual } from 'lodash';
2
2
  import { BehaviorSubject, Subject } from 'rxjs';
3
+ import { loadImage } from 'canvas';
3
4
  import * as fabric from 'fabric';
4
5
 
5
6
  const PIXELS_PER_MM$2 = 3.937;
@@ -3033,81 +3034,82 @@ class AbstractInteractiveCanvas {
3033
3034
  this.fabricCanvas.requestRenderAll();
3034
3035
  }
3035
3036
  }
3036
- /* public async addImageObject(imageUrl: string): Promise<IImage> {
3037
- // Don't let the image be more than half the canvas in size.
3038
- const maxSizeScale = 2;
3039
- const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3040
-
3041
- const options = { crossOrigin: "anonymous" };
3042
-
3043
- if (this.headless) {
3044
- // Server-side logic without mock DOM
3045
- //const canvas = createCanvas(1, 1);
3046
- //const ctx = canvas.getContext("2d");
3047
-
3048
- const image = await loadImage(imageUrl);
3049
- const id = this.randomId();
3050
-
3051
- let newScale = 1;
3052
- const curWidth = newScale * image.width;
3053
- if (curWidth > maxWidth) {
3054
- newScale = maxWidth / curWidth;
3055
- }
3056
-
3057
- // Set image properties and add to the canvas
3058
- //const fabricImage = new FabricImage(imageUrl, {
3059
- //const fabricImage = new this.fabric.Image(image, {
3060
- const fabricImage = new FabricImage(image as any, {
3061
- id: id,
3062
- scaleX: newScale,
3063
- scaleY: newScale,
3064
- originX: "left",
3065
- originY: "top",
3066
- crossOrigin: options.crossOrigin,
3067
- src: imageUrl
3068
- } as any);
3069
-
3070
- this.fabricCanvas.add(fabricImage);
3071
-
3072
- const object = new Image(this, fabricImage);
3073
- this.trackNewObject(object);
3074
- this.syncObjectList();
3075
-
3076
- return object;
3077
- } else {
3078
- // Browser-side logic
3079
- return await new Promise((resolve) => {
3080
- this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
3081
- const id = this.randomId();
3082
-
3083
- let newScale = 1;
3084
-
3085
- const curWidth = newScale * fabricImage.width;
3086
- if (curWidth > maxWidth) {
3087
- newScale = maxWidth / curWidth;
3088
- }
3089
-
3090
- fabricImage.set({
3091
- id: id,
3092
- scaleX: newScale,
3093
- scaleY: newScale,
3094
- originX: "left",
3095
- originY: "top",
3096
- } as any);
3097
-
3098
- this.fabricCanvas.add(fabricImage);
3099
-
3100
- const object = new Image(this, fabricImage);
3101
- this.trackNewObject(object);
3102
- this.selectFabricObject(fabricImage);
3103
-
3104
- this.syncObjectList();
3105
-
3106
- resolve(object);
3107
- });
3108
- });
3109
- }
3110
- }*/
3037
+ addImageObject(imageUrl) {
3038
+ return __awaiter(this, void 0, void 0, function* () {
3039
+ // Don't let the image be more than half the canvas in size.
3040
+ const maxSizeScale = 2;
3041
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3042
+ const options = { crossOrigin: "anonymous" };
3043
+ if (this.headless) {
3044
+ // Server-side logic without mock DOM
3045
+ //const canvas = createCanvas(1, 1);
3046
+ //const ctx = canvas.getContext("2d");
3047
+ const image = yield loadImage(imageUrl);
3048
+ const fabricImage = new this.fabric.Image(image, { src: imageUrl, crossOrigin: "anonymous" });
3049
+ const id = this.randomId();
3050
+ let newScale = 1;
3051
+ const curWidth = newScale * fabricImage.width;
3052
+ if (curWidth > maxWidth) {
3053
+ newScale = maxWidth / curWidth;
3054
+ }
3055
+ /*const fabricImage = new this.fabric.Image(image as any, {
3056
+ src: imageUrl,
3057
+ crossOrigin: "anonymous",
3058
+ toObject: function () {
3059
+ return {
3060
+ ...this.callSuper("toObject"),
3061
+ src: this.src,
3062
+ };
3063
+ },
3064
+ });*/
3065
+ // Set image properties and add to the canvas
3066
+ // const fabricImage = new FabricImage(imageUrl, {
3067
+ //const fabricImage = new this.fabric.Image(image, {
3068
+ // const fabricImage = new FabricImage(image as any, {
3069
+ fabricImage.set({
3070
+ id: id,
3071
+ scaleX: newScale,
3072
+ scaleY: newScale,
3073
+ originX: "left",
3074
+ originY: "top"
3075
+ });
3076
+ console.log("headless image-->", fabricImage);
3077
+ this.fabricCanvas.add(fabricImage);
3078
+ const canvasObjectData = new CanvasObjectData(ObjectType.IMAGE, id);
3079
+ canvasObjectData.imagePath = imageUrl;
3080
+ const object = new Image(this, fabricImage, canvasObjectData);
3081
+ this.trackNewObject(object);
3082
+ this.syncObjectList();
3083
+ return object;
3084
+ }
3085
+ else {
3086
+ // Browser-side logic
3087
+ return yield new Promise((resolve) => {
3088
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage) => {
3089
+ const id = this.randomId();
3090
+ let newScale = 1;
3091
+ const curWidth = newScale * fabricImage.width;
3092
+ if (curWidth > maxWidth) {
3093
+ newScale = maxWidth / curWidth;
3094
+ }
3095
+ fabricImage.set({
3096
+ id: id,
3097
+ scaleX: newScale,
3098
+ scaleY: newScale,
3099
+ originX: "left",
3100
+ originY: "top",
3101
+ });
3102
+ this.fabricCanvas.add(fabricImage);
3103
+ const object = new Image(this, fabricImage);
3104
+ this.trackNewObject(object);
3105
+ this.selectFabricObject(fabricImage);
3106
+ this.syncObjectList();
3107
+ resolve(object);
3108
+ });
3109
+ });
3110
+ }
3111
+ });
3112
+ }
3111
3113
  /* public async addImageObject(imageUrl: string): Promise<IImage> {
3112
3114
  const maxSizeScale = 2;
3113
3115
  const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
@@ -3153,42 +3155,86 @@ class AbstractInteractiveCanvas {
3153
3155
 
3154
3156
  return object;
3155
3157
  }*/
3158
+ /* public async addImageObject(imageUrl: string): Promise<IImage> {
3159
+ const maxSizeScale = 2;
3160
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3161
+
3162
+ // 1. Safe Isomorphic Options: Prevent browser configurations from cracking Server runs
3163
+ const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
3164
+ const options = isBrowser ? { crossOrigin: "anonymous" } : {};
3165
+
3166
+ // 2. Fabric v6 Asynchronous Image Loader
3167
+ const fabricImage = await this.fabric.Image.fromURL(imageUrl, options);
3168
+
3169
+ const id = this.randomId();
3170
+ let newScale = 1;
3171
+ const curWidth = newScale * fabricImage.width;
3172
+
3173
+ if (curWidth > maxWidth) {
3174
+ newScale = maxWidth / curWidth;
3175
+ }
3176
+
3177
+ fabricImage.set({
3178
+ id: id,
3179
+ scaleX: newScale,
3180
+ scaleY: newScale,
3181
+ originX: "left",
3182
+ originY: "top"
3183
+ } as any);
3184
+
3185
+ this.fabricCanvas.add(fabricImage);
3186
+
3187
+ // Ensure 'Image' resolves to your internal custom class wrapper, not the native DOM global
3188
+ // If your 'Image' class is a domain wrapper (e.g., CustomImageWrapper), name it distinctly to prevent collision.
3189
+ const object = new (Image as any)(this, fabricImage);
3190
+
3191
+ this.trackNewObject(object);
3192
+ this.selectFabricObject(fabricImage);
3193
+ this.syncObjectList();
3194
+
3195
+ return object;
3196
+ }*/
3156
3197
  /**
3157
3198
  * @override
3158
3199
  * @inheritDoc
3159
3200
  */
3160
- addImageObject(imageUrl) {
3161
- return __awaiter(this, void 0, void 0, function* () {
3162
- //Don't let the image be more than half the canvas in size.
3163
- const maxSizeScale = 2;
3164
- const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3165
- const options = { crossOrigin: "anonymous" };
3166
- return yield new Promise((resolve) => {
3167
- const fabricImage = this.fabric.Image.fromURL(imageUrl, options);
3168
- const id = this.randomId();
3169
- let newScale = 1;
3170
- const curWidth = newScale * fabricImage.width;
3171
- if (curWidth > maxWidth) {
3172
- newScale = maxWidth / curWidth;
3173
- }
3174
- fabricImage.set({
3175
- id: id,
3176
- scaleX: newScale,
3177
- scaleY: newScale,
3178
- originX: "left",
3179
- originY: "top"
3180
- });
3181
- this.fabricCanvas.add(fabricImage);
3182
- const object = new Image(this, fabricImage);
3183
- this.trackNewObject(object);
3184
- this.selectFabricObject(fabricImage);
3185
- this.syncObjectList();
3186
- // resolve(object);
3187
- return object;
3188
- });
3189
- // });
3190
- });
3191
- }
3201
+ /* public async addImageObject(imageUrl: string): Promise<IImage> {
3202
+ //Don't let the image be more than half the canvas in size.
3203
+ const maxSizeScale = 2;
3204
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3205
+
3206
+ const options = {crossOrigin: "anonymous"};
3207
+ return await new Promise((resolve) => {
3208
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
3209
+ const id = this.randomId();
3210
+
3211
+ let newScale = 1;
3212
+
3213
+ const curWidth = newScale * fabricImage.width;
3214
+ if (curWidth > maxWidth) {
3215
+ newScale = maxWidth / curWidth;
3216
+ }
3217
+
3218
+ fabricImage.set({
3219
+ id: id,
3220
+ scaleX: newScale,
3221
+ scaleY: newScale,
3222
+ originX: "left",
3223
+ originY: "top"
3224
+ } as any);
3225
+
3226
+ this.fabricCanvas.add(fabricImage);
3227
+
3228
+ const object = new Image(this, fabricImage);
3229
+ this.trackNewObject(object);
3230
+ this.selectFabricObject(fabricImage);
3231
+
3232
+ this.syncObjectList();
3233
+
3234
+ resolve(object);
3235
+ });
3236
+ });
3237
+ }*/
3192
3238
  /**
3193
3239
  * @override
3194
3240
  * @inheritDoc
@@ -2,6 +2,7 @@
2
2
 
3
3
  var _ = require('lodash');
4
4
  var rxjs = require('rxjs');
5
+ var canvas = require('canvas');
5
6
  var fabric = require('fabric');
6
7
 
7
8
  function _interopNamespaceDefault(e) {
@@ -3054,81 +3055,82 @@ class AbstractInteractiveCanvas {
3054
3055
  this.fabricCanvas.requestRenderAll();
3055
3056
  }
3056
3057
  }
3057
- /* public async addImageObject(imageUrl: string): Promise<IImage> {
3058
- // Don't let the image be more than half the canvas in size.
3059
- const maxSizeScale = 2;
3060
- const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3061
-
3062
- const options = { crossOrigin: "anonymous" };
3063
-
3064
- if (this.headless) {
3065
- // Server-side logic without mock DOM
3066
- //const canvas = createCanvas(1, 1);
3067
- //const ctx = canvas.getContext("2d");
3068
-
3069
- const image = await loadImage(imageUrl);
3070
- const id = this.randomId();
3071
-
3072
- let newScale = 1;
3073
- const curWidth = newScale * image.width;
3074
- if (curWidth > maxWidth) {
3075
- newScale = maxWidth / curWidth;
3076
- }
3077
-
3078
- // Set image properties and add to the canvas
3079
- //const fabricImage = new FabricImage(imageUrl, {
3080
- //const fabricImage = new this.fabric.Image(image, {
3081
- const fabricImage = new FabricImage(image as any, {
3082
- id: id,
3083
- scaleX: newScale,
3084
- scaleY: newScale,
3085
- originX: "left",
3086
- originY: "top",
3087
- crossOrigin: options.crossOrigin,
3088
- src: imageUrl
3089
- } as any);
3090
-
3091
- this.fabricCanvas.add(fabricImage);
3092
-
3093
- const object = new Image(this, fabricImage);
3094
- this.trackNewObject(object);
3095
- this.syncObjectList();
3096
-
3097
- return object;
3098
- } else {
3099
- // Browser-side logic
3100
- return await new Promise((resolve) => {
3101
- this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
3102
- const id = this.randomId();
3103
-
3104
- let newScale = 1;
3105
-
3106
- const curWidth = newScale * fabricImage.width;
3107
- if (curWidth > maxWidth) {
3108
- newScale = maxWidth / curWidth;
3109
- }
3110
-
3111
- fabricImage.set({
3112
- id: id,
3113
- scaleX: newScale,
3114
- scaleY: newScale,
3115
- originX: "left",
3116
- originY: "top",
3117
- } as any);
3118
-
3119
- this.fabricCanvas.add(fabricImage);
3120
-
3121
- const object = new Image(this, fabricImage);
3122
- this.trackNewObject(object);
3123
- this.selectFabricObject(fabricImage);
3124
-
3125
- this.syncObjectList();
3126
-
3127
- resolve(object);
3128
- });
3129
- });
3130
- }
3131
- }*/
3058
+ addImageObject(imageUrl) {
3059
+ return __awaiter(this, void 0, void 0, function* () {
3060
+ // Don't let the image be more than half the canvas in size.
3061
+ const maxSizeScale = 2;
3062
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3063
+ const options = { crossOrigin: "anonymous" };
3064
+ if (this.headless) {
3065
+ // Server-side logic without mock DOM
3066
+ //const canvas = createCanvas(1, 1);
3067
+ //const ctx = canvas.getContext("2d");
3068
+ const image = yield canvas.loadImage(imageUrl);
3069
+ const fabricImage = new this.fabric.Image(image, { src: imageUrl, crossOrigin: "anonymous" });
3070
+ const id = this.randomId();
3071
+ let newScale = 1;
3072
+ const curWidth = newScale * fabricImage.width;
3073
+ if (curWidth > maxWidth) {
3074
+ newScale = maxWidth / curWidth;
3075
+ }
3076
+ /*const fabricImage = new this.fabric.Image(image as any, {
3077
+ src: imageUrl,
3078
+ crossOrigin: "anonymous",
3079
+ toObject: function () {
3080
+ return {
3081
+ ...this.callSuper("toObject"),
3082
+ src: this.src,
3083
+ };
3084
+ },
3085
+ });*/
3086
+ // Set image properties and add to the canvas
3087
+ // const fabricImage = new FabricImage(imageUrl, {
3088
+ //const fabricImage = new this.fabric.Image(image, {
3089
+ // const fabricImage = new FabricImage(image as any, {
3090
+ fabricImage.set({
3091
+ id: id,
3092
+ scaleX: newScale,
3093
+ scaleY: newScale,
3094
+ originX: "left",
3095
+ originY: "top"
3096
+ });
3097
+ console.log("headless image-->", fabricImage);
3098
+ this.fabricCanvas.add(fabricImage);
3099
+ const canvasObjectData = new CanvasObjectData(exports.ObjectType.IMAGE, id);
3100
+ canvasObjectData.imagePath = imageUrl;
3101
+ const object = new Image(this, fabricImage, canvasObjectData);
3102
+ this.trackNewObject(object);
3103
+ this.syncObjectList();
3104
+ return object;
3105
+ }
3106
+ else {
3107
+ // Browser-side logic
3108
+ return yield new Promise((resolve) => {
3109
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage) => {
3110
+ const id = this.randomId();
3111
+ let newScale = 1;
3112
+ const curWidth = newScale * fabricImage.width;
3113
+ if (curWidth > maxWidth) {
3114
+ newScale = maxWidth / curWidth;
3115
+ }
3116
+ fabricImage.set({
3117
+ id: id,
3118
+ scaleX: newScale,
3119
+ scaleY: newScale,
3120
+ originX: "left",
3121
+ originY: "top",
3122
+ });
3123
+ this.fabricCanvas.add(fabricImage);
3124
+ const object = new Image(this, fabricImage);
3125
+ this.trackNewObject(object);
3126
+ this.selectFabricObject(fabricImage);
3127
+ this.syncObjectList();
3128
+ resolve(object);
3129
+ });
3130
+ });
3131
+ }
3132
+ });
3133
+ }
3132
3134
  /* public async addImageObject(imageUrl: string): Promise<IImage> {
3133
3135
  const maxSizeScale = 2;
3134
3136
  const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
@@ -3174,42 +3176,86 @@ class AbstractInteractiveCanvas {
3174
3176
 
3175
3177
  return object;
3176
3178
  }*/
3179
+ /* public async addImageObject(imageUrl: string): Promise<IImage> {
3180
+ const maxSizeScale = 2;
3181
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3182
+
3183
+ // 1. Safe Isomorphic Options: Prevent browser configurations from cracking Server runs
3184
+ const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined';
3185
+ const options = isBrowser ? { crossOrigin: "anonymous" } : {};
3186
+
3187
+ // 2. Fabric v6 Asynchronous Image Loader
3188
+ const fabricImage = await this.fabric.Image.fromURL(imageUrl, options);
3189
+
3190
+ const id = this.randomId();
3191
+ let newScale = 1;
3192
+ const curWidth = newScale * fabricImage.width;
3193
+
3194
+ if (curWidth > maxWidth) {
3195
+ newScale = maxWidth / curWidth;
3196
+ }
3197
+
3198
+ fabricImage.set({
3199
+ id: id,
3200
+ scaleX: newScale,
3201
+ scaleY: newScale,
3202
+ originX: "left",
3203
+ originY: "top"
3204
+ } as any);
3205
+
3206
+ this.fabricCanvas.add(fabricImage);
3207
+
3208
+ // Ensure 'Image' resolves to your internal custom class wrapper, not the native DOM global
3209
+ // If your 'Image' class is a domain wrapper (e.g., CustomImageWrapper), name it distinctly to prevent collision.
3210
+ const object = new (Image as any)(this, fabricImage);
3211
+
3212
+ this.trackNewObject(object);
3213
+ this.selectFabricObject(fabricImage);
3214
+ this.syncObjectList();
3215
+
3216
+ return object;
3217
+ }*/
3177
3218
  /**
3178
3219
  * @override
3179
3220
  * @inheritDoc
3180
3221
  */
3181
- addImageObject(imageUrl) {
3182
- return __awaiter(this, void 0, void 0, function* () {
3183
- //Don't let the image be more than half the canvas in size.
3184
- const maxSizeScale = 2;
3185
- const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3186
- const options = { crossOrigin: "anonymous" };
3187
- return yield new Promise((resolve) => {
3188
- const fabricImage = this.fabric.Image.fromURL(imageUrl, options);
3189
- const id = this.randomId();
3190
- let newScale = 1;
3191
- const curWidth = newScale * fabricImage.width;
3192
- if (curWidth > maxWidth) {
3193
- newScale = maxWidth / curWidth;
3194
- }
3195
- fabricImage.set({
3196
- id: id,
3197
- scaleX: newScale,
3198
- scaleY: newScale,
3199
- originX: "left",
3200
- originY: "top"
3201
- });
3202
- this.fabricCanvas.add(fabricImage);
3203
- const object = new Image(this, fabricImage);
3204
- this.trackNewObject(object);
3205
- this.selectFabricObject(fabricImage);
3206
- this.syncObjectList();
3207
- // resolve(object);
3208
- return object;
3209
- });
3210
- // });
3211
- });
3212
- }
3222
+ /* public async addImageObject(imageUrl: string): Promise<IImage> {
3223
+ //Don't let the image be more than half the canvas in size.
3224
+ const maxSizeScale = 2;
3225
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3226
+
3227
+ const options = {crossOrigin: "anonymous"};
3228
+ return await new Promise((resolve) => {
3229
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
3230
+ const id = this.randomId();
3231
+
3232
+ let newScale = 1;
3233
+
3234
+ const curWidth = newScale * fabricImage.width;
3235
+ if (curWidth > maxWidth) {
3236
+ newScale = maxWidth / curWidth;
3237
+ }
3238
+
3239
+ fabricImage.set({
3240
+ id: id,
3241
+ scaleX: newScale,
3242
+ scaleY: newScale,
3243
+ originX: "left",
3244
+ originY: "top"
3245
+ } as any);
3246
+
3247
+ this.fabricCanvas.add(fabricImage);
3248
+
3249
+ const object = new Image(this, fabricImage);
3250
+ this.trackNewObject(object);
3251
+ this.selectFabricObject(fabricImage);
3252
+
3253
+ this.syncObjectList();
3254
+
3255
+ resolve(object);
3256
+ });
3257
+ });
3258
+ }*/
3213
3259
  /**
3214
3260
  * @override
3215
3261
  * @inheritDoc
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leapfrog/interactive-canvas",
3
- "version": "2.0.23-beta-6",
3
+ "version": "2.0.23-beta-8",
4
4
  "description": "Leapfrog interactive canvas",
5
5
  "files": [
6
6
  "dist"