@leapfrog/interactive-canvas 2.0.22-beta-9 → 2.0.23-beta-1
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 { createCanvas, loadImage } from 'canvas';
|
|
3
4
|
import * as fabric from 'fabric';
|
|
4
5
|
|
|
5
6
|
const PIXELS_PER_MM$2 = 3.937;
|
|
@@ -3033,41 +3034,108 @@ class AbstractInteractiveCanvas {
|
|
|
3033
3034
|
this.fabricCanvas.requestRenderAll();
|
|
3034
3035
|
}
|
|
3035
3036
|
}
|
|
3036
|
-
/**
|
|
3037
|
-
* @override
|
|
3038
|
-
* @inheritDoc
|
|
3039
|
-
*/
|
|
3040
3037
|
addImageObject(imageUrl) {
|
|
3041
3038
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3042
|
-
//Don't let the image be more than half the canvas in size.
|
|
3039
|
+
// Don't let the image be more than half the canvas in size.
|
|
3043
3040
|
const maxSizeScale = 2;
|
|
3044
3041
|
const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
|
|
3045
3042
|
const options = { crossOrigin: "anonymous" };
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
|
|
3050
|
-
|
|
3051
|
-
|
|
3052
|
-
|
|
3053
|
-
|
|
3054
|
-
|
|
3055
|
-
|
|
3056
|
-
|
|
3057
|
-
|
|
3058
|
-
|
|
3059
|
-
|
|
3043
|
+
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
|
+
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;
|
|
3068
|
+
}
|
|
3069
|
+
else {
|
|
3070
|
+
// Browser-side logic
|
|
3071
|
+
return yield new Promise((resolve) => {
|
|
3072
|
+
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);
|
|
3060
3092
|
});
|
|
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
3093
|
});
|
|
3068
|
-
}
|
|
3094
|
+
}
|
|
3069
3095
|
});
|
|
3070
3096
|
}
|
|
3097
|
+
/**
|
|
3098
|
+
* @override
|
|
3099
|
+
* @inheritDoc
|
|
3100
|
+
*/
|
|
3101
|
+
/* public async addImageObject(imageUrl: string): Promise<IImage> {
|
|
3102
|
+
//Don't let the image be more than half the canvas in size.
|
|
3103
|
+
const maxSizeScale = 2;
|
|
3104
|
+
const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
|
|
3105
|
+
|
|
3106
|
+
const options = {crossOrigin: "anonymous"};
|
|
3107
|
+
return await new Promise((resolve) => {
|
|
3108
|
+
this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
|
|
3109
|
+
const id = this.randomId();
|
|
3110
|
+
|
|
3111
|
+
let newScale = 1;
|
|
3112
|
+
|
|
3113
|
+
const curWidth = newScale * fabricImage.width;
|
|
3114
|
+
if (curWidth > maxWidth) {
|
|
3115
|
+
newScale = maxWidth / curWidth;
|
|
3116
|
+
}
|
|
3117
|
+
|
|
3118
|
+
fabricImage.set({
|
|
3119
|
+
id: id,
|
|
3120
|
+
scaleX: newScale,
|
|
3121
|
+
scaleY: newScale,
|
|
3122
|
+
originX: "left",
|
|
3123
|
+
originY: "top"
|
|
3124
|
+
} as any);
|
|
3125
|
+
|
|
3126
|
+
this.fabricCanvas.add(fabricImage);
|
|
3127
|
+
|
|
3128
|
+
const object = new Image(this, fabricImage);
|
|
3129
|
+
this.trackNewObject(object);
|
|
3130
|
+
this.selectFabricObject(fabricImage);
|
|
3131
|
+
|
|
3132
|
+
this.syncObjectList();
|
|
3133
|
+
|
|
3134
|
+
resolve(object);
|
|
3135
|
+
});
|
|
3136
|
+
});
|
|
3137
|
+
}
|
|
3138
|
+
*/
|
|
3071
3139
|
/**
|
|
3072
3140
|
* @override
|
|
3073
3141
|
* @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,41 +3055,108 @@ class AbstractInteractiveCanvas {
|
|
|
3054
3055
|
this.fabricCanvas.requestRenderAll();
|
|
3055
3056
|
}
|
|
3056
3057
|
}
|
|
3057
|
-
/**
|
|
3058
|
-
* @override
|
|
3059
|
-
* @inheritDoc
|
|
3060
|
-
*/
|
|
3061
3058
|
addImageObject(imageUrl) {
|
|
3062
3059
|
return __awaiter(this, void 0, void 0, function* () {
|
|
3063
|
-
//Don't let the image be more than half the canvas in size.
|
|
3060
|
+
// Don't let the image be more than half the canvas in size.
|
|
3064
3061
|
const maxSizeScale = 2;
|
|
3065
3062
|
const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
|
|
3066
3063
|
const options = { crossOrigin: "anonymous" };
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
3075
|
-
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3064
|
+
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
|
+
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;
|
|
3089
|
+
}
|
|
3090
|
+
else {
|
|
3091
|
+
// Browser-side logic
|
|
3092
|
+
return yield new Promise((resolve) => {
|
|
3093
|
+
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);
|
|
3081
3113
|
});
|
|
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
3114
|
});
|
|
3089
|
-
}
|
|
3115
|
+
}
|
|
3090
3116
|
});
|
|
3091
3117
|
}
|
|
3118
|
+
/**
|
|
3119
|
+
* @override
|
|
3120
|
+
* @inheritDoc
|
|
3121
|
+
*/
|
|
3122
|
+
/* public async addImageObject(imageUrl: string): Promise<IImage> {
|
|
3123
|
+
//Don't let the image be more than half the canvas in size.
|
|
3124
|
+
const maxSizeScale = 2;
|
|
3125
|
+
const maxWidth = this.dimensions$.value.widthPx / maxSizeScale;
|
|
3126
|
+
|
|
3127
|
+
const options = {crossOrigin: "anonymous"};
|
|
3128
|
+
return await new Promise((resolve) => {
|
|
3129
|
+
this.fabric.Image.fromURL(imageUrl, options).then((fabricImage: FabricImage) => {
|
|
3130
|
+
const id = this.randomId();
|
|
3131
|
+
|
|
3132
|
+
let newScale = 1;
|
|
3133
|
+
|
|
3134
|
+
const curWidth = newScale * fabricImage.width;
|
|
3135
|
+
if (curWidth > maxWidth) {
|
|
3136
|
+
newScale = maxWidth / curWidth;
|
|
3137
|
+
}
|
|
3138
|
+
|
|
3139
|
+
fabricImage.set({
|
|
3140
|
+
id: id,
|
|
3141
|
+
scaleX: newScale,
|
|
3142
|
+
scaleY: newScale,
|
|
3143
|
+
originX: "left",
|
|
3144
|
+
originY: "top"
|
|
3145
|
+
} as any);
|
|
3146
|
+
|
|
3147
|
+
this.fabricCanvas.add(fabricImage);
|
|
3148
|
+
|
|
3149
|
+
const object = new Image(this, fabricImage);
|
|
3150
|
+
this.trackNewObject(object);
|
|
3151
|
+
this.selectFabricObject(fabricImage);
|
|
3152
|
+
|
|
3153
|
+
this.syncObjectList();
|
|
3154
|
+
|
|
3155
|
+
resolve(object);
|
|
3156
|
+
});
|
|
3157
|
+
});
|
|
3158
|
+
}
|
|
3159
|
+
*/
|
|
3092
3160
|
/**
|
|
3093
3161
|
* @override
|
|
3094
3162
|
* @inheritDoc
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leapfrog/interactive-canvas",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.23-beta-1",
|
|
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",
|