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