@nmmty/lazycanvas 0.6.0-dev.f33019 → 0.6.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/dist/helpers/Utlis.d.ts +28 -0
- package/dist/helpers/Utlis.js +78 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -1
- package/dist/structures/LazyCanvas.d.ts +38 -1
- package/dist/structures/LazyCanvas.js +50 -3
- package/dist/structures/components/BaseLayer.js +1 -1
- package/dist/structures/components/ImageLayer.d.ts +0 -2
- package/dist/structures/components/MorphLayer.d.ts +0 -6
- package/dist/structures/components/MorphLayer.js +1 -9
- package/dist/structures/components/Path2DLayer.d.ts +0 -6
- package/dist/structures/components/Path2DLayer.js +1 -9
- package/dist/structures/components/TextLayer.d.ts +0 -6
- package/dist/structures/components/TextLayer.js +1 -9
- package/dist/structures/helpers/Exporter.d.ts +0 -2
- package/dist/structures/helpers/Exporter.js +40 -23
- package/dist/structures/helpers/Font.d.ts +0 -2
- package/dist/structures/helpers/readers/JSONReader.js +17 -7
- package/dist/structures/helpers/readers/YAMLReader.js +17 -7
- package/dist/structures/managers/LayersManager.d.ts +11 -1
- package/dist/structures/managers/LayersManager.js +16 -1
- package/dist/structures/managers/PluginManager.d.ts +228 -0
- package/dist/structures/managers/PluginManager.js +181 -0
- package/dist/structures/managers/RenderManager.d.ts +0 -2
- package/dist/structures/managers/RenderManager.js +21 -6
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +1 -0
- package/dist/utils/utils.js +15 -16
- package/package.json +9 -5
|
@@ -7,6 +7,10 @@ const LazyUtil_1 = require("../../utils/LazyUtil");
|
|
|
7
7
|
* Class representing a manager for handling layers and groups.
|
|
8
8
|
*/
|
|
9
9
|
class LayersManager {
|
|
10
|
+
/**
|
|
11
|
+
* The LazyCanvas instance associated with this manager.
|
|
12
|
+
*/
|
|
13
|
+
lazycanvas;
|
|
10
14
|
/**
|
|
11
15
|
* A map storing layers or groups with their IDs as keys.
|
|
12
16
|
*/
|
|
@@ -17,10 +21,12 @@ class LayersManager {
|
|
|
17
21
|
debug;
|
|
18
22
|
/**
|
|
19
23
|
* Constructs a new LayersManager instance.
|
|
24
|
+
* @param lazycanvas {LazyCanvas} - The LazyCanvas instance to associate with this manager.
|
|
20
25
|
* @param opts {Object} - Optional settings for the LayersManager.
|
|
21
26
|
* @param opts.debug {boolean} - Whether debugging is enabled.
|
|
22
27
|
*/
|
|
23
|
-
constructor(opts) {
|
|
28
|
+
constructor(lazycanvas, opts) {
|
|
29
|
+
this.lazycanvas = lazycanvas;
|
|
24
30
|
this.map = new Map();
|
|
25
31
|
this.debug = opts?.debug || false;
|
|
26
32
|
}
|
|
@@ -41,6 +47,10 @@ class LayersManager {
|
|
|
41
47
|
if (this.map.has(layer.id))
|
|
42
48
|
throw new LazyUtil_1.LazyError("Layer already exists");
|
|
43
49
|
this.map.set(layer.id, layer);
|
|
50
|
+
// onLayerAdded hook
|
|
51
|
+
if (this.lazycanvas && this.lazycanvas.manager?.plugins) {
|
|
52
|
+
this.lazycanvas.manager.plugins.executeHook('onLayerAdded', this.lazycanvas, layer);
|
|
53
|
+
}
|
|
44
54
|
}
|
|
45
55
|
this.sort();
|
|
46
56
|
return this;
|
|
@@ -52,7 +62,12 @@ class LayersManager {
|
|
|
52
62
|
*/
|
|
53
63
|
remove(...ids) {
|
|
54
64
|
for (const id of ids) {
|
|
65
|
+
const layer = this.map.get(id);
|
|
55
66
|
this.map.delete(id);
|
|
67
|
+
// onLayerRemoved hook
|
|
68
|
+
if (this.lazycanvas && this.lazycanvas.manager?.plugins) {
|
|
69
|
+
this.lazycanvas.manager.plugins.executeHook('onLayerRemoved', this.lazycanvas, id);
|
|
70
|
+
}
|
|
56
71
|
}
|
|
57
72
|
return this;
|
|
58
73
|
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { LazyCanvas } from "../LazyCanvas";
|
|
2
|
+
import { AnyLayer } from "../../types";
|
|
3
|
+
import { Group } from "../components";
|
|
4
|
+
/**
|
|
5
|
+
* Interface representing a LazyCanvas plugin.
|
|
6
|
+
*/
|
|
7
|
+
export interface ILazyCanvasPlugin {
|
|
8
|
+
/**
|
|
9
|
+
* The unique name of the plugin.
|
|
10
|
+
*/
|
|
11
|
+
name: string;
|
|
12
|
+
/**
|
|
13
|
+
* The version of the plugin.
|
|
14
|
+
*/
|
|
15
|
+
version: string;
|
|
16
|
+
/**
|
|
17
|
+
* Optional description of the plugin.
|
|
18
|
+
*/
|
|
19
|
+
description?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Optional author of the plugin.
|
|
22
|
+
*/
|
|
23
|
+
author?: string;
|
|
24
|
+
/**
|
|
25
|
+
* Optional dependencies on other plugins.
|
|
26
|
+
*/
|
|
27
|
+
dependencies?: string[];
|
|
28
|
+
/**
|
|
29
|
+
* Optional configuration object for the plugin.
|
|
30
|
+
*/
|
|
31
|
+
config?: any;
|
|
32
|
+
/**
|
|
33
|
+
* Optional private data storage for the plugin.
|
|
34
|
+
*/
|
|
35
|
+
private?: any;
|
|
36
|
+
/**
|
|
37
|
+
* Method called when the plugin is installed.
|
|
38
|
+
* @param canvas - The LazyCanvas instance.
|
|
39
|
+
* @returns true if installation was successful, false otherwise.
|
|
40
|
+
*/
|
|
41
|
+
install(canvas: LazyCanvas): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Optional method called when the plugin is uninstalled.
|
|
44
|
+
* @param canvas - The LazyCanvas instance.
|
|
45
|
+
* @returns true if uninstallation was successful, false otherwise.
|
|
46
|
+
*/
|
|
47
|
+
uninstall?(canvas: LazyCanvas): boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Optional hooks for lifecycle events.
|
|
50
|
+
*/
|
|
51
|
+
hooks?: IPluginHooks;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Interface representing plugin hooks for lifecycle events.
|
|
55
|
+
*/
|
|
56
|
+
export interface IPluginHooks {
|
|
57
|
+
/**
|
|
58
|
+
* Called before rendering starts.
|
|
59
|
+
*/
|
|
60
|
+
beforeRender?(canvas: LazyCanvas): void;
|
|
61
|
+
/**
|
|
62
|
+
* Called after rendering completes.
|
|
63
|
+
*/
|
|
64
|
+
afterRender?(canvas: LazyCanvas): void;
|
|
65
|
+
/**
|
|
66
|
+
* Called before export starts.
|
|
67
|
+
*/
|
|
68
|
+
beforeExport?(canvas: LazyCanvas): void;
|
|
69
|
+
/**
|
|
70
|
+
* Called after export completes.
|
|
71
|
+
*/
|
|
72
|
+
afterExport?(canvas: LazyCanvas, result: any): void;
|
|
73
|
+
/**
|
|
74
|
+
* Called when canvas is resized.
|
|
75
|
+
*/
|
|
76
|
+
onResize?(canvas: LazyCanvas, ratio: number): void;
|
|
77
|
+
/**
|
|
78
|
+
* Called when a layer is added.
|
|
79
|
+
*/
|
|
80
|
+
onLayerAdded?(canvas: LazyCanvas, layer: AnyLayer | Group): void;
|
|
81
|
+
/**
|
|
82
|
+
* Called when a layer is removed.
|
|
83
|
+
*/
|
|
84
|
+
onLayerRemoved?(canvas: LazyCanvas, layerId: string): void;
|
|
85
|
+
/**
|
|
86
|
+
* Called when canvas is created/recreated.
|
|
87
|
+
*/
|
|
88
|
+
onCanvasCreated?(canvas: LazyCanvas, width: number, height: number): void;
|
|
89
|
+
/**
|
|
90
|
+
* Called when a layer is modified.
|
|
91
|
+
*/
|
|
92
|
+
onLayerModified?(canvas: LazyCanvas, layer: AnyLayer | Group): void;
|
|
93
|
+
/**
|
|
94
|
+
* Called when animation frame is processed.
|
|
95
|
+
*/
|
|
96
|
+
onAnimationFrame?(canvas: LazyCanvas, frame: number): void;
|
|
97
|
+
/**
|
|
98
|
+
* Called when an error occurs.
|
|
99
|
+
*/
|
|
100
|
+
onError?(canvas: LazyCanvas, error: Error): void;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Interface representing the PluginManager.
|
|
104
|
+
*/
|
|
105
|
+
export interface IPluginManager {
|
|
106
|
+
/**
|
|
107
|
+
* A map storing installed plugins with their names as keys.
|
|
108
|
+
*/
|
|
109
|
+
plugins: Map<string, ILazyCanvasPlugin>;
|
|
110
|
+
/**
|
|
111
|
+
* Whether debugging is enabled.
|
|
112
|
+
*/
|
|
113
|
+
debug: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Reference to the LazyCanvas instance.
|
|
116
|
+
*/
|
|
117
|
+
canvas: LazyCanvas;
|
|
118
|
+
/**
|
|
119
|
+
* Registers a plugin.
|
|
120
|
+
* @param plugin - The plugin to register.
|
|
121
|
+
*/
|
|
122
|
+
register(plugin: ILazyCanvasPlugin): void;
|
|
123
|
+
/**
|
|
124
|
+
* Unregisters a plugin by name.
|
|
125
|
+
* @param pluginName - The name of the plugin to unregister.
|
|
126
|
+
* @throws {LazyError} If the plugin is not found or if other plugins depend on it.
|
|
127
|
+
*/
|
|
128
|
+
unregister(pluginName: string): void;
|
|
129
|
+
/**
|
|
130
|
+
* Gets a plugin by name.
|
|
131
|
+
* @param pluginName - The name of the plugin.
|
|
132
|
+
*/
|
|
133
|
+
get(pluginName: string): ILazyCanvasPlugin | undefined;
|
|
134
|
+
/**
|
|
135
|
+
* Lists all registered plugin names.
|
|
136
|
+
*/
|
|
137
|
+
list(): string[];
|
|
138
|
+
/**
|
|
139
|
+
* Checks if a plugin is registered.
|
|
140
|
+
* @param pluginName - The name of the plugin.
|
|
141
|
+
*/
|
|
142
|
+
has(pluginName: string): boolean;
|
|
143
|
+
/**
|
|
144
|
+
* Executes a hook for all plugins that implement it.
|
|
145
|
+
* @param hookName - The name of the hook to execute.
|
|
146
|
+
* @param args - Arguments to pass to the hook.
|
|
147
|
+
*/
|
|
148
|
+
executeHook(hookName: keyof IPluginHooks, ...args: any[]): void;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Class representing a manager for handling plugins.
|
|
152
|
+
*/
|
|
153
|
+
export declare class PluginManager implements IPluginManager {
|
|
154
|
+
/**
|
|
155
|
+
* A map storing installed plugins with their names as keys.
|
|
156
|
+
*/
|
|
157
|
+
plugins: Map<string, ILazyCanvasPlugin>;
|
|
158
|
+
/**
|
|
159
|
+
* Whether debugging is enabled.
|
|
160
|
+
*/
|
|
161
|
+
debug: boolean;
|
|
162
|
+
/**
|
|
163
|
+
* Reference to the LazyCanvas instance.
|
|
164
|
+
*/
|
|
165
|
+
canvas: LazyCanvas;
|
|
166
|
+
/**
|
|
167
|
+
* Constructs a new PluginManager instance.
|
|
168
|
+
* @param canvas - The LazyCanvas instance.
|
|
169
|
+
* @param opts - Optional settings for the PluginManager.
|
|
170
|
+
*/
|
|
171
|
+
constructor(canvas: LazyCanvas, opts?: {
|
|
172
|
+
debug?: boolean;
|
|
173
|
+
});
|
|
174
|
+
/**
|
|
175
|
+
* Registers a plugin.
|
|
176
|
+
* @param plugin - The plugin to register.
|
|
177
|
+
* @throws {LazyError} If a plugin with the same name is already registered.
|
|
178
|
+
*/
|
|
179
|
+
register(plugin: ILazyCanvasPlugin): void;
|
|
180
|
+
/**
|
|
181
|
+
* Unregisters a plugin by name.
|
|
182
|
+
* @param pluginName - The name of the plugin to unregister.
|
|
183
|
+
* @throws {LazyError} If the plugin is not found or if other plugins depend on it.
|
|
184
|
+
*/
|
|
185
|
+
unregister(pluginName: string): void;
|
|
186
|
+
/**
|
|
187
|
+
* Gets a plugin by name.
|
|
188
|
+
* @param pluginName - The name of the plugin.
|
|
189
|
+
* @returns The plugin or undefined if not found.
|
|
190
|
+
*/
|
|
191
|
+
get(pluginName: string): ILazyCanvasPlugin | undefined;
|
|
192
|
+
/**
|
|
193
|
+
* Lists all registered plugin names.
|
|
194
|
+
* @returns Array of plugin names.
|
|
195
|
+
*/
|
|
196
|
+
list(): string[];
|
|
197
|
+
/**
|
|
198
|
+
* Checks if a plugin is registered.
|
|
199
|
+
* @param pluginName - The name of the plugin.
|
|
200
|
+
* @returns True if the plugin is registered, false otherwise.
|
|
201
|
+
*/
|
|
202
|
+
has(pluginName: string): boolean;
|
|
203
|
+
/**
|
|
204
|
+
* Executes a hook for all plugins that implement it.
|
|
205
|
+
* @param hookName - The name of the hook to execute.
|
|
206
|
+
* @param args - Arguments to pass to the hook.
|
|
207
|
+
*/
|
|
208
|
+
executeHook(hookName: keyof IPluginHooks, ...args: any[]): void;
|
|
209
|
+
/**
|
|
210
|
+
* Executes the onError hook for all plugins when an error occurs.
|
|
211
|
+
* @param error - The error that occurred.
|
|
212
|
+
*/
|
|
213
|
+
private executeErrorHook;
|
|
214
|
+
/**
|
|
215
|
+
* Gets plugin information.
|
|
216
|
+
* @returns Array of plugin information objects.
|
|
217
|
+
*/
|
|
218
|
+
getPluginInfo(): Array<{
|
|
219
|
+
name: string;
|
|
220
|
+
version: string;
|
|
221
|
+
description?: string;
|
|
222
|
+
dependencies?: string[];
|
|
223
|
+
}>;
|
|
224
|
+
/**
|
|
225
|
+
* Clears all plugins.
|
|
226
|
+
*/
|
|
227
|
+
clear(): void;
|
|
228
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PluginManager = void 0;
|
|
4
|
+
const LazyUtil_1 = require("../../utils/LazyUtil");
|
|
5
|
+
/**
|
|
6
|
+
* Class representing a manager for handling plugins.
|
|
7
|
+
*/
|
|
8
|
+
class PluginManager {
|
|
9
|
+
/**
|
|
10
|
+
* A map storing installed plugins with their names as keys.
|
|
11
|
+
*/
|
|
12
|
+
plugins;
|
|
13
|
+
/**
|
|
14
|
+
* Whether debugging is enabled.
|
|
15
|
+
*/
|
|
16
|
+
debug;
|
|
17
|
+
/**
|
|
18
|
+
* Reference to the LazyCanvas instance.
|
|
19
|
+
*/
|
|
20
|
+
canvas;
|
|
21
|
+
/**
|
|
22
|
+
* Constructs a new PluginManager instance.
|
|
23
|
+
* @param canvas - The LazyCanvas instance.
|
|
24
|
+
* @param opts - Optional settings for the PluginManager.
|
|
25
|
+
*/
|
|
26
|
+
constructor(canvas, opts) {
|
|
27
|
+
this.plugins = new Map();
|
|
28
|
+
this.debug = opts?.debug || false;
|
|
29
|
+
this.canvas = canvas;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Registers a plugin.
|
|
33
|
+
* @param plugin - The plugin to register.
|
|
34
|
+
* @throws {LazyError} If a plugin with the same name is already registered.
|
|
35
|
+
*/
|
|
36
|
+
register(plugin) {
|
|
37
|
+
if (this.plugins.has(plugin.name)) {
|
|
38
|
+
throw new LazyUtil_1.LazyError(`Plugin '${plugin.name}' is already registered`);
|
|
39
|
+
}
|
|
40
|
+
// Check dependencies
|
|
41
|
+
if (plugin.dependencies) {
|
|
42
|
+
for (const dependency of plugin.dependencies) {
|
|
43
|
+
if (!this.plugins.has(dependency)) {
|
|
44
|
+
throw new LazyUtil_1.LazyError(`Plugin '${plugin.name}' requires dependency '${dependency}' which is not installed`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const result = plugin.install(this.canvas);
|
|
50
|
+
this.plugins.set(plugin.name, plugin);
|
|
51
|
+
if (this.debug) {
|
|
52
|
+
LazyUtil_1.LazyLog.log('info', `Plugin '${plugin.name}' v${plugin.version} registered successfully`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
throw new LazyUtil_1.LazyError(`Failed to install plugin '${plugin.name}': ${error}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Unregisters a plugin by name.
|
|
61
|
+
* @param pluginName - The name of the plugin to unregister.
|
|
62
|
+
* @throws {LazyError} If the plugin is not found or if other plugins depend on it.
|
|
63
|
+
*/
|
|
64
|
+
unregister(pluginName) {
|
|
65
|
+
const plugin = this.plugins.get(pluginName);
|
|
66
|
+
if (!plugin) {
|
|
67
|
+
throw new LazyUtil_1.LazyError(`Plugin '${pluginName}' is not registered`);
|
|
68
|
+
}
|
|
69
|
+
// Check if other plugins depend on this one
|
|
70
|
+
this.plugins.forEach((p, name) => {
|
|
71
|
+
if (name !== pluginName && p.dependencies?.includes(pluginName)) {
|
|
72
|
+
throw new LazyUtil_1.LazyError(`Cannot unregister plugin '${pluginName}' because plugin '${name}' depends on it`);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
try {
|
|
76
|
+
if (plugin.uninstall) {
|
|
77
|
+
plugin.uninstall(this.canvas);
|
|
78
|
+
}
|
|
79
|
+
this.plugins.delete(pluginName);
|
|
80
|
+
if (this.debug) {
|
|
81
|
+
LazyUtil_1.LazyLog.log('info', `Plugin '${pluginName}' unregistered successfully`);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
throw new LazyUtil_1.LazyError(`Failed to uninstall plugin '${pluginName}': ${error}`);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Gets a plugin by name.
|
|
90
|
+
* @param pluginName - The name of the plugin.
|
|
91
|
+
* @returns The plugin or undefined if not found.
|
|
92
|
+
*/
|
|
93
|
+
get(pluginName) {
|
|
94
|
+
return this.plugins.get(pluginName);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Lists all registered plugin names.
|
|
98
|
+
* @returns Array of plugin names.
|
|
99
|
+
*/
|
|
100
|
+
list() {
|
|
101
|
+
return Array.from(this.plugins.keys());
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Checks if a plugin is registered.
|
|
105
|
+
* @param pluginName - The name of the plugin.
|
|
106
|
+
* @returns True if the plugin is registered, false otherwise.
|
|
107
|
+
*/
|
|
108
|
+
has(pluginName) {
|
|
109
|
+
return this.plugins.has(pluginName);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Executes a hook for all plugins that implement it.
|
|
113
|
+
* @param hookName - The name of the hook to execute.
|
|
114
|
+
* @param args - Arguments to pass to the hook.
|
|
115
|
+
*/
|
|
116
|
+
executeHook(hookName, ...args) {
|
|
117
|
+
this.plugins.forEach(plugin => {
|
|
118
|
+
try {
|
|
119
|
+
const hook = plugin.hooks?.[hookName];
|
|
120
|
+
if (hook) {
|
|
121
|
+
hook(...args);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
if (this.debug) {
|
|
126
|
+
LazyUtil_1.LazyLog.log('error', `Error executing hook '${hookName}' for plugin '${plugin.name}': ${error}`);
|
|
127
|
+
}
|
|
128
|
+
// Execute onError hook for all plugins when a hook fails
|
|
129
|
+
this.executeErrorHook(error);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Executes the onError hook for all plugins when an error occurs.
|
|
135
|
+
* @param error - The error that occurred.
|
|
136
|
+
*/
|
|
137
|
+
executeErrorHook(error) {
|
|
138
|
+
this.plugins.forEach(plugin => {
|
|
139
|
+
try {
|
|
140
|
+
const errorHook = plugin.hooks?.onError;
|
|
141
|
+
if (errorHook) {
|
|
142
|
+
errorHook(this.canvas, error);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch (hookError) {
|
|
146
|
+
if (this.debug) {
|
|
147
|
+
LazyUtil_1.LazyLog.log('error', `Error in onError hook for plugin '${plugin.name}': ${hookError}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Gets plugin information.
|
|
154
|
+
* @returns Array of plugin information objects.
|
|
155
|
+
*/
|
|
156
|
+
getPluginInfo() {
|
|
157
|
+
return Array.from(this.plugins.values()).map(plugin => ({
|
|
158
|
+
name: plugin.name,
|
|
159
|
+
version: plugin.version,
|
|
160
|
+
description: plugin.description,
|
|
161
|
+
dependencies: plugin.dependencies
|
|
162
|
+
}));
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Clears all plugins.
|
|
166
|
+
*/
|
|
167
|
+
clear() {
|
|
168
|
+
const pluginNames = this.list();
|
|
169
|
+
for (const name of pluginNames) {
|
|
170
|
+
try {
|
|
171
|
+
this.unregister(name);
|
|
172
|
+
}
|
|
173
|
+
catch (error) {
|
|
174
|
+
if (this.debug) {
|
|
175
|
+
LazyUtil_1.LazyLog.log('error', `Error unregistering plugin '${name}': ${error}`);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
exports.PluginManager = PluginManager;
|
|
@@ -114,7 +114,10 @@ class RenderManager {
|
|
|
114
114
|
const { width, height } = this.lazyCanvas.options;
|
|
115
115
|
const delay = 1000 / this.lazyCanvas.manager.animation.options.frameRate;
|
|
116
116
|
const { loop, colorSpace, maxColors, transparency, utils } = this.lazyCanvas.manager.animation.options;
|
|
117
|
+
let frameNumber = 0;
|
|
117
118
|
for (const layer of this.lazyCanvas.manager.layers.toArray()) {
|
|
119
|
+
// onAnimationFrame hook
|
|
120
|
+
this.lazyCanvas.manager.plugins.executeHook('onAnimationFrame', this.lazyCanvas, frameNumber);
|
|
118
121
|
const ctx = await this.renderLayer(layer);
|
|
119
122
|
frameBuffer.push(ctx.getImageData(0, 0, width, height));
|
|
120
123
|
if (frameBuffer.length > utils.buffer.size) {
|
|
@@ -131,6 +134,7 @@ class RenderManager {
|
|
|
131
134
|
});
|
|
132
135
|
if (utils.clear)
|
|
133
136
|
ctx.clearRect(0, 0, width, height);
|
|
137
|
+
frameNumber++;
|
|
134
138
|
}
|
|
135
139
|
encoder.finish();
|
|
136
140
|
return encoder.bytesView();
|
|
@@ -141,28 +145,39 @@ class RenderManager {
|
|
|
141
145
|
* @returns {Promise<Buffer | SKRSContext2D | Canvas | SvgCanvas | string>} The rendered output in the specified format.
|
|
142
146
|
*/
|
|
143
147
|
async render(format) {
|
|
148
|
+
let result;
|
|
149
|
+
// beforeRender hook
|
|
150
|
+
this.lazyCanvas.manager.plugins.executeHook('beforeRender', this.lazyCanvas);
|
|
144
151
|
switch (format) {
|
|
145
152
|
case types_1.Export.BUFFER:
|
|
146
153
|
case "buffer":
|
|
147
154
|
if (this.lazyCanvas.options.animated) {
|
|
148
|
-
|
|
155
|
+
result = await this.renderAnimation();
|
|
149
156
|
}
|
|
150
157
|
else {
|
|
151
|
-
|
|
158
|
+
result = await this.renderStatic(types_1.Export.BUFFER);
|
|
152
159
|
}
|
|
160
|
+
break;
|
|
153
161
|
case types_1.Export.CTX:
|
|
154
162
|
case "ctx":
|
|
155
|
-
|
|
163
|
+
result = this.lazyCanvas.ctx;
|
|
164
|
+
break;
|
|
156
165
|
case types_1.Export.SVG:
|
|
157
166
|
case "svg":
|
|
158
|
-
|
|
167
|
+
result = await this.renderStatic(types_1.Export.SVG);
|
|
168
|
+
break;
|
|
159
169
|
case types_1.Export.CANVAS:
|
|
160
170
|
case "canvas":
|
|
161
171
|
await this.renderStatic(this.lazyCanvas.options.exportType === 'svg' ? types_1.Export.SVG : types_1.Export.BUFFER);
|
|
162
|
-
|
|
172
|
+
result = this.lazyCanvas.canvas;
|
|
173
|
+
break;
|
|
163
174
|
default:
|
|
164
|
-
|
|
175
|
+
result = await this.renderStatic(types_1.Export.BUFFER);
|
|
176
|
+
break;
|
|
165
177
|
}
|
|
178
|
+
// afterRender hook
|
|
179
|
+
this.lazyCanvas.manager.plugins.executeHook('afterRender', this.lazyCanvas);
|
|
180
|
+
return result;
|
|
166
181
|
}
|
|
167
182
|
}
|
|
168
183
|
exports.RenderManager = RenderManager;
|
package/dist/types/index.d.ts
CHANGED
package/dist/types/index.js
CHANGED
package/dist/utils/utils.js
CHANGED
|
@@ -1,6 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.generateID = generateID;
|
|
4
|
+
exports.isColor = isColor;
|
|
5
|
+
exports.parseToNormal = parseToNormal;
|
|
6
|
+
exports.parser = parser;
|
|
7
|
+
exports.drawShadow = drawShadow;
|
|
8
|
+
exports.opacity = opacity;
|
|
9
|
+
exports.filters = filters;
|
|
10
|
+
exports.parseFillStyle = parseFillStyle;
|
|
11
|
+
exports.transform = transform;
|
|
12
|
+
exports.generateRandomName = generateRandomName;
|
|
13
|
+
exports.isImageUrlValid = isImageUrlValid;
|
|
14
|
+
exports.centring = centring;
|
|
15
|
+
exports.getBoundingBoxBezier = getBoundingBoxBezier;
|
|
16
|
+
exports.resize = resize;
|
|
17
|
+
exports.resizeLayers = resizeLayers;
|
|
4
18
|
const types_1 = require("../types");
|
|
5
19
|
const helpers_1 = require("../structures/helpers");
|
|
6
20
|
const canvas_1 = require("@napi-rs/canvas");
|
|
@@ -9,7 +23,6 @@ const components_1 = require("../structures/components");
|
|
|
9
23
|
function generateID(type) {
|
|
10
24
|
return `${type}-${Math.random().toString(36).substr(2, 9)}`;
|
|
11
25
|
}
|
|
12
|
-
exports.generateID = generateID;
|
|
13
26
|
let percentReg = /^(\d+)%$/;
|
|
14
27
|
let pxReg = /^(\d+)px$/;
|
|
15
28
|
let canvasReg = /^(vw|vh|vmin|vmax)$/;
|
|
@@ -22,7 +35,6 @@ let hslaReg = /^hsla\((\d+),\s*(\d+)%,\s*(\d+)%,\s*(0|0?\.\d+|1(\.0)?)\)$/;
|
|
|
22
35
|
function isColor(v) {
|
|
23
36
|
return typeof (v === 'string' && (hexReg.test(v) || rgbReg.test(v) || rgbaReg.test(v) || hslReg.test(v) || hslaReg.test(v))) || v instanceof helpers_1.Gradient || v instanceof helpers_1.Pattern;
|
|
24
37
|
}
|
|
25
|
-
exports.isColor = isColor;
|
|
26
38
|
function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options = { vertical: false, layer: false }, manager) {
|
|
27
39
|
if (typeof v === 'number')
|
|
28
40
|
return v;
|
|
@@ -78,7 +90,6 @@ function parseToNormal(v, ctx, canvas, layer = { width: 0, height: 0 }, options
|
|
|
78
90
|
}
|
|
79
91
|
return 0;
|
|
80
92
|
}
|
|
81
|
-
exports.parseToNormal = parseToNormal;
|
|
82
93
|
function getLayerWidth(anyLayer, ctx, canvas, manager, parserInstance) {
|
|
83
94
|
if (anyLayer instanceof components_1.LineLayer || anyLayer instanceof components_1.BezierLayer || anyLayer instanceof components_1.QuadraticLayer) {
|
|
84
95
|
return anyLayer.getBoundingBox(ctx, canvas, manager).width;
|
|
@@ -112,7 +123,6 @@ function parser(ctx, canvas, manager) {
|
|
|
112
123
|
}
|
|
113
124
|
};
|
|
114
125
|
}
|
|
115
|
-
exports.parser = parser;
|
|
116
126
|
function drawShadow(ctx, shadow) {
|
|
117
127
|
if (shadow) {
|
|
118
128
|
ctx.shadowColor = shadow.color;
|
|
@@ -121,19 +131,16 @@ function drawShadow(ctx, shadow) {
|
|
|
121
131
|
ctx.shadowOffsetY = shadow.offsetY || 0;
|
|
122
132
|
}
|
|
123
133
|
}
|
|
124
|
-
exports.drawShadow = drawShadow;
|
|
125
134
|
function opacity(ctx, opacity = 1) {
|
|
126
135
|
if (opacity < 1) {
|
|
127
136
|
ctx.globalAlpha = opacity;
|
|
128
137
|
}
|
|
129
138
|
}
|
|
130
|
-
exports.opacity = opacity;
|
|
131
139
|
function filters(ctx, filters) {
|
|
132
140
|
if (filters) {
|
|
133
141
|
ctx.filter = filters;
|
|
134
142
|
}
|
|
135
143
|
}
|
|
136
|
-
exports.filters = filters;
|
|
137
144
|
function parseFillStyle(ctx, color, opts) {
|
|
138
145
|
if (!ctx)
|
|
139
146
|
throw new LazyUtil_1.LazyError('The context is not defined');
|
|
@@ -144,7 +151,6 @@ function parseFillStyle(ctx, color, opts) {
|
|
|
144
151
|
}
|
|
145
152
|
return color;
|
|
146
153
|
}
|
|
147
|
-
exports.parseFillStyle = parseFillStyle;
|
|
148
154
|
function transform(ctx, transform, layer = { width: 0, height: 0, x: 0, y: 0, type: types_1.LayerType.Morph }, extra = { text: '', textAlign: types_1.TextAlign.Left, fontSize: 0, multiline: false }) {
|
|
149
155
|
if (transform) {
|
|
150
156
|
if (transform.translate) {
|
|
@@ -197,11 +203,9 @@ function transform(ctx, transform, layer = { width: 0, height: 0, x: 0, y: 0, ty
|
|
|
197
203
|
}
|
|
198
204
|
}
|
|
199
205
|
}
|
|
200
|
-
exports.transform = transform;
|
|
201
206
|
function generateRandomName() {
|
|
202
207
|
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
|
|
203
208
|
}
|
|
204
|
-
exports.generateRandomName = generateRandomName;
|
|
205
209
|
function isImageUrlValid(src) {
|
|
206
210
|
try {
|
|
207
211
|
(0, canvas_1.loadImage)(src);
|
|
@@ -211,7 +215,6 @@ function isImageUrlValid(src) {
|
|
|
211
215
|
return false;
|
|
212
216
|
}
|
|
213
217
|
}
|
|
214
|
-
exports.isImageUrlValid = isImageUrlValid;
|
|
215
218
|
function centring(centring, type, width, height, x, y) {
|
|
216
219
|
switch (centring) {
|
|
217
220
|
case types_1.Centring.Center:
|
|
@@ -307,7 +310,6 @@ function centring(centring, type, width, height, x, y) {
|
|
|
307
310
|
return { x, y };
|
|
308
311
|
}
|
|
309
312
|
}
|
|
310
|
-
exports.centring = centring;
|
|
311
313
|
function quadraticBezier(p0, p1, p2, t) {
|
|
312
314
|
const x = (1 - t) ** 2 * p0.x + 2 * (1 - t) * t * p1.x + t ** 2 * p2.x;
|
|
313
315
|
const y = (1 - t) ** 2 * p0.y + 2 * (1 - t) * t * p1.y + t ** 2 * p2.y;
|
|
@@ -339,7 +341,6 @@ function getBoundingBoxBezier(points, steps = 100) {
|
|
|
339
341
|
}
|
|
340
342
|
return { min: { x: minX, y: minY }, max: { x: maxX, y: maxY }, center: { x: (minX + maxX) / 2, y: (minY + maxY) / 2 }, width: maxX - minX, height: maxY - minY };
|
|
341
343
|
}
|
|
342
|
-
exports.getBoundingBoxBezier = getBoundingBoxBezier;
|
|
343
344
|
function resize(value, ratio) {
|
|
344
345
|
if (typeof value === 'number') {
|
|
345
346
|
return value * ratio;
|
|
@@ -358,7 +359,6 @@ function resize(value, ratio) {
|
|
|
358
359
|
}
|
|
359
360
|
return value;
|
|
360
361
|
}
|
|
361
|
-
exports.resize = resize;
|
|
362
362
|
function resizeLayers(layers, ratio) {
|
|
363
363
|
let newLayers = [];
|
|
364
364
|
if (layers.length > 0) {
|
|
@@ -411,4 +411,3 @@ function resizeLayers(layers, ratio) {
|
|
|
411
411
|
}
|
|
412
412
|
return newLayers;
|
|
413
413
|
}
|
|
414
|
-
exports.resizeLayers = resizeLayers;
|