@nmmty/lazycanvas 0.6.4 → 1.0.0-dev.10
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/ReadMe.md +1 -1
- package/dist/core/Interpolation.d.ts +30 -0
- package/dist/core/Interpolation.js +200 -0
- package/dist/core/Scene.d.ts +94 -0
- package/dist/core/Scene.js +157 -0
- package/dist/core/Signal.d.ts +133 -0
- package/dist/core/Signal.js +255 -0
- package/dist/core/SignalUtils.d.ts +133 -0
- package/dist/core/SignalUtils.js +333 -0
- package/dist/core/ThreadScheduler.d.ts +38 -0
- package/dist/core/ThreadScheduler.js +74 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.js +21 -0
- package/dist/helpers/Filters.js +1 -1
- package/dist/helpers/FontsList.js +18 -18
- package/dist/helpers/Utlis.d.ts +3 -3
- package/dist/helpers/Utlis.js +25 -36
- package/dist/helpers/index.d.ts +3 -3
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/jsx-runtime.d.ts +17 -0
- package/dist/jsx-runtime.js +111 -0
- package/dist/structures/LazyCanvas.d.ts +10 -48
- package/dist/structures/LazyCanvas.js +17 -78
- package/dist/structures/components/BaseLayer.d.ts +78 -32
- package/dist/structures/components/BaseLayer.js +106 -37
- package/dist/structures/components/BezierLayer.d.ts +27 -40
- package/dist/structures/components/BezierLayer.js +90 -55
- package/dist/structures/components/{Group.d.ts → Div.d.ts} +41 -22
- package/dist/structures/components/Div.js +202 -0
- package/dist/structures/components/ImageLayer.d.ts +2 -2
- package/dist/structures/components/ImageLayer.js +29 -30
- package/dist/structures/components/LineLayer.d.ts +20 -38
- package/dist/structures/components/LineLayer.js +45 -47
- package/dist/structures/components/MorphLayer.d.ts +5 -34
- package/dist/structures/components/MorphLayer.js +38 -52
- package/dist/structures/components/Path2DLayer.d.ts +7 -35
- package/dist/structures/components/Path2DLayer.js +32 -41
- package/dist/structures/components/PolygonLayer.d.ts +95 -0
- package/dist/structures/components/PolygonLayer.js +203 -0
- package/dist/structures/components/QuadraticLayer.d.ts +36 -45
- package/dist/structures/components/QuadraticLayer.js +90 -54
- package/dist/structures/components/TextLayer.d.ts +17 -46
- package/dist/structures/components/TextLayer.js +94 -77
- package/dist/structures/components/index.d.ts +10 -10
- package/dist/structures/components/index.js +2 -2
- package/dist/structures/helpers/Exporter.d.ts +13 -4
- package/dist/structures/helpers/Exporter.js +82 -46
- package/dist/structures/helpers/Font.js +1 -17
- package/dist/structures/helpers/Gradient.js +35 -49
- package/dist/structures/helpers/Link.js +2 -14
- package/dist/structures/helpers/Pattern.js +10 -18
- package/dist/structures/helpers/index.d.ts +7 -7
- package/dist/structures/helpers/readers/JSONReader.d.ts +4 -4
- package/dist/structures/helpers/readers/JSONReader.js +46 -48
- package/dist/structures/helpers/readers/YAMLReader.js +11 -11
- package/dist/structures/managers/FontsManager.js +9 -18
- package/dist/structures/managers/LayersManager.d.ts +18 -28
- package/dist/structures/managers/LayersManager.js +14 -36
- package/dist/structures/managers/LayoutManager.d.ts +23 -0
- package/dist/structures/managers/LayoutManager.js +409 -0
- package/dist/structures/managers/index.d.ts +3 -5
- package/dist/structures/managers/index.js +1 -3
- package/dist/structures/managers/{RenderManager.d.ts → piplines/ClassicRenderPipeline.d.ts} +4 -30
- package/dist/structures/managers/piplines/ClassicRenderPipeline.js +90 -0
- package/dist/structures/managers/piplines/ModernRenderPipeline.d.ts +44 -0
- package/dist/structures/managers/piplines/ModernRenderPipeline.js +123 -0
- package/dist/structures/managers/piplines/index.d.ts +24 -0
- package/dist/structures/managers/piplines/index.js +18 -0
- package/dist/types/enum.d.ts +4 -3
- package/dist/types/enum.js +3 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/types/types.d.ts +257 -105
- package/dist/utils/APNGEncoder.d.ts +67 -0
- package/dist/utils/APNGEncoder.js +205 -0
- package/dist/utils/DrawUtils.d.ts +9 -0
- package/dist/utils/DrawUtils.js +42 -0
- package/dist/utils/LazyUtil.js +1 -2
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +20 -0
- package/dist/utils/utils.d.ts +5 -9
- package/dist/utils/utils.js +154 -78
- package/package.json +61 -59
- package/dist/structures/components/ClearLayer.d.ts +0 -147
- package/dist/structures/components/ClearLayer.js +0 -158
- package/dist/structures/components/Group.js +0 -153
- package/dist/structures/managers/AnimationManager.d.ts +0 -120
- package/dist/structures/managers/AnimationManager.js +0 -99
- package/dist/structures/managers/PluginManager.d.ts +0 -230
- package/dist/structures/managers/PluginManager.js +0 -182
- package/dist/structures/managers/RenderManager.js +0 -183
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { AnyGlobalCompositeOperation, AnyLayer, LayerType } from "../../types";
|
|
2
2
|
import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
|
|
3
3
|
import { LayersManager } from "../managers";
|
|
4
|
+
import { BaseLayer, IBaseLayer, IBaseLayerProps } from "./BaseLayer";
|
|
4
5
|
/**
|
|
5
|
-
* Interface representing a group of
|
|
6
|
+
* Interface representing a group of layer's.
|
|
6
7
|
*/
|
|
7
|
-
export interface
|
|
8
|
+
export interface IDiv extends IBaseLayer {
|
|
8
9
|
/**
|
|
9
10
|
* The unique identifier of the group.
|
|
10
11
|
*/
|
|
@@ -22,24 +23,25 @@ export interface IGroup {
|
|
|
22
23
|
*/
|
|
23
24
|
zIndex: number;
|
|
24
25
|
/**
|
|
25
|
-
* The
|
|
26
|
+
* The layer's contained within the group.
|
|
26
27
|
*/
|
|
27
|
-
layers: Array<AnyLayer>;
|
|
28
|
+
layers: Array<AnyLayer | Div>;
|
|
28
29
|
/**
|
|
29
|
-
*
|
|
30
|
+
* The properties specific to the Div group.
|
|
30
31
|
*/
|
|
31
|
-
props
|
|
32
|
+
props: IDivProps;
|
|
32
33
|
}
|
|
33
|
-
export interface
|
|
34
|
+
export interface IDivProps extends IBaseLayerProps {
|
|
34
35
|
/**
|
|
35
36
|
* Don't use, this is just for compatibility.
|
|
36
37
|
*/
|
|
37
|
-
globalComposite
|
|
38
|
+
globalComposite?: AnyGlobalCompositeOperation;
|
|
39
|
+
children?: Array<any>;
|
|
38
40
|
}
|
|
39
41
|
/**
|
|
40
|
-
* Class representing a group of
|
|
42
|
+
* Class representing a group of layer's.
|
|
41
43
|
*/
|
|
42
|
-
export declare class
|
|
44
|
+
export declare class Div extends BaseLayer<IDivProps> implements IDiv {
|
|
43
45
|
/**
|
|
44
46
|
* The unique identifier of the group.
|
|
45
47
|
*/
|
|
@@ -57,17 +59,19 @@ export declare class Group implements IGroup {
|
|
|
57
59
|
*/
|
|
58
60
|
zIndex: number;
|
|
59
61
|
/**
|
|
60
|
-
* The
|
|
62
|
+
* The layer's contained within the group.
|
|
61
63
|
*/
|
|
62
|
-
layers: Array<
|
|
63
|
-
props
|
|
64
|
+
layers: Array<AnyLayer | Div>;
|
|
65
|
+
props: IDivProps;
|
|
66
|
+
parent?: IBaseLayer | any | null;
|
|
64
67
|
/**
|
|
65
68
|
* Constructs a new Group instance.
|
|
69
|
+
* @param {IDivProps} [props] - The properties of the Div.
|
|
66
70
|
* @param {string} [opts.id] - The unique identifier of the group.
|
|
67
71
|
* @param {boolean} [opts.visible] - The visibility of the group.
|
|
68
72
|
* @param {number} [opts.zIndex] - The z-index of the group.
|
|
69
73
|
*/
|
|
70
|
-
constructor(opts?: {
|
|
74
|
+
constructor(props?: IDivProps, opts?: {
|
|
71
75
|
id?: string;
|
|
72
76
|
visible?: boolean;
|
|
73
77
|
zIndex?: number;
|
|
@@ -92,10 +96,10 @@ export declare class Group implements IGroup {
|
|
|
92
96
|
setZIndex(zIndex: number): this;
|
|
93
97
|
/**
|
|
94
98
|
* Adds components to the group.
|
|
95
|
-
* @param {AnyLayer[]} [components] - The components to add to the group.
|
|
99
|
+
* @param {AnyLayer[] | Div[]} [components] - The components to add to the group.
|
|
96
100
|
* @returns {this} The current instance for chaining.
|
|
97
101
|
*/
|
|
98
|
-
add(...components: AnyLayer
|
|
102
|
+
add(...components: Array<AnyLayer | Div>): this;
|
|
99
103
|
/**
|
|
100
104
|
* Removes a component from the group by its ID.
|
|
101
105
|
* @param {string} [id] - The unique identifier of the component to remove.
|
|
@@ -110,23 +114,38 @@ export declare class Group implements IGroup {
|
|
|
110
114
|
/**
|
|
111
115
|
* Retrieves a component from the group by its ID.
|
|
112
116
|
* @param {string} [id] - The unique identifier of the component to retrieve.
|
|
113
|
-
* @returns {AnyLayer | undefined} The component with the specified ID, or undefined if not found.
|
|
117
|
+
* @returns {AnyLayer | Div | undefined} The component with the specified ID, or undefined if not found.
|
|
114
118
|
*/
|
|
115
|
-
get(id: string): AnyLayer | undefined;
|
|
119
|
+
get(id: string): AnyLayer | Div | undefined;
|
|
116
120
|
/**
|
|
117
121
|
* Retrieves all components from the group.
|
|
118
|
-
* @returns {AnyLayer[]} An array of all components in the group.
|
|
122
|
+
* @returns {AnyLayer[] | Div[]} An array of all components in the group.
|
|
119
123
|
*/
|
|
120
|
-
getAll(): AnyLayer
|
|
124
|
+
getAll(): Array<AnyLayer | Div>;
|
|
121
125
|
/**
|
|
122
126
|
* Gets the number of components in the group.
|
|
123
127
|
* @returns {number} The number of components in the group.
|
|
124
128
|
*/
|
|
125
129
|
get length(): number;
|
|
130
|
+
/**
|
|
131
|
+
* Update state for all child layers (for animation support)
|
|
132
|
+
* @param {number} time - Current time in seconds
|
|
133
|
+
*/
|
|
134
|
+
updateState(time: number): void;
|
|
135
|
+
/**
|
|
136
|
+
* Renders a single layer or group of layers.
|
|
137
|
+
* @param {AnyLayer | Div} [layer] - The layer or group to render.
|
|
138
|
+
* @param {SKRSContext2D} [ctx] - The canvas rendering context.
|
|
139
|
+
* @param {Canvas | SvgCanvas} [canvas] - The canvas instance.
|
|
140
|
+
* @param {LayersManager} [manager] - The layer's manager.
|
|
141
|
+
* @param {boolean} [debug] - Whether to enable debug logging.
|
|
142
|
+
* @returns {Promise<SKRSContext2D>} The canvas rendering context after rendering.
|
|
143
|
+
*/
|
|
144
|
+
private renderLayer;
|
|
126
145
|
draw(ctx: SKRSContext2D, canvas: Canvas | SvgCanvas, manager: LayersManager, debug: boolean): Promise<void>;
|
|
127
146
|
/**
|
|
128
147
|
* Converts the group to a JSON representation.
|
|
129
|
-
* @returns {
|
|
148
|
+
* @returns {IDiv} The JSON representation of the group.
|
|
130
149
|
*/
|
|
131
|
-
toJSON():
|
|
150
|
+
toJSON(): IDiv;
|
|
132
151
|
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Div = void 0;
|
|
4
|
+
const types_1 = require("../../types");
|
|
5
|
+
const utils_1 = require("../../utils");
|
|
6
|
+
const BaseLayer_1 = require("./BaseLayer");
|
|
7
|
+
/**
|
|
8
|
+
* Class representing a group of layer's.
|
|
9
|
+
*/
|
|
10
|
+
class Div extends BaseLayer_1.BaseLayer {
|
|
11
|
+
/**
|
|
12
|
+
* Constructs a new Group instance.
|
|
13
|
+
* @param {IDivProps} [props] - The properties of the Div.
|
|
14
|
+
* @param {string} [opts.id] - The unique identifier of the group.
|
|
15
|
+
* @param {boolean} [opts.visible] - The visibility of the group.
|
|
16
|
+
* @param {number} [opts.zIndex] - The z-index of the group.
|
|
17
|
+
*/
|
|
18
|
+
constructor(props, opts) {
|
|
19
|
+
super(types_1.LayerType.Group, props || {}, opts);
|
|
20
|
+
/**
|
|
21
|
+
* The type of the group, which is `Group`.
|
|
22
|
+
*/
|
|
23
|
+
this.type = types_1.LayerType.Group;
|
|
24
|
+
// Extract id, visible, zIndex from props if provided (for JSX support)
|
|
25
|
+
const propsId = props?.id;
|
|
26
|
+
const propsVisible = props?.visible;
|
|
27
|
+
const propsZIndex = props?.zIndex;
|
|
28
|
+
this.id = opts?.id || propsId || (0, utils_1.generateID)(types_1.LayerType.Group);
|
|
29
|
+
this.visible = opts?.visible ?? propsVisible ?? true;
|
|
30
|
+
this.zIndex = opts?.zIndex ?? propsZIndex ?? 1;
|
|
31
|
+
this.layers = [];
|
|
32
|
+
this.props = props || {};
|
|
33
|
+
this.parent = null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Sets the ID of the group.
|
|
37
|
+
* @param {string} [id] - The unique identifier of the group.
|
|
38
|
+
* @returns {this} The current instance for chaining.
|
|
39
|
+
*/
|
|
40
|
+
setID(id) {
|
|
41
|
+
this.id = id;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Sets the visibility of the group.
|
|
46
|
+
* @param {boolean} [visible] - The visibility state of the group.
|
|
47
|
+
* @returns {this} The current instance for chaining.
|
|
48
|
+
*/
|
|
49
|
+
setVisible(visible) {
|
|
50
|
+
this.visible = visible;
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Sets the z-index of the group.
|
|
55
|
+
* @param {number} [zIndex] - The z-index value of the group.
|
|
56
|
+
* @returns {this} The current instance for chaining.
|
|
57
|
+
*/
|
|
58
|
+
setZIndex(zIndex) {
|
|
59
|
+
this.zIndex = zIndex;
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Adds components to the group.
|
|
64
|
+
* @param {AnyLayer[] | Div[]} [components] - The components to add to the group.
|
|
65
|
+
* @returns {this} The current instance for chaining.
|
|
66
|
+
*/
|
|
67
|
+
add(...components) {
|
|
68
|
+
let layersArray = components.filter((l) => l !== undefined);
|
|
69
|
+
layersArray = layersArray.sort((a, b) => a.zIndex - b.zIndex);
|
|
70
|
+
for (const layer of layersArray) {
|
|
71
|
+
layer.parent = this;
|
|
72
|
+
}
|
|
73
|
+
this.layers.push(...layersArray);
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Removes a component from the group by its ID.
|
|
78
|
+
* @param {string} [id] - The unique identifier of the component to remove.
|
|
79
|
+
* @returns {this} The current instance for chaining.
|
|
80
|
+
*/
|
|
81
|
+
remove(id) {
|
|
82
|
+
this.layers = this.layers.filter((c) => c.id !== id);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Clears all components from the group.
|
|
87
|
+
* @returns {this} The current instance for chaining.
|
|
88
|
+
*/
|
|
89
|
+
clear() {
|
|
90
|
+
this.layers = [];
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Retrieves a component from the group by its ID.
|
|
95
|
+
* @param {string} [id] - The unique identifier of the component to retrieve.
|
|
96
|
+
* @returns {AnyLayer | Div | undefined} The component with the specified ID, or undefined if not found.
|
|
97
|
+
*/
|
|
98
|
+
get(id) {
|
|
99
|
+
return this.layers.find((c) => c.id === id);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Retrieves all components from the group.
|
|
103
|
+
* @returns {AnyLayer[] | Div[]} An array of all components in the group.
|
|
104
|
+
*/
|
|
105
|
+
getAll() {
|
|
106
|
+
return this.layers;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Gets the number of components in the group.
|
|
110
|
+
* @returns {number} The number of components in the group.
|
|
111
|
+
*/
|
|
112
|
+
get length() {
|
|
113
|
+
return this.layers.length;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Update state for all child layers (for animation support)
|
|
117
|
+
* @param {number} time - Current time in seconds
|
|
118
|
+
*/
|
|
119
|
+
updateState(time) {
|
|
120
|
+
for (const layer of this.layers) {
|
|
121
|
+
if ("updateState" in layer && typeof layer.updateState === "function") {
|
|
122
|
+
layer.updateState(time);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Renders a single layer or group of layers.
|
|
128
|
+
* @param {AnyLayer | Div} [layer] - The layer or group to render.
|
|
129
|
+
* @param {SKRSContext2D} [ctx] - The canvas rendering context.
|
|
130
|
+
* @param {Canvas | SvgCanvas} [canvas] - The canvas instance.
|
|
131
|
+
* @param {LayersManager} [manager] - The layer's manager.
|
|
132
|
+
* @param {boolean} [debug] - Whether to enable debug logging.
|
|
133
|
+
* @returns {Promise<SKRSContext2D>} The canvas rendering context after rendering.
|
|
134
|
+
*/
|
|
135
|
+
async renderLayer(layer, ctx, canvas, manager, debug) {
|
|
136
|
+
if (debug)
|
|
137
|
+
utils_1.LazyLog.log("info", `Rendering ${layer.id}...\nData:`, layer.toJSON());
|
|
138
|
+
if (layer.visible) {
|
|
139
|
+
ctx.globalCompositeOperation = layer.props?.globalComposite || "source-over";
|
|
140
|
+
await layer.draw(ctx, canvas, manager, debug);
|
|
141
|
+
// Draw children if any (and not a Div, as Div handles its own children)
|
|
142
|
+
// Actually, if we want to support children on any layer, we should handle it here.
|
|
143
|
+
// But Div.draw already handles children.
|
|
144
|
+
// If we handle it here for Div, we get double rendering.
|
|
145
|
+
// So we skip Div.
|
|
146
|
+
const children = layer.children;
|
|
147
|
+
if (!(layer instanceof Div) && children && Array.isArray(children) && children.length > 0) {
|
|
148
|
+
ctx.save();
|
|
149
|
+
// Apply parent position offset
|
|
150
|
+
// LayoutManager sets position relative to parent.
|
|
151
|
+
// We need to translate context to parent's position so children are drawn relative to it.
|
|
152
|
+
// However, layer.draw() might have already drawn the layer at that position.
|
|
153
|
+
// And layer.draw() usually restores context.
|
|
154
|
+
// So we are back at parent's parent coordinate system.
|
|
155
|
+
// We need to translate to layer's position.
|
|
156
|
+
if (layer.props.position) {
|
|
157
|
+
const x = typeof layer.props.position.x === "number" ? layer.props.position.x : 0;
|
|
158
|
+
const y = typeof layer.props.position.y === "number" ? layer.props.position.y : 0;
|
|
159
|
+
ctx.translate(x, y);
|
|
160
|
+
}
|
|
161
|
+
for (const child of children) {
|
|
162
|
+
await this.renderLayer(child, ctx, canvas, manager, debug);
|
|
163
|
+
}
|
|
164
|
+
ctx.restore();
|
|
165
|
+
}
|
|
166
|
+
ctx.shadowColor = "transparent";
|
|
167
|
+
}
|
|
168
|
+
return ctx;
|
|
169
|
+
}
|
|
170
|
+
async draw(ctx, canvas, manager, debug) {
|
|
171
|
+
ctx.save();
|
|
172
|
+
// Apply position translation if available (from layout)
|
|
173
|
+
if (this.props.position) {
|
|
174
|
+
const x = typeof this.props.position.x === "number" ? this.props.position.x : 0;
|
|
175
|
+
const y = typeof this.props.position.y === "number" ? this.props.position.y : 0;
|
|
176
|
+
ctx.translate(x, y);
|
|
177
|
+
}
|
|
178
|
+
for (const subLayer of this.layers) {
|
|
179
|
+
if (debug)
|
|
180
|
+
utils_1.LazyLog.log("info", `Rendering ${subLayer.id}...\nData:`, subLayer.toJSON());
|
|
181
|
+
if (subLayer.visible) {
|
|
182
|
+
await this.renderLayer(subLayer, ctx, canvas, manager, debug);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
ctx.restore();
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Converts the group to a JSON representation.
|
|
189
|
+
* @returns {IDiv} The JSON representation of the group.
|
|
190
|
+
*/
|
|
191
|
+
toJSON() {
|
|
192
|
+
return {
|
|
193
|
+
id: this.id,
|
|
194
|
+
type: this.type,
|
|
195
|
+
visible: this.visible,
|
|
196
|
+
zIndex: this.zIndex,
|
|
197
|
+
// @ts-ignore
|
|
198
|
+
layers: this.layers.map((c) => c.toJSON()),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
exports.Div = Div;
|
|
@@ -38,7 +38,7 @@ export interface IImageLayerProps extends IBaseLayerProps {
|
|
|
38
38
|
/**
|
|
39
39
|
* The radius of the image.
|
|
40
40
|
*/
|
|
41
|
-
radius
|
|
41
|
+
radius?: {
|
|
42
42
|
[corner in RadiusCorner]?: ScaleType;
|
|
43
43
|
};
|
|
44
44
|
};
|
|
@@ -78,7 +78,7 @@ export declare class ImageLayer extends BaseLayer<IImageLayerProps> {
|
|
|
78
78
|
* Draws the Image Layer on the canvas.
|
|
79
79
|
* @param {SKRSContext2D} [ctx] - The canvas rendering context.
|
|
80
80
|
* @param {Canvas | SvgCanvas} [canvas] - The canvas instance.
|
|
81
|
-
* @param {LayersManager} [manager] - The
|
|
81
|
+
* @param {LayersManager} [manager] - The layer's manager.
|
|
82
82
|
* @param {boolean} [debug] - Whether to enable debug logging.
|
|
83
83
|
* @throws {LazyError} If the image could not be loaded.
|
|
84
84
|
*/
|
|
@@ -4,17 +4,12 @@ exports.ImageLayer = void 0;
|
|
|
4
4
|
const BaseLayer_1 = require("./BaseLayer");
|
|
5
5
|
const types_1 = require("../../types");
|
|
6
6
|
const canvas_1 = require("@napi-rs/canvas");
|
|
7
|
-
const utils_1 = require("../../utils
|
|
8
|
-
const LazyUtil_1 = require("../../utils/LazyUtil");
|
|
7
|
+
const utils_1 = require("../../utils");
|
|
9
8
|
const helpers_1 = require("../helpers");
|
|
10
9
|
/**
|
|
11
10
|
* Class representing an Image Layer, extending the BaseLayer class.
|
|
12
11
|
*/
|
|
13
12
|
class ImageLayer extends BaseLayer_1.BaseLayer {
|
|
14
|
-
/**
|
|
15
|
-
* The properties of the Image Layer.
|
|
16
|
-
*/
|
|
17
|
-
props;
|
|
18
13
|
/**
|
|
19
14
|
* Constructs a new ImageLayer instance.
|
|
20
15
|
* @param {IImageLayerProps} [props] - The properties of the Image Layer.
|
|
@@ -33,7 +28,7 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
|
|
|
33
28
|
*/
|
|
34
29
|
setSrc(src) {
|
|
35
30
|
if (!(0, utils_1.isImageUrlValid)(src))
|
|
36
|
-
throw new
|
|
31
|
+
throw new utils_1.LazyError("The src of the image must be a valid URL");
|
|
37
32
|
this.props.src = src;
|
|
38
33
|
return this;
|
|
39
34
|
}
|
|
@@ -56,45 +51,49 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
|
|
|
56
51
|
* Draws the Image Layer on the canvas.
|
|
57
52
|
* @param {SKRSContext2D} [ctx] - The canvas rendering context.
|
|
58
53
|
* @param {Canvas | SvgCanvas} [canvas] - The canvas instance.
|
|
59
|
-
* @param {LayersManager} [manager] - The
|
|
54
|
+
* @param {LayersManager} [manager] - The layer's manager.
|
|
60
55
|
* @param {boolean} [debug] - Whether to enable debug logging.
|
|
61
56
|
* @throws {LazyError} If the image could not be loaded.
|
|
62
57
|
*/
|
|
63
58
|
async draw(ctx, canvas, manager, debug) {
|
|
64
59
|
const parcer = (0, utils_1.parser)(ctx, canvas, manager);
|
|
65
60
|
const { xs, ys, w } = parcer.parseBatch({
|
|
66
|
-
xs: { v: this.props.x },
|
|
67
|
-
ys: { v: this.props.y, options:
|
|
68
|
-
w: { v: this.props.size.width }
|
|
61
|
+
xs: { v: this.props.position?.x || 0 },
|
|
62
|
+
ys: { v: this.props.position?.y || 0, options: utils_1.defaultArg.vl(true) },
|
|
63
|
+
w: { v: this.props.size.width },
|
|
69
64
|
});
|
|
70
|
-
const h = parcer.parse(this.props.size.height,
|
|
65
|
+
const h = parcer.parse(this.props.size.height, utils_1.defaultArg.wh(w), utils_1.defaultArg.vl(true));
|
|
71
66
|
let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
|
|
72
67
|
const rad = {};
|
|
73
|
-
if (typeof this.props.size.radius ===
|
|
68
|
+
if (typeof this.props.size.radius === "object" && this.props.size.radius !== helpers_1.Link) {
|
|
74
69
|
for (const corner in this.props.size.radius) {
|
|
75
70
|
// @ts-ignore
|
|
76
|
-
rad[corner] = parcer.parse(
|
|
71
|
+
rad[corner] = parcer.parse(
|
|
72
|
+
// @ts-ignore
|
|
73
|
+
this.props.size.radius[corner], utils_1.defaultArg.wh(w / 2, h / 2), utils_1.defaultArg.vl(false, true));
|
|
77
74
|
}
|
|
78
75
|
}
|
|
79
76
|
if (debug)
|
|
80
|
-
|
|
77
|
+
utils_1.LazyLog.log("none", `ImageLayer:`, { x, y, w, h, rad });
|
|
81
78
|
ctx.save();
|
|
82
79
|
let image = await (0, canvas_1.loadImage)(this.props.src);
|
|
83
80
|
image.width = w;
|
|
84
81
|
image.height = h;
|
|
85
82
|
if (!image)
|
|
86
|
-
throw new
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
83
|
+
throw new utils_1.LazyError("The image could not be loaded");
|
|
84
|
+
if (this.props.transform) {
|
|
85
|
+
(0, utils_1.transform)(ctx, this.props.transform, { width: w, height: h, x, y, type: this.type });
|
|
86
|
+
}
|
|
87
|
+
utils_1.DrawUtils.drawShadow(ctx, this.props.shadow);
|
|
88
|
+
utils_1.DrawUtils.opacity(ctx, this.props.opacity);
|
|
89
|
+
utils_1.DrawUtils.filters(ctx, this.props.filter);
|
|
91
90
|
if (Object.keys(rad).length > 0) {
|
|
92
91
|
ctx.beginPath();
|
|
93
|
-
ctx.moveTo(x +
|
|
94
|
-
ctx.arcTo(x + w, y, x + w, y +
|
|
95
|
-
ctx.arcTo(x + w, y + h, x +
|
|
96
|
-
ctx.arcTo(x, y + h, x, y +
|
|
97
|
-
ctx.arcTo(x, y, x +
|
|
92
|
+
ctx.moveTo(x + w / 2, y);
|
|
93
|
+
ctx.arcTo(x + w, y, x + w, y + h / 2, rad.rightTop || rad.all || 0);
|
|
94
|
+
ctx.arcTo(x + w, y + h, x + w / 2, y + h, rad.rightBottom || rad.all || 0);
|
|
95
|
+
ctx.arcTo(x, y + h, x, y + h / 2, rad.leftBottom || rad.all || 0);
|
|
96
|
+
ctx.arcTo(x, y, x + w / 2, y, rad.leftTop || rad.all || 0);
|
|
98
97
|
ctx.closePath();
|
|
99
98
|
ctx.clip();
|
|
100
99
|
ctx.drawImage(image, x, y, w, h);
|
|
@@ -111,8 +110,8 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
|
|
|
111
110
|
toJSON() {
|
|
112
111
|
let data = super.toJSON();
|
|
113
112
|
let copy = { ...this.props };
|
|
114
|
-
for (const key of [
|
|
115
|
-
if (copy[key] && typeof copy[key] ===
|
|
113
|
+
for (const key of ["x", "y", "size.width", "size.height", "size.radius"]) {
|
|
114
|
+
if (copy[key] && typeof copy[key] === "object" && "toJSON" in copy[key]) {
|
|
116
115
|
copy[key] = copy[key].toJSON();
|
|
117
116
|
}
|
|
118
117
|
}
|
|
@@ -126,12 +125,12 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
|
|
|
126
125
|
validateProps(data) {
|
|
127
126
|
return {
|
|
128
127
|
...super.validateProps(data),
|
|
129
|
-
src: data.src ||
|
|
128
|
+
src: data.src || "",
|
|
130
129
|
size: {
|
|
131
130
|
width: data.size?.width || 0,
|
|
132
131
|
height: data.size?.height || 0,
|
|
133
|
-
radius: data.size?.radius || { all: 0 }
|
|
134
|
-
}
|
|
132
|
+
radius: data.size?.radius || { all: 0 },
|
|
133
|
+
},
|
|
135
134
|
};
|
|
136
135
|
}
|
|
137
136
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BaseLayer, IBaseLayer, IBaseLayerMisc, IBaseLayerProps } from "./BaseLayer";
|
|
2
|
-
import { ColorType, ScaleType, LayerType } from "../../types";
|
|
2
|
+
import { ColorType, ScaleType, LayerType, StrokeOptions } from "../../types";
|
|
3
3
|
import { Canvas, SKRSContext2D, SvgCanvas } from "@napi-rs/canvas";
|
|
4
4
|
import { LayersManager } from "../managers";
|
|
5
5
|
/**
|
|
@@ -19,18 +19,23 @@ export interface ILineLayer extends IBaseLayer {
|
|
|
19
19
|
* Interface representing the properties of a Line Layer.
|
|
20
20
|
*/
|
|
21
21
|
export interface ILineLayerProps extends IBaseLayerProps {
|
|
22
|
-
|
|
23
|
-
* The end point of the line, including x and y coordinates.
|
|
24
|
-
*/
|
|
25
|
-
endPoint: {
|
|
22
|
+
position: {
|
|
26
23
|
/**
|
|
27
|
-
* The x-coordinate of the
|
|
24
|
+
* The x-coordinate of the start point of the line.
|
|
28
25
|
*/
|
|
29
26
|
x: ScaleType;
|
|
30
27
|
/**
|
|
31
|
-
* The y-coordinate of the
|
|
28
|
+
* The y-coordinate of the start point of the line.
|
|
32
29
|
*/
|
|
33
30
|
y: ScaleType;
|
|
31
|
+
/**
|
|
32
|
+
* The x-coordinate of the end point of the line.
|
|
33
|
+
*/
|
|
34
|
+
endX: ScaleType;
|
|
35
|
+
/**
|
|
36
|
+
* The y-coordinate of the end point of the line.
|
|
37
|
+
*/
|
|
38
|
+
endY: ScaleType;
|
|
34
39
|
};
|
|
35
40
|
/**
|
|
36
41
|
* Whether the layer is filled.
|
|
@@ -39,36 +44,11 @@ export interface ILineLayerProps extends IBaseLayerProps {
|
|
|
39
44
|
/**
|
|
40
45
|
* The fill style (color or pattern) of the layer.
|
|
41
46
|
*/
|
|
42
|
-
|
|
47
|
+
color: ColorType;
|
|
43
48
|
/**
|
|
44
49
|
* The stroke properties of the line.
|
|
45
50
|
*/
|
|
46
|
-
stroke:
|
|
47
|
-
/**
|
|
48
|
-
* The width of the stroke.
|
|
49
|
-
*/
|
|
50
|
-
width: number;
|
|
51
|
-
/**
|
|
52
|
-
* The cap style of the stroke.
|
|
53
|
-
*/
|
|
54
|
-
cap: CanvasLineCap;
|
|
55
|
-
/**
|
|
56
|
-
* The join style of the stroke.
|
|
57
|
-
*/
|
|
58
|
-
join: CanvasLineJoin;
|
|
59
|
-
/**
|
|
60
|
-
* The dash offset of the stroke.
|
|
61
|
-
*/
|
|
62
|
-
dashOffset: number;
|
|
63
|
-
/**
|
|
64
|
-
* The dash pattern of the stroke.
|
|
65
|
-
*/
|
|
66
|
-
dash: number[];
|
|
67
|
-
/**
|
|
68
|
-
* The miter limit of the stroke.
|
|
69
|
-
*/
|
|
70
|
-
miterLimit: number;
|
|
71
|
-
};
|
|
51
|
+
stroke: StrokeOptions;
|
|
72
52
|
}
|
|
73
53
|
/**
|
|
74
54
|
* Class representing a Line Layer, extending the BaseLayer class.
|
|
@@ -85,12 +65,14 @@ export declare class LineLayer extends BaseLayer<ILineLayerProps> {
|
|
|
85
65
|
*/
|
|
86
66
|
constructor(props?: ILineLayerProps, misc?: IBaseLayerMisc);
|
|
87
67
|
/**
|
|
88
|
-
* Sets the
|
|
68
|
+
* Sets the position of the line layer.
|
|
89
69
|
* @param {ScaleType} [x] - The x-coordinate of the end point.
|
|
90
70
|
* @param {ScaleType} [y] - The y-coordinate of the end point.
|
|
71
|
+
* @param {ScaleType} [endX] - The x-coordinate of the end point.
|
|
72
|
+
* @param {ScaleType} [endY] - The y-coordinate of the end point.
|
|
91
73
|
* @returns {this} The current instance for chaining.
|
|
92
74
|
*/
|
|
93
|
-
|
|
75
|
+
setPosition(x: ScaleType, y: ScaleType, endX?: ScaleType, endY?: ScaleType): this;
|
|
94
76
|
/**
|
|
95
77
|
* Sets the color of the line layer.
|
|
96
78
|
* @param {ColorType} [color] - The color of the layer.
|
|
@@ -113,7 +95,7 @@ export declare class LineLayer extends BaseLayer<ILineLayerProps> {
|
|
|
113
95
|
* Calculates the bounding box of the line layer.
|
|
114
96
|
* @param {SKRSContext2D} [ctx] - The canvas rendering context.
|
|
115
97
|
* @param {Canvas | SvgCanvas} [canvas] - The canvas instance.
|
|
116
|
-
* @param {LayersManager} [manager] - The
|
|
98
|
+
* @param {LayersManager} [manager] - The layer's manager.
|
|
117
99
|
* @returns {Object} The bounding box details including start and end points, width, and height.
|
|
118
100
|
*/
|
|
119
101
|
getBoundingBox(ctx: SKRSContext2D, canvas: Canvas | SvgCanvas, manager: LayersManager): {
|
|
@@ -128,7 +110,7 @@ export declare class LineLayer extends BaseLayer<ILineLayerProps> {
|
|
|
128
110
|
* Draws the line layer on the canvas.
|
|
129
111
|
* @param {SKRSContext2D} [ctx] - The canvas rendering context.
|
|
130
112
|
* @param {Canvas | SvgCanvas} [canvas] - The canvas instance.
|
|
131
|
-
* @param {LayersManager} [manager] - The
|
|
113
|
+
* @param {LayersManager} [manager] - The layer's manager.
|
|
132
114
|
* @param {boolean} [debug] - Whether to enable debug logging.
|
|
133
115
|
*/
|
|
134
116
|
draw(ctx: SKRSContext2D, canvas: Canvas | SvgCanvas, manager: LayersManager, debug: boolean): Promise<void>;
|