@leapfrog/interactive-canvas 2.0.23-beta-1 → 2.0.23-beta-2

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.
@@ -203,6 +203,7 @@ export declare abstract class AbstractInteractiveCanvas implements IInteractiveC
203
203
  */
204
204
  sendObjectToBack(target: FabricObject): void;
205
205
  addImageObject(imageUrl: string): Promise<IImage>;
206
+ private createFabricImage;
206
207
  /**
207
208
  * @override
208
209
  * @inheritDoc
@@ -1,7 +1,8 @@
1
1
  import _, { round, each, values, reduce, clone, max, isEqual } from 'lodash';
2
2
  import { BehaviorSubject, Subject } from 'rxjs';
3
- import { createCanvas, loadImage } from 'canvas';
4
3
  import * as fabric from 'fabric';
4
+ import { FabricImage } from 'fabric';
5
+ import { loadImage } from 'canvas';
5
6
 
6
7
  const PIXELS_PER_MM$2 = 3.937;
7
8
  class MillimeterDimensions {
@@ -3034,66 +3035,118 @@ class AbstractInteractiveCanvas {
3034
3035
  this.fabricCanvas.requestRenderAll();
3035
3036
  }
3036
3037
  }
3038
+ /*public async addImageObject(imageUrl: string): Promise<IImage> {
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
+
3043
+ const options = { crossOrigin: "anonymous" };
3044
+
3045
+ if (this.headless) {
3046
+ // Server-side logic without mock DOM
3047
+ const canvas = createCanvas(1, 1);
3048
+ //const ctx = canvas.getContext("2d");
3049
+
3050
+ const image = await loadImage(imageUrl);
3051
+ const id = this.randomId();
3052
+
3053
+ let newScale = 1;
3054
+ const curWidth = newScale * image.width;
3055
+ if (curWidth > maxWidth) {
3056
+ newScale = maxWidth / curWidth;
3057
+ }
3058
+
3059
+ // Set image properties and add to the canvas
3060
+ const fabricImage = new this.fabric.Image(image, {
3061
+ id: id,
3062
+ scaleX: newScale,
3063
+ scaleY: newScale,
3064
+ originX: "left",
3065
+ originY: "top",
3066
+ });
3067
+
3068
+ this.fabricCanvas.add(fabricImage);
3069
+
3070
+ const object = new Image(this, fabricImage);
3071
+ this.trackNewObject(object);
3072
+ this.syncObjectList();
3073
+
3074
+ return object;
3075
+ } else {
3076
+ // Browser-side logic
3077
+ return await new Promise((resolve) => {
3078
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
3079
+ const id = this.randomId();
3080
+
3081
+ let newScale = 1;
3082
+
3083
+ const curWidth = newScale * fabricImage.width;
3084
+ if (curWidth > maxWidth) {
3085
+ newScale = maxWidth / curWidth;
3086
+ }
3087
+
3088
+ fabricImage.set({
3089
+ id: id,
3090
+ scaleX: newScale,
3091
+ scaleY: newScale,
3092
+ originX: "left",
3093
+ originY: "top",
3094
+ } as any);
3095
+
3096
+ this.fabricCanvas.add(fabricImage);
3097
+
3098
+ const object = new Image(this, fabricImage);
3099
+ this.trackNewObject(object);
3100
+ this.selectFabricObject(fabricImage);
3101
+
3102
+ this.syncObjectList();
3103
+
3104
+ resolve(object);
3105
+ });
3106
+ });
3107
+ }
3108
+ }*/
3037
3109
  addImageObject(imageUrl) {
3038
3110
  return __awaiter(this, void 0, void 0, function* () {
3039
- // Don't let the image be more than half the canvas in size.
3040
3111
  const maxSizeScale = 2;
3041
3112
  const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3042
3113
  const options = { crossOrigin: "anonymous" };
3043
3114
  if (this.headless) {
3044
- // Server-side logic without mock DOM
3045
- //const { createCanvas, loadImage } = require("canvas"); // Use node-canvas
3046
- const canvas = createCanvas(1, 1); // Temporary canvas for image processing
3047
- canvas.getContext("2d");
3048
3115
  const image = yield loadImage(imageUrl);
3049
- const id = this.randomId();
3050
- let newScale = 1;
3051
- const curWidth = newScale * image.width;
3052
- if (curWidth > maxWidth) {
3053
- newScale = maxWidth / curWidth;
3054
- }
3055
- // Set image properties and add to the canvas
3056
- const fabricImage = new this.fabric.Image(image, {
3057
- id: id,
3058
- scaleX: newScale,
3059
- scaleY: newScale,
3060
- originX: "left",
3061
- originY: "top",
3062
- });
3063
- this.fabricCanvas.add(fabricImage);
3064
- const object = new Image(this, fabricImage);
3065
- this.trackNewObject(object);
3066
- this.syncObjectList();
3067
- return object;
3116
+ console.log("image-->", image);
3117
+ console.log("Loaded image:", imageUrl, "Width:", image.width, "Height:", image.height);
3118
+ return this.createFabricImage(image, maxWidth);
3068
3119
  }
3069
3120
  else {
3070
- // Browser-side logic
3071
3121
  return yield new Promise((resolve) => {
3072
3122
  this.fabric.Image.fromURL(imageUrl, options).then((fabricImage) => {
3073
- const id = this.randomId();
3074
- let newScale = 1;
3075
- const curWidth = newScale * fabricImage.width;
3076
- if (curWidth > maxWidth) {
3077
- newScale = maxWidth / curWidth;
3078
- }
3079
- fabricImage.set({
3080
- id: id,
3081
- scaleX: newScale,
3082
- scaleY: newScale,
3083
- originX: "left",
3084
- originY: "top",
3085
- });
3086
- this.fabricCanvas.add(fabricImage);
3087
- const object = new Image(this, fabricImage);
3088
- this.trackNewObject(object);
3089
- this.selectFabricObject(fabricImage);
3090
- this.syncObjectList();
3091
- resolve(object);
3123
+ resolve(this.createFabricImage(fabricImage, maxWidth));
3092
3124
  });
3093
3125
  });
3094
3126
  }
3095
3127
  });
3096
3128
  }
3129
+ createFabricImage(image, maxWidth) {
3130
+ const id = this.randomId();
3131
+ console.log("image inside createfabricimage-->", image);
3132
+ let newScale = 1;
3133
+ const curWidth = newScale * image.width;
3134
+ if (curWidth > maxWidth) {
3135
+ newScale = maxWidth / curWidth;
3136
+ }
3137
+ const fabricImage = new FabricImage(image, {
3138
+ id: id,
3139
+ scaleX: newScale,
3140
+ scaleY: newScale,
3141
+ originX: "left",
3142
+ originY: "top",
3143
+ });
3144
+ this.fabricCanvas.add(fabricImage);
3145
+ const object = new Image(this, fabricImage);
3146
+ this.trackNewObject(object);
3147
+ this.syncObjectList();
3148
+ return object;
3149
+ }
3097
3150
  /**
3098
3151
  * @override
3099
3152
  * @inheritDoc
@@ -2,8 +2,8 @@
2
2
 
3
3
  var _ = require('lodash');
4
4
  var rxjs = require('rxjs');
5
- var canvas = require('canvas');
6
5
  var fabric = require('fabric');
6
+ var canvas = require('canvas');
7
7
 
8
8
  function _interopNamespaceDefault(e) {
9
9
  var n = Object.create(null);
@@ -3055,66 +3055,118 @@ class AbstractInteractiveCanvas {
3055
3055
  this.fabricCanvas.requestRenderAll();
3056
3056
  }
3057
3057
  }
3058
+ /*public async addImageObject(imageUrl: string): Promise<IImage> {
3059
+ // Don't let the image be more than half the canvas in size.
3060
+ const maxSizeScale = 2;
3061
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3062
+
3063
+ const options = { crossOrigin: "anonymous" };
3064
+
3065
+ if (this.headless) {
3066
+ // Server-side logic without mock DOM
3067
+ const canvas = createCanvas(1, 1);
3068
+ //const ctx = canvas.getContext("2d");
3069
+
3070
+ const image = await loadImage(imageUrl);
3071
+ const id = this.randomId();
3072
+
3073
+ let newScale = 1;
3074
+ const curWidth = newScale * image.width;
3075
+ if (curWidth > maxWidth) {
3076
+ newScale = maxWidth / curWidth;
3077
+ }
3078
+
3079
+ // Set image properties and add to the canvas
3080
+ const fabricImage = new this.fabric.Image(image, {
3081
+ id: id,
3082
+ scaleX: newScale,
3083
+ scaleY: newScale,
3084
+ originX: "left",
3085
+ originY: "top",
3086
+ });
3087
+
3088
+ this.fabricCanvas.add(fabricImage);
3089
+
3090
+ const object = new Image(this, fabricImage);
3091
+ this.trackNewObject(object);
3092
+ this.syncObjectList();
3093
+
3094
+ return object;
3095
+ } else {
3096
+ // Browser-side logic
3097
+ return await new Promise((resolve) => {
3098
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
3099
+ const id = this.randomId();
3100
+
3101
+ let newScale = 1;
3102
+
3103
+ const curWidth = newScale * fabricImage.width;
3104
+ if (curWidth > maxWidth) {
3105
+ newScale = maxWidth / curWidth;
3106
+ }
3107
+
3108
+ fabricImage.set({
3109
+ id: id,
3110
+ scaleX: newScale,
3111
+ scaleY: newScale,
3112
+ originX: "left",
3113
+ originY: "top",
3114
+ } as any);
3115
+
3116
+ this.fabricCanvas.add(fabricImage);
3117
+
3118
+ const object = new Image(this, fabricImage);
3119
+ this.trackNewObject(object);
3120
+ this.selectFabricObject(fabricImage);
3121
+
3122
+ this.syncObjectList();
3123
+
3124
+ resolve(object);
3125
+ });
3126
+ });
3127
+ }
3128
+ }*/
3058
3129
  addImageObject(imageUrl) {
3059
3130
  return __awaiter(this, void 0, void 0, function* () {
3060
- // Don't let the image be more than half the canvas in size.
3061
3131
  const maxSizeScale = 2;
3062
3132
  const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3063
3133
  const options = { crossOrigin: "anonymous" };
3064
3134
  if (this.headless) {
3065
- // Server-side logic without mock DOM
3066
- //const { createCanvas, loadImage } = require("canvas"); // Use node-canvas
3067
- const canvas$1 = canvas.createCanvas(1, 1); // Temporary canvas for image processing
3068
- canvas$1.getContext("2d");
3069
3135
  const image = yield canvas.loadImage(imageUrl);
3070
- const id = this.randomId();
3071
- let newScale = 1;
3072
- const curWidth = newScale * image.width;
3073
- if (curWidth > maxWidth) {
3074
- newScale = maxWidth / curWidth;
3075
- }
3076
- // Set image properties and add to the canvas
3077
- const fabricImage = new this.fabric.Image(image, {
3078
- id: id,
3079
- scaleX: newScale,
3080
- scaleY: newScale,
3081
- originX: "left",
3082
- originY: "top",
3083
- });
3084
- this.fabricCanvas.add(fabricImage);
3085
- const object = new Image(this, fabricImage);
3086
- this.trackNewObject(object);
3087
- this.syncObjectList();
3088
- return object;
3136
+ console.log("image-->", image);
3137
+ console.log("Loaded image:", imageUrl, "Width:", image.width, "Height:", image.height);
3138
+ return this.createFabricImage(image, maxWidth);
3089
3139
  }
3090
3140
  else {
3091
- // Browser-side logic
3092
3141
  return yield new Promise((resolve) => {
3093
3142
  this.fabric.Image.fromURL(imageUrl, options).then((fabricImage) => {
3094
- const id = this.randomId();
3095
- let newScale = 1;
3096
- const curWidth = newScale * fabricImage.width;
3097
- if (curWidth > maxWidth) {
3098
- newScale = maxWidth / curWidth;
3099
- }
3100
- fabricImage.set({
3101
- id: id,
3102
- scaleX: newScale,
3103
- scaleY: newScale,
3104
- originX: "left",
3105
- originY: "top",
3106
- });
3107
- this.fabricCanvas.add(fabricImage);
3108
- const object = new Image(this, fabricImage);
3109
- this.trackNewObject(object);
3110
- this.selectFabricObject(fabricImage);
3111
- this.syncObjectList();
3112
- resolve(object);
3143
+ resolve(this.createFabricImage(fabricImage, maxWidth));
3113
3144
  });
3114
3145
  });
3115
3146
  }
3116
3147
  });
3117
3148
  }
3149
+ createFabricImage(image, maxWidth) {
3150
+ const id = this.randomId();
3151
+ console.log("image inside createfabricimage-->", image);
3152
+ let newScale = 1;
3153
+ const curWidth = newScale * image.width;
3154
+ if (curWidth > maxWidth) {
3155
+ newScale = maxWidth / curWidth;
3156
+ }
3157
+ const fabricImage = new fabric.FabricImage(image, {
3158
+ id: id,
3159
+ scaleX: newScale,
3160
+ scaleY: newScale,
3161
+ originX: "left",
3162
+ originY: "top",
3163
+ });
3164
+ this.fabricCanvas.add(fabricImage);
3165
+ const object = new Image(this, fabricImage);
3166
+ this.trackNewObject(object);
3167
+ this.syncObjectList();
3168
+ return object;
3169
+ }
3118
3170
  /**
3119
3171
  * @override
3120
3172
  * @inheritDoc
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leapfrog/interactive-canvas",
3
- "version": "2.0.23-beta-1",
3
+ "version": "2.0.23-beta-2",
4
4
  "description": "Leapfrog interactive canvas",
5
5
  "files": [
6
6
  "dist"