@leapfrog/interactive-canvas 2.0.22-beta-9 → 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.
@@ -202,11 +202,12 @@ export declare abstract class AbstractInteractiveCanvas implements IInteractiveC
202
202
  * @inheritDoc
203
203
  */
204
204
  sendObjectToBack(target: FabricObject): void;
205
+ addImageObject(imageUrl: string): Promise<IImage>;
206
+ private createFabricImage;
205
207
  /**
206
208
  * @override
207
209
  * @inheritDoc
208
210
  */
209
- addImageObject(imageUrl: string): Promise<IImage>;
210
211
  /**
211
212
  * @override
212
213
  * @inheritDoc
@@ -1,6 +1,8 @@
1
1
  import _, { round, each, values, reduce, clone, max, isEqual } from 'lodash';
2
2
  import { BehaviorSubject, Subject } from 'rxjs';
3
3
  import * as fabric from 'fabric';
4
+ import { FabricImage } from 'fabric';
5
+ import { loadImage } from 'canvas';
4
6
 
5
7
  const PIXELS_PER_MM$2 = 3.937;
6
8
  class MillimeterDimensions {
@@ -3033,41 +3035,160 @@ class AbstractInteractiveCanvas {
3033
3035
  this.fabricCanvas.requestRenderAll();
3034
3036
  }
3035
3037
  }
3036
- /**
3037
- * @override
3038
- * @inheritDoc
3039
- */
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
+ }*/
3040
3109
  addImageObject(imageUrl) {
3041
3110
  return __awaiter(this, void 0, void 0, function* () {
3042
- //Don't let the image be more than half the canvas in size.
3043
3111
  const maxSizeScale = 2;
3044
3112
  const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3045
3113
  const options = { crossOrigin: "anonymous" };
3046
- return yield new Promise((resolve) => {
3047
- this.fabric.Image.fromURL(imageUrl, options).then((fabricImage) => {
3048
- const id = this.randomId();
3049
- let newScale = 1;
3050
- const curWidth = newScale * fabricImage.width;
3051
- if (curWidth > maxWidth) {
3052
- newScale = maxWidth / curWidth;
3053
- }
3054
- fabricImage.set({
3055
- id: id,
3056
- scaleX: newScale,
3057
- scaleY: newScale,
3058
- originX: "left",
3059
- originY: "top"
3114
+ if (this.headless) {
3115
+ const image = yield loadImage(imageUrl);
3116
+ console.log("image-->", image);
3117
+ console.log("Loaded image:", imageUrl, "Width:", image.width, "Height:", image.height);
3118
+ return this.createFabricImage(image, maxWidth);
3119
+ }
3120
+ else {
3121
+ return yield new Promise((resolve) => {
3122
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage) => {
3123
+ resolve(this.createFabricImage(fabricImage, maxWidth));
3060
3124
  });
3061
- this.fabricCanvas.add(fabricImage);
3062
- const object = new Image(this, fabricImage);
3063
- this.trackNewObject(object);
3064
- this.selectFabricObject(fabricImage);
3065
- this.syncObjectList();
3066
- resolve(object);
3067
3125
  });
3068
- });
3126
+ }
3069
3127
  });
3070
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
+ }
3150
+ /**
3151
+ * @override
3152
+ * @inheritDoc
3153
+ */
3154
+ /* public async addImageObject(imageUrl: string): Promise<IImage> {
3155
+ //Don't let the image be more than half the canvas in size.
3156
+ const maxSizeScale = 2;
3157
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3158
+
3159
+ const options = {crossOrigin: "anonymous"};
3160
+ return await new Promise((resolve) => {
3161
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
3162
+ const id = this.randomId();
3163
+
3164
+ let newScale = 1;
3165
+
3166
+ const curWidth = newScale * fabricImage.width;
3167
+ if (curWidth > maxWidth) {
3168
+ newScale = maxWidth / curWidth;
3169
+ }
3170
+
3171
+ fabricImage.set({
3172
+ id: id,
3173
+ scaleX: newScale,
3174
+ scaleY: newScale,
3175
+ originX: "left",
3176
+ originY: "top"
3177
+ } as any);
3178
+
3179
+ this.fabricCanvas.add(fabricImage);
3180
+
3181
+ const object = new Image(this, fabricImage);
3182
+ this.trackNewObject(object);
3183
+ this.selectFabricObject(fabricImage);
3184
+
3185
+ this.syncObjectList();
3186
+
3187
+ resolve(object);
3188
+ });
3189
+ });
3190
+ }
3191
+ */
3071
3192
  /**
3072
3193
  * @override
3073
3194
  * @inheritDoc
@@ -3,6 +3,7 @@
3
3
  var _ = require('lodash');
4
4
  var rxjs = require('rxjs');
5
5
  var fabric = require('fabric');
6
+ var canvas = require('canvas');
6
7
 
7
8
  function _interopNamespaceDefault(e) {
8
9
  var n = Object.create(null);
@@ -3054,41 +3055,160 @@ class AbstractInteractiveCanvas {
3054
3055
  this.fabricCanvas.requestRenderAll();
3055
3056
  }
3056
3057
  }
3057
- /**
3058
- * @override
3059
- * @inheritDoc
3060
- */
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
+ }*/
3061
3129
  addImageObject(imageUrl) {
3062
3130
  return __awaiter(this, void 0, void 0, function* () {
3063
- //Don't let the image be more than half the canvas in size.
3064
3131
  const maxSizeScale = 2;
3065
3132
  const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3066
3133
  const options = { crossOrigin: "anonymous" };
3067
- return yield new Promise((resolve) => {
3068
- this.fabric.Image.fromURL(imageUrl, options).then((fabricImage) => {
3069
- const id = this.randomId();
3070
- let newScale = 1;
3071
- const curWidth = newScale * fabricImage.width;
3072
- if (curWidth > maxWidth) {
3073
- newScale = maxWidth / curWidth;
3074
- }
3075
- fabricImage.set({
3076
- id: id,
3077
- scaleX: newScale,
3078
- scaleY: newScale,
3079
- originX: "left",
3080
- originY: "top"
3134
+ if (this.headless) {
3135
+ const image = yield canvas.loadImage(imageUrl);
3136
+ console.log("image-->", image);
3137
+ console.log("Loaded image:", imageUrl, "Width:", image.width, "Height:", image.height);
3138
+ return this.createFabricImage(image, maxWidth);
3139
+ }
3140
+ else {
3141
+ return yield new Promise((resolve) => {
3142
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage) => {
3143
+ resolve(this.createFabricImage(fabricImage, maxWidth));
3081
3144
  });
3082
- this.fabricCanvas.add(fabricImage);
3083
- const object = new Image(this, fabricImage);
3084
- this.trackNewObject(object);
3085
- this.selectFabricObject(fabricImage);
3086
- this.syncObjectList();
3087
- resolve(object);
3088
3145
  });
3089
- });
3146
+ }
3090
3147
  });
3091
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
+ }
3170
+ /**
3171
+ * @override
3172
+ * @inheritDoc
3173
+ */
3174
+ /* public async addImageObject(imageUrl: string): Promise<IImage> {
3175
+ //Don't let the image be more than half the canvas in size.
3176
+ const maxSizeScale = 2;
3177
+ const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
3178
+
3179
+ const options = {crossOrigin: "anonymous"};
3180
+ return await new Promise((resolve) => {
3181
+ this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
3182
+ const id = this.randomId();
3183
+
3184
+ let newScale = 1;
3185
+
3186
+ const curWidth = newScale * fabricImage.width;
3187
+ if (curWidth > maxWidth) {
3188
+ newScale = maxWidth / curWidth;
3189
+ }
3190
+
3191
+ fabricImage.set({
3192
+ id: id,
3193
+ scaleX: newScale,
3194
+ scaleY: newScale,
3195
+ originX: "left",
3196
+ originY: "top"
3197
+ } as any);
3198
+
3199
+ this.fabricCanvas.add(fabricImage);
3200
+
3201
+ const object = new Image(this, fabricImage);
3202
+ this.trackNewObject(object);
3203
+ this.selectFabricObject(fabricImage);
3204
+
3205
+ this.syncObjectList();
3206
+
3207
+ resolve(object);
3208
+ });
3209
+ });
3210
+ }
3211
+ */
3092
3212
  /**
3093
3213
  * @override
3094
3214
  * @inheritDoc
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leapfrog/interactive-canvas",
3
- "version": "2.0.22-beta-9",
3
+ "version": "2.0.23-beta-2",
4
4
  "description": "Leapfrog interactive canvas",
5
5
  "files": [
6
6
  "dist"
@@ -18,6 +18,7 @@
18
18
  "typescript": "^6.0.3"
19
19
  },
20
20
  "dependencies": {
21
+ "canvas": "^3.2.3",
21
22
  "fabric": "^7.2.0",
22
23
  "lodash": "^4.18.1",
23
24
  "rollup": "^4.60.3",