@pooder/kit 4.1.0 → 4.2.0
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.
- package/.test-dist/src/CanvasService.js +83 -0
- package/.test-dist/src/ViewportSystem.js +75 -0
- package/.test-dist/src/background.js +203 -0
- package/.test-dist/src/constraints.js +153 -0
- package/.test-dist/src/coordinate.js +74 -0
- package/.test-dist/src/dieline.js +758 -0
- package/.test-dist/src/feature.js +687 -0
- package/.test-dist/src/featureComplete.js +31 -0
- package/.test-dist/src/featureDraft.js +31 -0
- package/.test-dist/src/film.js +167 -0
- package/.test-dist/src/geometry.js +292 -0
- package/.test-dist/src/image.js +421 -0
- package/.test-dist/src/index.js +31 -0
- package/.test-dist/src/mirror.js +104 -0
- package/.test-dist/src/ruler.js +383 -0
- package/.test-dist/src/tracer.js +448 -0
- package/.test-dist/src/units.js +30 -0
- package/.test-dist/src/white-ink.js +310 -0
- package/.test-dist/tests/run.js +60 -0
- package/CHANGELOG.md +6 -0
- package/dist/index.d.mts +50 -5
- package/dist/index.d.ts +50 -5
- package/dist/index.js +544 -297
- package/dist/index.mjs +541 -296
- package/package.json +3 -2
- package/src/CanvasService.ts +7 -0
- package/src/ViewportSystem.ts +92 -0
- package/src/constraints.ts +53 -4
- package/src/dieline.ts +169 -85
- package/src/feature.ts +217 -150
- package/src/featureComplete.ts +45 -0
- package/src/index.ts +1 -0
- package/src/ruler.ts +26 -18
- package/src/units.ts +27 -0
- package/tests/run.ts +81 -0
- package/tsconfig.test.json +15 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateFeaturesStrict = validateFeaturesStrict;
|
|
4
|
+
exports.completeFeaturesStrict = completeFeaturesStrict;
|
|
5
|
+
const constraints_1 = require("./constraints");
|
|
6
|
+
function validateFeaturesStrict(features, context) {
|
|
7
|
+
const eps = 1e-6;
|
|
8
|
+
const issues = [];
|
|
9
|
+
for (const f of features) {
|
|
10
|
+
if (!f.constraints?.type)
|
|
11
|
+
continue;
|
|
12
|
+
const constrained = constraints_1.ConstraintRegistry.apply(f.x, f.y, f, context);
|
|
13
|
+
if (Math.abs(constrained.x - f.x) > eps ||
|
|
14
|
+
Math.abs(constrained.y - f.y) > eps) {
|
|
15
|
+
issues.push({
|
|
16
|
+
featureId: f.id,
|
|
17
|
+
groupId: f.groupId,
|
|
18
|
+
reason: "Position violates constraint strategy",
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return { ok: issues.length === 0, issues: issues.length ? issues : undefined };
|
|
23
|
+
}
|
|
24
|
+
function completeFeaturesStrict(features, context, update) {
|
|
25
|
+
const validation = validateFeaturesStrict(features, context);
|
|
26
|
+
if (!validation.ok)
|
|
27
|
+
return validation;
|
|
28
|
+
const next = JSON.parse(JSON.stringify(features || []));
|
|
29
|
+
update(next);
|
|
30
|
+
return { ok: true };
|
|
31
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateDraftFeaturesStrict = validateDraftFeaturesStrict;
|
|
4
|
+
exports.saveDraftFeaturesStrict = saveDraftFeaturesStrict;
|
|
5
|
+
const constraints_1 = require("./constraints");
|
|
6
|
+
function validateDraftFeaturesStrict(draftFeatures, context) {
|
|
7
|
+
const eps = 1e-6;
|
|
8
|
+
const issues = [];
|
|
9
|
+
for (const f of draftFeatures) {
|
|
10
|
+
if (!f.constraints?.type)
|
|
11
|
+
continue;
|
|
12
|
+
const constrained = constraints_1.ConstraintRegistry.apply(f.x, f.y, f, context);
|
|
13
|
+
if (Math.abs(constrained.x - f.x) > eps ||
|
|
14
|
+
Math.abs(constrained.y - f.y) > eps) {
|
|
15
|
+
issues.push({
|
|
16
|
+
featureId: f.id,
|
|
17
|
+
groupId: f.groupId,
|
|
18
|
+
reason: "Position violates constraint strategy",
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return { ok: issues.length === 0, issues: issues.length ? issues : undefined };
|
|
23
|
+
}
|
|
24
|
+
function saveDraftFeaturesStrict(draftFeatures, context, update) {
|
|
25
|
+
const validation = validateDraftFeaturesStrict(draftFeatures, context);
|
|
26
|
+
if (!validation.ok)
|
|
27
|
+
return validation;
|
|
28
|
+
const next = JSON.parse(JSON.stringify(draftFeatures || []));
|
|
29
|
+
update(next);
|
|
30
|
+
return { ok: true, nextFeatures: next };
|
|
31
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FilmTool = void 0;
|
|
4
|
+
const core_1 = require("@pooder/core");
|
|
5
|
+
const fabric_1 = require("fabric");
|
|
6
|
+
class FilmTool {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.id = "pooder.kit.film";
|
|
9
|
+
this.metadata = {
|
|
10
|
+
name: "FilmTool",
|
|
11
|
+
};
|
|
12
|
+
this.url = "";
|
|
13
|
+
this.opacity = 0.5;
|
|
14
|
+
if (options) {
|
|
15
|
+
Object.assign(this, options);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
activate(context) {
|
|
19
|
+
this.canvasService = context.services.get("CanvasService");
|
|
20
|
+
if (!this.canvasService) {
|
|
21
|
+
console.warn("CanvasService not found for FilmTool");
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const configService = context.services.get("ConfigurationService");
|
|
25
|
+
if (configService) {
|
|
26
|
+
// Load initial config
|
|
27
|
+
this.url = configService.get("film.url", this.url);
|
|
28
|
+
this.opacity = configService.get("film.opacity", this.opacity);
|
|
29
|
+
// Listen for changes
|
|
30
|
+
configService.onAnyChange((e) => {
|
|
31
|
+
if (e.key.startsWith("film.")) {
|
|
32
|
+
const prop = e.key.split(".")[1];
|
|
33
|
+
console.log(`[FilmTool] Config change detected: ${e.key} -> ${e.value}`);
|
|
34
|
+
if (prop && prop in this) {
|
|
35
|
+
this[prop] = e.value;
|
|
36
|
+
this.updateFilm();
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
this.initLayer();
|
|
42
|
+
this.updateFilm();
|
|
43
|
+
}
|
|
44
|
+
deactivate(context) {
|
|
45
|
+
if (this.canvasService) {
|
|
46
|
+
const layer = this.canvasService.getLayer("overlay");
|
|
47
|
+
if (layer) {
|
|
48
|
+
const img = this.canvasService.getObject("film-image", "overlay");
|
|
49
|
+
if (img) {
|
|
50
|
+
layer.remove(img);
|
|
51
|
+
this.canvasService.requestRenderAll();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
this.canvasService = undefined;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
contribute() {
|
|
58
|
+
return {
|
|
59
|
+
[core_1.ContributionPointIds.CONFIGURATIONS]: [
|
|
60
|
+
{
|
|
61
|
+
id: "film.url",
|
|
62
|
+
type: "string",
|
|
63
|
+
label: "Film Image URL",
|
|
64
|
+
default: "",
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
id: "film.opacity",
|
|
68
|
+
type: "number",
|
|
69
|
+
label: "Opacity",
|
|
70
|
+
min: 0,
|
|
71
|
+
max: 1,
|
|
72
|
+
step: 0.1,
|
|
73
|
+
default: 0.5,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
[core_1.ContributionPointIds.COMMANDS]: [
|
|
77
|
+
{
|
|
78
|
+
command: "setFilmImage",
|
|
79
|
+
title: "Set Film Image",
|
|
80
|
+
handler: (url, opacity) => {
|
|
81
|
+
if (this.url === url && this.opacity === opacity)
|
|
82
|
+
return true;
|
|
83
|
+
this.url = url;
|
|
84
|
+
this.opacity = opacity;
|
|
85
|
+
this.updateFilm();
|
|
86
|
+
return true;
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
initLayer() {
|
|
93
|
+
if (!this.canvasService)
|
|
94
|
+
return;
|
|
95
|
+
let overlayLayer = this.canvasService.getLayer("overlay");
|
|
96
|
+
if (!overlayLayer) {
|
|
97
|
+
const width = this.canvasService.canvas.width || 800;
|
|
98
|
+
const height = this.canvasService.canvas.height || 600;
|
|
99
|
+
const layer = this.canvasService.createLayer("overlay", {
|
|
100
|
+
width,
|
|
101
|
+
height,
|
|
102
|
+
left: 0,
|
|
103
|
+
top: 0,
|
|
104
|
+
originX: "left",
|
|
105
|
+
originY: "top",
|
|
106
|
+
selectable: false,
|
|
107
|
+
evented: false,
|
|
108
|
+
subTargetCheck: false,
|
|
109
|
+
interactive: false,
|
|
110
|
+
});
|
|
111
|
+
this.canvasService.canvas.bringObjectToFront(layer);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async updateFilm() {
|
|
115
|
+
if (!this.canvasService)
|
|
116
|
+
return;
|
|
117
|
+
const layer = this.canvasService.getLayer("overlay");
|
|
118
|
+
if (!layer) {
|
|
119
|
+
console.warn("[FilmTool] Overlay layer not found");
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const { url, opacity } = this;
|
|
123
|
+
if (!url) {
|
|
124
|
+
const img = this.canvasService.getObject("film-image", "overlay");
|
|
125
|
+
if (img) {
|
|
126
|
+
layer.remove(img);
|
|
127
|
+
this.canvasService.requestRenderAll();
|
|
128
|
+
}
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const width = this.canvasService.canvas.width || 800;
|
|
132
|
+
const height = this.canvasService.canvas.height || 600;
|
|
133
|
+
let img = this.canvasService.getObject("film-image", "overlay");
|
|
134
|
+
try {
|
|
135
|
+
if (img) {
|
|
136
|
+
if (img.getSrc() !== url) {
|
|
137
|
+
await img.setSrc(url);
|
|
138
|
+
}
|
|
139
|
+
img.set({ opacity });
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
img = await fabric_1.FabricImage.fromURL(url, { crossOrigin: "anonymous" });
|
|
143
|
+
img.scaleToWidth(width);
|
|
144
|
+
if (img.getScaledHeight() < height)
|
|
145
|
+
img.scaleToHeight(height);
|
|
146
|
+
img.set({
|
|
147
|
+
originX: "left",
|
|
148
|
+
originY: "top",
|
|
149
|
+
left: 0,
|
|
150
|
+
top: 0,
|
|
151
|
+
opacity,
|
|
152
|
+
selectable: false,
|
|
153
|
+
evented: false,
|
|
154
|
+
data: { id: "film-image" },
|
|
155
|
+
});
|
|
156
|
+
layer.add(img);
|
|
157
|
+
}
|
|
158
|
+
this.canvasService.requestRenderAll();
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
console.error("[FilmTool] Failed to load film image", url, error);
|
|
162
|
+
}
|
|
163
|
+
layer.dirty = true;
|
|
164
|
+
this.canvasService.requestRenderAll();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
exports.FilmTool = FilmTool;
|
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.resolveFeaturePosition = resolveFeaturePosition;
|
|
7
|
+
exports.generateDielinePath = generateDielinePath;
|
|
8
|
+
exports.generateMaskPath = generateMaskPath;
|
|
9
|
+
exports.generateBleedZonePath = generateBleedZonePath;
|
|
10
|
+
exports.getNearestPointOnDieline = getNearestPointOnDieline;
|
|
11
|
+
exports.getPathBounds = getPathBounds;
|
|
12
|
+
const paper_1 = __importDefault(require("paper"));
|
|
13
|
+
/**
|
|
14
|
+
* Resolves the absolute position of a feature based on normalized coordinates.
|
|
15
|
+
*/
|
|
16
|
+
function resolveFeaturePosition(feature, geometry) {
|
|
17
|
+
const { x, y, width, height } = geometry;
|
|
18
|
+
// geometry.x/y is the Center.
|
|
19
|
+
const left = x - width / 2;
|
|
20
|
+
const top = y - height / 2;
|
|
21
|
+
return {
|
|
22
|
+
x: left + feature.x * width,
|
|
23
|
+
y: top + feature.y * height,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Initializes paper.js project if not already initialized.
|
|
28
|
+
*/
|
|
29
|
+
function ensurePaper(width, height) {
|
|
30
|
+
if (!paper_1.default.project) {
|
|
31
|
+
paper_1.default.setup(new paper_1.default.Size(width, height));
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
paper_1.default.view.viewSize = new paper_1.default.Size(width, height);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates the base dieline shape (Rect/Circle/Ellipse/Custom)
|
|
39
|
+
*/
|
|
40
|
+
function createBaseShape(options) {
|
|
41
|
+
const { shape, width, height, radius, x, y, pathData } = options;
|
|
42
|
+
const center = new paper_1.default.Point(x, y);
|
|
43
|
+
if (shape === "rect") {
|
|
44
|
+
return new paper_1.default.Path.Rectangle({
|
|
45
|
+
point: [x - width / 2, y - height / 2],
|
|
46
|
+
size: [Math.max(0, width), Math.max(0, height)],
|
|
47
|
+
radius: Math.max(0, radius),
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
else if (shape === "circle") {
|
|
51
|
+
const r = Math.min(width, height) / 2;
|
|
52
|
+
return new paper_1.default.Path.Circle({
|
|
53
|
+
center: center,
|
|
54
|
+
radius: Math.max(0, r),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
else if (shape === "ellipse") {
|
|
58
|
+
return new paper_1.default.Path.Ellipse({
|
|
59
|
+
center: center,
|
|
60
|
+
radius: [Math.max(0, width / 2), Math.max(0, height / 2)],
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
else if (shape === "custom" && pathData) {
|
|
64
|
+
const path = new paper_1.default.Path();
|
|
65
|
+
path.pathData = pathData;
|
|
66
|
+
// Align center
|
|
67
|
+
path.position = center;
|
|
68
|
+
if (width > 0 &&
|
|
69
|
+
height > 0 &&
|
|
70
|
+
path.bounds.width > 0 &&
|
|
71
|
+
path.bounds.height > 0) {
|
|
72
|
+
path.scale(width / path.bounds.width, height / path.bounds.height);
|
|
73
|
+
}
|
|
74
|
+
return path;
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
return new paper_1.default.Path.Rectangle({
|
|
78
|
+
point: [x - width / 2, y - height / 2],
|
|
79
|
+
size: [Math.max(0, width), Math.max(0, height)],
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Creates a Paper.js Item for a single feature.
|
|
85
|
+
*/
|
|
86
|
+
function createFeatureItem(feature, center) {
|
|
87
|
+
let item;
|
|
88
|
+
if (feature.shape === "rect") {
|
|
89
|
+
const w = feature.width || 10;
|
|
90
|
+
const h = feature.height || 10;
|
|
91
|
+
const r = feature.radius || 0;
|
|
92
|
+
item = new paper_1.default.Path.Rectangle({
|
|
93
|
+
point: [center.x - w / 2, center.y - h / 2],
|
|
94
|
+
size: [w, h],
|
|
95
|
+
radius: r,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
// Circle
|
|
100
|
+
const r = feature.radius || 5;
|
|
101
|
+
item = new paper_1.default.Path.Circle({
|
|
102
|
+
center: center,
|
|
103
|
+
radius: r,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (feature.rotation) {
|
|
107
|
+
item.rotate(feature.rotation, center);
|
|
108
|
+
}
|
|
109
|
+
return item;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Internal helper to generate the Perimeter Shape (Base + Edge Features).
|
|
113
|
+
*/
|
|
114
|
+
function getPerimeterShape(options) {
|
|
115
|
+
// 1. Create Base Shape
|
|
116
|
+
let mainShape = createBaseShape(options);
|
|
117
|
+
const { features } = options;
|
|
118
|
+
if (features && features.length > 0) {
|
|
119
|
+
// Filter for Edge Features (Default or explicit 'edge')
|
|
120
|
+
const edgeFeatures = features.filter((f) => !f.placement || f.placement === "edge");
|
|
121
|
+
const adds = [];
|
|
122
|
+
const subtracts = [];
|
|
123
|
+
edgeFeatures.forEach((f) => {
|
|
124
|
+
const pos = resolveFeaturePosition(f, options);
|
|
125
|
+
const center = new paper_1.default.Point(pos.x, pos.y);
|
|
126
|
+
const item = createFeatureItem(f, center);
|
|
127
|
+
if (f.operation === "add") {
|
|
128
|
+
adds.push(item);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
subtracts.push(item);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
// 2. Process Additions (Union)
|
|
135
|
+
if (adds.length > 0) {
|
|
136
|
+
for (const item of adds) {
|
|
137
|
+
try {
|
|
138
|
+
const temp = mainShape.unite(item);
|
|
139
|
+
mainShape.remove();
|
|
140
|
+
item.remove();
|
|
141
|
+
mainShape = temp;
|
|
142
|
+
}
|
|
143
|
+
catch (e) {
|
|
144
|
+
console.error("Geometry: Failed to unite feature", e);
|
|
145
|
+
item.remove();
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// 3. Process Subtractions (Difference)
|
|
150
|
+
if (subtracts.length > 0) {
|
|
151
|
+
for (const item of subtracts) {
|
|
152
|
+
try {
|
|
153
|
+
const temp = mainShape.subtract(item);
|
|
154
|
+
mainShape.remove();
|
|
155
|
+
item.remove();
|
|
156
|
+
mainShape = temp;
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
console.error("Geometry: Failed to subtract feature", e);
|
|
160
|
+
item.remove();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return mainShape;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Applies Internal/Surface features to a shape.
|
|
169
|
+
*/
|
|
170
|
+
function applySurfaceFeatures(shape, features, options) {
|
|
171
|
+
const internalFeatures = features.filter((f) => f.placement === "internal");
|
|
172
|
+
if (internalFeatures.length === 0)
|
|
173
|
+
return shape;
|
|
174
|
+
let result = shape;
|
|
175
|
+
// Internal features are usually subtractive (holes)
|
|
176
|
+
// But we support 'add' too (islands? maybe just unite)
|
|
177
|
+
for (const f of internalFeatures) {
|
|
178
|
+
const pos = resolveFeaturePosition(f, options);
|
|
179
|
+
const center = new paper_1.default.Point(pos.x, pos.y);
|
|
180
|
+
const item = createFeatureItem(f, center);
|
|
181
|
+
try {
|
|
182
|
+
if (f.operation === "add") {
|
|
183
|
+
const temp = result.unite(item);
|
|
184
|
+
result.remove();
|
|
185
|
+
item.remove();
|
|
186
|
+
result = temp;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
const temp = result.subtract(item);
|
|
190
|
+
result.remove();
|
|
191
|
+
item.remove();
|
|
192
|
+
result = temp;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
catch (e) {
|
|
196
|
+
console.error("Geometry: Failed to apply surface feature", e);
|
|
197
|
+
item.remove();
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return result;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Generates the path data for the Dieline (Product Shape).
|
|
204
|
+
*/
|
|
205
|
+
function generateDielinePath(options) {
|
|
206
|
+
const paperWidth = options.canvasWidth || options.width * 2 || 2000;
|
|
207
|
+
const paperHeight = options.canvasHeight || options.height * 2 || 2000;
|
|
208
|
+
ensurePaper(paperWidth, paperHeight);
|
|
209
|
+
paper_1.default.project.activeLayer.removeChildren();
|
|
210
|
+
const perimeter = getPerimeterShape(options);
|
|
211
|
+
const finalShape = applySurfaceFeatures(perimeter, options.features, options);
|
|
212
|
+
const pathData = finalShape.pathData;
|
|
213
|
+
finalShape.remove();
|
|
214
|
+
return pathData;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Generates the path data for the Mask (Background Overlay).
|
|
218
|
+
* Logic: Canvas SUBTRACT ProductShape
|
|
219
|
+
*/
|
|
220
|
+
function generateMaskPath(options) {
|
|
221
|
+
ensurePaper(options.canvasWidth, options.canvasHeight);
|
|
222
|
+
paper_1.default.project.activeLayer.removeChildren();
|
|
223
|
+
const { canvasWidth, canvasHeight } = options;
|
|
224
|
+
const maskRect = new paper_1.default.Path.Rectangle({
|
|
225
|
+
point: [0, 0],
|
|
226
|
+
size: [canvasWidth, canvasHeight],
|
|
227
|
+
});
|
|
228
|
+
const perimeter = getPerimeterShape(options);
|
|
229
|
+
const mainShape = applySurfaceFeatures(perimeter, options.features, options);
|
|
230
|
+
const finalMask = maskRect.subtract(mainShape);
|
|
231
|
+
maskRect.remove();
|
|
232
|
+
mainShape.remove();
|
|
233
|
+
const pathData = finalMask.pathData;
|
|
234
|
+
finalMask.remove();
|
|
235
|
+
return pathData;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Generates the path data for the Bleed Zone.
|
|
239
|
+
*/
|
|
240
|
+
function generateBleedZonePath(originalOptions, offsetOptions, offset) {
|
|
241
|
+
const paperWidth = originalOptions.canvasWidth || originalOptions.width * 2 || 2000;
|
|
242
|
+
const paperHeight = originalOptions.canvasHeight || originalOptions.height * 2 || 2000;
|
|
243
|
+
ensurePaper(paperWidth, paperHeight);
|
|
244
|
+
paper_1.default.project.activeLayer.removeChildren();
|
|
245
|
+
// 1. Generate Original Shape
|
|
246
|
+
const pOriginal = getPerimeterShape(originalOptions);
|
|
247
|
+
const shapeOriginal = applySurfaceFeatures(pOriginal, originalOptions.features, originalOptions);
|
|
248
|
+
// 2. Generate Offset Shape
|
|
249
|
+
const pOffset = getPerimeterShape(offsetOptions);
|
|
250
|
+
const shapeOffset = applySurfaceFeatures(pOffset, offsetOptions.features, offsetOptions);
|
|
251
|
+
// 3. Calculate Difference
|
|
252
|
+
let bleedZone;
|
|
253
|
+
if (offset > 0) {
|
|
254
|
+
bleedZone = shapeOffset.subtract(shapeOriginal);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
bleedZone = shapeOriginal.subtract(shapeOffset);
|
|
258
|
+
}
|
|
259
|
+
const pathData = bleedZone.pathData;
|
|
260
|
+
shapeOriginal.remove();
|
|
261
|
+
shapeOffset.remove();
|
|
262
|
+
bleedZone.remove();
|
|
263
|
+
return pathData;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Finds the nearest point on the Dieline geometry (Base Shape ONLY) for a given target point.
|
|
267
|
+
* Used for constraining feature movement.
|
|
268
|
+
*/
|
|
269
|
+
function getNearestPointOnDieline(point, options) {
|
|
270
|
+
ensurePaper(options.width * 2, options.height * 2);
|
|
271
|
+
paper_1.default.project.activeLayer.removeChildren();
|
|
272
|
+
// We constrain to the BASE shape, not including other features,
|
|
273
|
+
// because usually you want to snap to the main edge.
|
|
274
|
+
const shape = createBaseShape(options);
|
|
275
|
+
const p = new paper_1.default.Point(point.x, point.y);
|
|
276
|
+
const nearest = shape.getNearestPoint(p);
|
|
277
|
+
const result = { x: nearest.x, y: nearest.y };
|
|
278
|
+
shape.remove();
|
|
279
|
+
return result;
|
|
280
|
+
}
|
|
281
|
+
function getPathBounds(pathData) {
|
|
282
|
+
const path = new paper_1.default.Path();
|
|
283
|
+
path.pathData = pathData;
|
|
284
|
+
const bounds = path.bounds;
|
|
285
|
+
path.remove();
|
|
286
|
+
return {
|
|
287
|
+
x: bounds.x,
|
|
288
|
+
y: bounds.y,
|
|
289
|
+
width: bounds.width,
|
|
290
|
+
height: bounds.height,
|
|
291
|
+
};
|
|
292
|
+
}
|