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

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