@nmmty/lazycanvas 0.3.6 → 0.4.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/index.d.ts CHANGED
@@ -5,10 +5,10 @@ import { TextLayer } from "./structures/components/TextLayer";
5
5
  import { BezierLayer } from "./structures/components/BezierLayer";
6
6
  import { QuadraticLayer } from "./structures/components/QuadraticLayer";
7
7
  import { LineLayer } from "./structures/components/LineLayer";
8
+ import { ClearLayer } from "./structures/components/ClearLayer";
8
9
  import { BaseLayer } from "./structures/components/BaseLayer";
9
10
  import { Group } from "./structures/components/Group";
10
11
  import { LayerType, LayerScaleType, LineCap, LineJoin, TextAlign, TextDirection, TextBaseline, FontWeight, Export, Centring, PatternType, SaveFormat, GradientType, LinkType } from "./types/enum";
11
- import type { AnyLayer, AnyCentring, AnyPatternType, AnyGradientType, AnyTextAlign, AnyTextDirection, AnyTextBaseline, AnyWeight, AnyExport, AnyLineCap, AnyLineJoin, AnySaveFormat, AnyLinkType, ScaleType, ColorType, IFont, IFonts, IGradient, IPattern, IImageLayer, IImageLayerProps, IMorphLayer, IMorphLayerProps, ITextLayer, ITextLayerProps, IBezierLayer, IBezierLayerProps, IQuadraticLayer, IQuadraticLayerProps, ILineLayer, ILineLayerProps, IBaseLayer, IBaseLayerProps, IGroup } from "./types";
12
12
  import { Font } from "./structures/helpers/Font";
13
13
  import { Gradient } from "./structures/helpers/Gradient";
14
14
  import { Pattern } from "./structures/helpers/Pattern";
@@ -16,5 +16,5 @@ import { Link } from "./structures/helpers/Link";
16
16
  import { saveFile } from "./utils/utils";
17
17
  import { Filters } from "./helpers/Filters";
18
18
  import { FontsList } from "./helpers/FontsList";
19
- export { LazyCanvas, ImageLayer, MorphLayer, TextLayer, BezierLayer, QuadraticLayer, LineLayer, BaseLayer, Group, Font, Gradient, Pattern, Link, LayerScaleType, LayerType, FontWeight, GradientType, Export, LineCap, LineJoin, TextAlign, TextDirection, TextBaseline, SaveFormat, Centring, PatternType, LinkType, saveFile, Filters, FontsList, };
20
- export type { IFont, IFonts, IGradient, IPattern, IImageLayer, IImageLayerProps, IMorphLayer, IMorphLayerProps, ITextLayer, ITextLayerProps, IBaseLayer, IBaseLayerProps, IBezierLayer, IBezierLayerProps, IQuadraticLayer, IQuadraticLayerProps, ILineLayer, ILineLayerProps, IGroup, AnyLayer, AnyCentring, AnyPatternType, AnyGradientType, AnyTextAlign, AnyTextDirection, AnyTextBaseline, AnyWeight, AnyExport, AnyLineCap, AnyLineJoin, AnySaveFormat, AnyLinkType, ScaleType, ColorType };
19
+ export { LazyCanvas, ImageLayer, MorphLayer, TextLayer, BezierLayer, QuadraticLayer, LineLayer, ClearLayer, BaseLayer, Group, Font, Gradient, Pattern, Link, LayerScaleType, LayerType, FontWeight, GradientType, Export, LineCap, LineJoin, TextAlign, TextDirection, TextBaseline, SaveFormat, Centring, PatternType, LinkType, saveFile, Filters, FontsList, };
20
+ export type { AnyLayer, AnyCentring, AnyPatternType, AnyGradientType, AnyTextAlign, AnyTextDirection, AnyTextBaseline, AnyWeight, AnyExport, AnyLineCap, AnyLineJoin, AnySaveFormat, AnyLinkType, AnyGlobalCompositeOperation, AnyColorSpace, ScaleType, ColorType, Point, PointNumber, GradientPoint, GradientColorStop, Transform, ILazyCanvas, IFont, IFonts, IGradient, IPattern, IImageLayer, IImageLayerProps, IMorphLayer, IMorphLayerProps, ITextLayer, ITextLayerProps, IBezierLayer, IBezierLayerProps, IQuadraticLayer, IQuadraticLayerProps, ILineLayer, ILineLayerProps, IBaseLayer, IBaseLayerProps, IGroup, ILink, IFontsManager, ILayersManager, IRenderManager } from "./types";
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FontsList = exports.Filters = exports.saveFile = exports.LinkType = exports.PatternType = exports.Centring = exports.SaveFormat = exports.TextBaseline = exports.TextDirection = exports.TextAlign = exports.LineJoin = exports.LineCap = exports.Export = exports.GradientType = exports.FontWeight = exports.LayerType = exports.LayerScaleType = exports.Link = exports.Pattern = exports.Gradient = exports.Font = exports.Group = exports.BaseLayer = exports.LineLayer = exports.QuadraticLayer = exports.BezierLayer = exports.TextLayer = exports.MorphLayer = exports.ImageLayer = exports.LazyCanvas = void 0;
3
+ exports.FontsList = exports.Filters = exports.saveFile = exports.LinkType = exports.PatternType = exports.Centring = exports.SaveFormat = exports.TextBaseline = exports.TextDirection = exports.TextAlign = exports.LineJoin = exports.LineCap = exports.Export = exports.GradientType = exports.FontWeight = exports.LayerType = exports.LayerScaleType = exports.Link = exports.Pattern = exports.Gradient = exports.Font = exports.Group = exports.BaseLayer = exports.ClearLayer = exports.LineLayer = exports.QuadraticLayer = exports.BezierLayer = exports.TextLayer = exports.MorphLayer = exports.ImageLayer = exports.LazyCanvas = void 0;
4
4
  const LazyCanvas_1 = require("./structures/LazyCanvas");
5
5
  Object.defineProperty(exports, "LazyCanvas", { enumerable: true, get: function () { return LazyCanvas_1.LazyCanvas; } });
6
6
  const ImageLayer_1 = require("./structures/components/ImageLayer");
@@ -15,6 +15,8 @@ const QuadraticLayer_1 = require("./structures/components/QuadraticLayer");
15
15
  Object.defineProperty(exports, "QuadraticLayer", { enumerable: true, get: function () { return QuadraticLayer_1.QuadraticLayer; } });
16
16
  const LineLayer_1 = require("./structures/components/LineLayer");
17
17
  Object.defineProperty(exports, "LineLayer", { enumerable: true, get: function () { return LineLayer_1.LineLayer; } });
18
+ const ClearLayer_1 = require("./structures/components/ClearLayer");
19
+ Object.defineProperty(exports, "ClearLayer", { enumerable: true, get: function () { return ClearLayer_1.ClearLayer; } });
18
20
  const BaseLayer_1 = require("./structures/components/BaseLayer");
19
21
  Object.defineProperty(exports, "BaseLayer", { enumerable: true, get: function () { return BaseLayer_1.BaseLayer; } });
20
22
  const Group_1 = require("./structures/components/Group");
@@ -3,6 +3,7 @@ import { Canvas, SKRSContext2D, SvgExportFlag } from "@napi-rs/canvas";
3
3
  import { LayersManager } from "./managers/LayersManager";
4
4
  import { RenderManager } from "./managers/RenderManager";
5
5
  import { FontsManager } from "./managers/FontsManager";
6
+ import { AnimationManager } from "./managers/AnimationManager";
6
7
  export declare class LazyCanvas implements ILazyCanvas {
7
8
  width: number | 0;
8
9
  height: number | 0;
@@ -11,6 +12,7 @@ export declare class LazyCanvas implements ILazyCanvas {
11
12
  layers: LayersManager;
12
13
  render: RenderManager;
13
14
  fonts: FontsManager;
15
+ animation: AnimationManager;
14
16
  exportType: AnyExport;
15
17
  constructor(debug?: boolean);
16
18
  /**
@@ -6,6 +6,7 @@ const canvas_1 = require("@napi-rs/canvas");
6
6
  const LayersManager_1 = require("./managers/LayersManager");
7
7
  const RenderManager_1 = require("./managers/RenderManager");
8
8
  const FontsManager_1 = require("./managers/FontsManager");
9
+ const AnimationManager_1 = require("./managers/AnimationManager");
9
10
  class LazyCanvas {
10
11
  width;
11
12
  height;
@@ -14,6 +15,7 @@ class LazyCanvas {
14
15
  layers;
15
16
  render;
16
17
  fonts;
18
+ animation;
17
19
  exportType;
18
20
  constructor(debug = false) {
19
21
  this.width = 0;
@@ -23,6 +25,7 @@ class LazyCanvas {
23
25
  this.layers = new LayersManager_1.LayersManager(debug);
24
26
  this.render = new RenderManager_1.RenderManager(this, debug);
25
27
  this.fonts = new FontsManager_1.FontsManager(debug);
28
+ this.animation = new AnimationManager_1.AnimationManager(debug);
26
29
  this.exportType = enum_1.Export.Buffer;
27
30
  }
28
31
  /**
@@ -0,0 +1,39 @@
1
+ import { IClearLayer, IClearLayerProps, ScaleType } from "../../types";
2
+ import { LayerType } from "../../types/enum";
3
+ import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
4
+ import { LayersManager } from "../managers/LayersManager";
5
+ export declare class ClearLayer implements IClearLayer {
6
+ id: string;
7
+ type: LayerType;
8
+ zIndex: number;
9
+ visible: boolean;
10
+ props: IClearLayerProps;
11
+ constructor(props?: IClearLayerProps);
12
+ /**
13
+ * @description Position of the layer in the 2D plane. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
14
+ * @param x {ScaleType} - The `x` position of the layer
15
+ * @param y {ScaleType} - The `y` position of the layer
16
+ */
17
+ setPosition(x: ScaleType, y: ScaleType): this;
18
+ /**
19
+ * @description Sets the size of the layer.
20
+ * @param width {ScaleType} - The `width` of the layer
21
+ * @param height {ScaleType} - The `height` of the
22
+ */
23
+ setSize(width: ScaleType, height: ScaleType): this;
24
+ /**
25
+ * @description Sets the visibility of the layer.
26
+ * @param visible {boolean} - The `visibility` of the layer
27
+ */
28
+ setVisible(visible: boolean): this;
29
+ /**
30
+ * @description Sets zIndex of the layer.
31
+ * @param zIndex {number} - The `zIndex` of the layer
32
+ */
33
+ setZIndex(zIndex: number): this;
34
+ draw(ctx: SKRSContext2D, canvas: Canvas, manager: LayersManager, debug: boolean): Promise<void>;
35
+ /**
36
+ * @returns {IClearLayer}
37
+ */
38
+ toJSON(): IClearLayer;
39
+ }
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ClearLayer = void 0;
4
+ const enum_1 = require("../../types/enum");
5
+ const utils_1 = require("../../utils/utils");
6
+ const LazyUtil_1 = require("../../utils/LazyUtil");
7
+ const utils_2 = require("../../utils/utils");
8
+ class ClearLayer {
9
+ id;
10
+ type;
11
+ zIndex;
12
+ visible;
13
+ props;
14
+ constructor(props) {
15
+ this.id = (0, utils_2.generateID)(enum_1.LayerType.Clear);
16
+ this.type = enum_1.LayerType.Clear;
17
+ this.zIndex = 1;
18
+ this.visible = true;
19
+ this.props = props ? props : {
20
+ x: 0,
21
+ y: 0,
22
+ size: {
23
+ width: 0,
24
+ height: 0
25
+ }
26
+ };
27
+ }
28
+ /**
29
+ * @description Position of the layer in the 2D plane. You can use `numbers`, `percentages`, `px`, `vw`, `vh`, `vmin`, `vmax`.
30
+ * @param x {ScaleType} - The `x` position of the layer
31
+ * @param y {ScaleType} - The `y` position of the layer
32
+ */
33
+ setPosition(x, y) {
34
+ this.props.x = x;
35
+ this.props.y = y;
36
+ return this;
37
+ }
38
+ /**
39
+ * @description Sets the size of the layer.
40
+ * @param width {ScaleType} - The `width` of the layer
41
+ * @param height {ScaleType} - The `height` of the
42
+ */
43
+ setSize(width, height) {
44
+ this.props.size = {
45
+ width,
46
+ height
47
+ };
48
+ return this;
49
+ }
50
+ /**
51
+ * @description Sets the visibility of the layer.
52
+ * @param visible {boolean} - The `visibility` of the layer
53
+ */
54
+ setVisible(visible) {
55
+ this.visible = visible;
56
+ return this;
57
+ }
58
+ /**
59
+ * @description Sets zIndex of the layer.
60
+ * @param zIndex {number} - The `zIndex` of the layer
61
+ */
62
+ setZIndex(zIndex) {
63
+ this.zIndex = zIndex;
64
+ return this;
65
+ }
66
+ async draw(ctx, canvas, manager, debug) {
67
+ const parcer = (0, utils_1.parser)(ctx, canvas, manager);
68
+ const { x, y, w } = parcer.parseBatch({
69
+ x: { v: this.props.x },
70
+ y: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
71
+ w: { v: this.props.size.width },
72
+ });
73
+ const h = parcer.parse(this.props.size.height, LazyUtil_1.defaultArg.wh(w), LazyUtil_1.defaultArg.vl(true));
74
+ if (debug)
75
+ LazyUtil_1.LazyLog.log('none', `ClearLayer:`, { x, y, w, h });
76
+ ctx.clearRect(x, y, w, h);
77
+ }
78
+ /**
79
+ * @returns {IClearLayer}
80
+ */
81
+ toJSON() {
82
+ return {
83
+ id: this.id,
84
+ type: this.type,
85
+ zIndex: this.zIndex,
86
+ visible: this.visible,
87
+ props: this.props,
88
+ };
89
+ }
90
+ }
91
+ exports.ClearLayer = ClearLayer;
@@ -31,7 +31,7 @@ export declare class Group implements IGroup {
31
31
  */
32
32
  remove(id: string): this;
33
33
  /**
34
- * Clear all components from the group
34
+ * ClearLayer all components from the group
35
35
  */
36
36
  clear(): this;
37
37
  /**
@@ -58,7 +58,7 @@ class Group {
58
58
  return this;
59
59
  }
60
60
  /**
61
- * Clear all components from the group
61
+ * ClearLayer all components from the group
62
62
  */
63
63
  clear() {
64
64
  this.components = [];
@@ -63,12 +63,12 @@ class ImageLayer extends BaseLayer_1.BaseLayer {
63
63
  }
64
64
  async draw(ctx, canvas, manager, debug) {
65
65
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
66
- const { xs, ys, w, h } = parcer.parseBatch({
66
+ const { xs, ys, w } = parcer.parseBatch({
67
67
  xs: { v: this.props.x },
68
68
  ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
69
- w: { v: this.props.size.width },
70
- h: { v: this.props.size.height, options: LazyUtil_1.defaultArg.vl(true) },
69
+ w: { v: this.props.size.width }
71
70
  });
71
+ const h = parcer.parse(this.props.size.height, LazyUtil_1.defaultArg.wh(w), LazyUtil_1.defaultArg.vl(true));
72
72
  const r = parcer.parse(this.props.size.radius, LazyUtil_1.defaultArg.wh(w / 2, h / 2), LazyUtil_1.defaultArg.vl(false, true));
73
73
  let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
74
74
  if (debug)
@@ -82,12 +82,12 @@ class MorphLayer extends BaseLayer_1.BaseLayer {
82
82
  }
83
83
  async draw(ctx, canvas, manager, debug) {
84
84
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
85
- const { xs, ys, w, h } = parcer.parseBatch({
85
+ const { xs, ys, w } = parcer.parseBatch({
86
86
  xs: { v: this.props.x },
87
87
  ys: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
88
88
  w: { v: this.props.size.width },
89
- h: { v: this.props.size.height, options: LazyUtil_1.defaultArg.vl(true) },
90
89
  });
90
+ const h = parcer.parse(this.props.size.height, LazyUtil_1.defaultArg.wh(w), LazyUtil_1.defaultArg.vl(true));
91
91
  const r = parcer.parse(this.props.size.radius, LazyUtil_1.defaultArg.wh(w / 2, h / 2), LazyUtil_1.defaultArg.vl(false, true));
92
92
  let { x, y } = (0, utils_1.centring)(this.props.centring, this.type, w, h, xs, ys);
93
93
  let fillStyle = await (0, utils_1.parseFillStyle)(ctx, this.props.fillStyle);
@@ -187,12 +187,12 @@ class TextLayer extends BaseLayer_1.BaseLayer {
187
187
  }
188
188
  async draw(ctx, canvas, manager, debug) {
189
189
  const parcer = (0, utils_1.parser)(ctx, canvas, manager);
190
- const { x, y, w, h } = parcer.parseBatch({
190
+ const { x, y, w } = parcer.parseBatch({
191
191
  x: { v: this.props.x },
192
192
  y: { v: this.props.y, options: LazyUtil_1.defaultArg.vl(true) },
193
193
  w: { v: this.props.size?.width },
194
- h: { v: this.props.size?.height, options: LazyUtil_1.defaultArg.vl(true) },
195
194
  });
195
+ const h = parcer.parse(this.props.size?.height, LazyUtil_1.defaultArg.wh(w), LazyUtil_1.defaultArg.vl(true));
196
196
  if (debug)
197
197
  LazyUtil_1.LazyLog.log('none', `TextLayer:`, { x, y, w, h });
198
198
  ctx.save();
@@ -0,0 +1,44 @@
1
+ import type { IAnimationManager, AnyColorSpace } from "../../types";
2
+ export declare class AnimationManager implements IAnimationManager {
3
+ opts: {
4
+ frameRate: number;
5
+ maxColors: number;
6
+ colorSpace: AnyColorSpace;
7
+ loop: boolean;
8
+ transparency: boolean;
9
+ clear: boolean;
10
+ };
11
+ animated: boolean;
12
+ debug: boolean;
13
+ constructor(debug?: boolean);
14
+ /**
15
+ * Sets the frame rate of the animation.
16
+ * @param frameRate {number} - The frame rate of the animation (by default 30).
17
+ */
18
+ setFrameRate(frameRate: number): this;
19
+ /**
20
+ * Sets the loop of the animation.
21
+ * @param loop {boolean} - Whether the animation should loop or not (by default true).
22
+ */
23
+ setLoop(loop: boolean): this;
24
+ /**
25
+ * Sets the transparency of the animation.
26
+ * @param transparency {boolean} - Whether the animation should have transparency or not (by default true).
27
+ */
28
+ setTransparent(transparency: boolean): this;
29
+ /**
30
+ * Sets the RGB format of the animation.
31
+ * @param rgb {ColorSpace} - The RGB format of the animation (by default RGB565).
32
+ */
33
+ setRGBFormat(rgb: AnyColorSpace): this;
34
+ /**
35
+ * Sets the maximum colors of the animation.
36
+ * @param maxColors {number} - The maximum colors of the animation (by default 256).
37
+ */
38
+ setMaxColors(maxColors: number): this;
39
+ /**
40
+ * Sets whether the content of previous frames will be cleared.
41
+ * @param clear {boolean} - Whether the animation should clear or not (by default true).
42
+ */
43
+ setClear(clear: boolean): this;
44
+ }
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AnimationManager = void 0;
4
+ const enum_1 = require("../../types/enum");
5
+ class AnimationManager {
6
+ opts;
7
+ animated;
8
+ debug;
9
+ constructor(debug = false) {
10
+ this.opts = {
11
+ frameRate: 30,
12
+ maxColors: 256,
13
+ colorSpace: enum_1.ColorSpace.RGB565,
14
+ loop: true,
15
+ transparency: true,
16
+ clear: true
17
+ };
18
+ this.animated = false;
19
+ this.debug = debug;
20
+ }
21
+ /**
22
+ * Sets the frame rate of the animation.
23
+ * @param frameRate {number} - The frame rate of the animation (by default 30).
24
+ */
25
+ setFrameRate(frameRate) {
26
+ this.opts.frameRate = frameRate;
27
+ this.animated = true;
28
+ return this;
29
+ }
30
+ /**
31
+ * Sets the loop of the animation.
32
+ * @param loop {boolean} - Whether the animation should loop or not (by default true).
33
+ */
34
+ setLoop(loop) {
35
+ this.opts.loop = loop;
36
+ return this;
37
+ }
38
+ /**
39
+ * Sets the transparency of the animation.
40
+ * @param transparency {boolean} - Whether the animation should have transparency or not (by default true).
41
+ */
42
+ setTransparent(transparency) {
43
+ this.opts.transparency = transparency;
44
+ return this;
45
+ }
46
+ /**
47
+ * Sets the RGB format of the animation.
48
+ * @param rgb {ColorSpace} - The RGB format of the animation (by default RGB565).
49
+ */
50
+ setRGBFormat(rgb) {
51
+ this.opts.colorSpace = rgb;
52
+ return this;
53
+ }
54
+ /**
55
+ * Sets the maximum colors of the animation.
56
+ * @param maxColors {number} - The maximum colors of the animation (by default 256).
57
+ */
58
+ setMaxColors(maxColors) {
59
+ this.opts.maxColors = maxColors;
60
+ return this;
61
+ }
62
+ /**
63
+ * Sets whether the content of previous frames will be cleared.
64
+ * @param clear {boolean} - Whether the animation should clear or not (by default true).
65
+ */
66
+ setClear(clear) {
67
+ this.opts.clear = clear;
68
+ return this;
69
+ }
70
+ }
71
+ exports.AnimationManager = AnimationManager;
@@ -26,7 +26,7 @@ export declare class FontsManager implements IFontsManager {
26
26
  weight: string;
27
27
  }>): this;
28
28
  /**
29
- * Clear all fonts from the map
29
+ * ClearLayer all fonts from the map
30
30
  */
31
31
  clear(): this;
32
32
  /**
@@ -73,7 +73,7 @@ class FontsManager {
73
73
  return this;
74
74
  }
75
75
  /**
76
- * Clear all fonts from the map
76
+ * ClearLayer all fonts from the map
77
77
  */
78
78
  clear() {
79
79
  this.map.clear();
@@ -16,7 +16,7 @@ export declare class LayersManager implements ILayersManager {
16
16
  */
17
17
  remove(...ids: string[]): this;
18
18
  /**
19
- * Clear all layers from the map
19
+ * ClearLayer all layers from the map
20
20
  */
21
21
  clear(): this;
22
22
  /**
@@ -40,7 +40,7 @@ class LayersManager {
40
40
  return this;
41
41
  }
42
42
  /**
43
- * Clear all layers from the map
43
+ * ClearLayer all layers from the map
44
44
  */
45
45
  clear() {
46
46
  this.map.clear();
@@ -128,7 +128,7 @@ class LayersManager {
128
128
  this.fromArray(this.toArray().sort((a, b) => a.zIndex - b.zIndex));
129
129
  }
130
130
  crossSearch(id) {
131
- for (const layer of this.map.values()) {
131
+ for (const layer of Array.from(this.map.values())) {
132
132
  if (layer.id === id)
133
133
  return layer;
134
134
  if (layer instanceof Group_1.Group) {
@@ -7,6 +7,9 @@ export declare class RenderManager implements IRenderManager {
7
7
  lazyCanvas: LazyCanvas;
8
8
  debug: boolean;
9
9
  constructor(lazyCanvas: LazyCanvas, debug?: boolean);
10
+ private renderLayer;
11
+ private renderStatic;
12
+ private renderAnimation;
10
13
  /**
11
14
  * This will render all the layers and return the rendered canvas buffer or ctx.
12
15
  * @returns {Promise<Buffer | SKRSContext2D>}
@@ -4,6 +4,8 @@ exports.RenderManager = void 0;
4
4
  const enum_1 = require("../../types/enum");
5
5
  const Group_1 = require("../components/Group");
6
6
  const LazyUtil_1 = require("../../utils/LazyUtil");
7
+ // @ts-ignore
8
+ const gifenc_1 = require("gifenc");
7
9
  class RenderManager {
8
10
  lazyCanvas;
9
11
  debug;
@@ -11,37 +13,39 @@ class RenderManager {
11
13
  this.lazyCanvas = lazyCanvas;
12
14
  this.debug = debug;
13
15
  }
14
- /**
15
- * This will render all the layers and return the rendered canvas buffer or ctx.
16
- * @returns {Promise<Buffer | SKRSContext2D>}
17
- */
18
- async render() {
19
- for (const layer of this.lazyCanvas.layers.toArray()) {
20
- if (this.debug)
21
- LazyUtil_1.LazyLog.log('info', `Rendering ${layer.id}...\nData:`, layer.toJSON());
22
- if (layer.visible) {
23
- if (layer instanceof Group_1.Group) {
24
- for (const subLayer of layer.components) {
25
- if (subLayer.visible) {
26
- if (!subLayer.props.globalComposite)
27
- this.lazyCanvas.ctx.globalCompositeOperation = 'source-over';
28
- else
29
- this.lazyCanvas.ctx.globalCompositeOperation = subLayer.props.globalComposite;
30
- await subLayer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.layers, this.debug);
31
- }
16
+ async renderLayer(layer) {
17
+ if (this.debug)
18
+ LazyUtil_1.LazyLog.log('info', `Rendering ${layer.id}...\nData:`, layer.toJSON());
19
+ if (layer.visible) {
20
+ if (layer instanceof Group_1.Group) {
21
+ for (const subLayer of layer.components) {
22
+ if (subLayer.visible) {
23
+ if ('globalComposite' in subLayer.props && subLayer.props.globalComposite)
24
+ this.lazyCanvas.ctx.globalCompositeOperation = subLayer.props.globalComposite;
25
+ else
26
+ this.lazyCanvas.ctx.globalCompositeOperation = 'source-over';
27
+ await subLayer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.layers, this.debug);
32
28
  }
33
29
  }
34
- else {
35
- if (!layer.props.globalComposite)
36
- this.lazyCanvas.ctx.globalCompositeOperation = 'source-over';
37
- else
38
- this.lazyCanvas.ctx.globalCompositeOperation = layer.props.globalComposite;
39
- await layer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.layers, this.debug);
40
- }
41
- this.lazyCanvas.ctx.shadowColor = 'transparent';
42
30
  }
31
+ else {
32
+ if ('globalComposite' in layer.props && layer.props.globalComposite)
33
+ this.lazyCanvas.ctx.globalCompositeOperation = layer.props.globalComposite;
34
+ else
35
+ this.lazyCanvas.ctx.globalCompositeOperation = 'source-over';
36
+ await layer.draw(this.lazyCanvas.ctx, this.lazyCanvas.canvas, this.lazyCanvas.layers, this.debug);
37
+ }
38
+ this.lazyCanvas.ctx.shadowColor = 'transparent';
43
39
  }
44
- switch (this.lazyCanvas.exportType) {
40
+ return this.lazyCanvas.ctx;
41
+ }
42
+ async renderStatic(exportType) {
43
+ if (this.debug)
44
+ LazyUtil_1.LazyLog.log('info', `Rendering static...`);
45
+ for (const layer of this.lazyCanvas.layers.toArray()) {
46
+ await this.renderLayer(layer);
47
+ }
48
+ switch (exportType) {
45
49
  case enum_1.Export.Buffer:
46
50
  case "buffer":
47
51
  return this.lazyCanvas.canvas.toBuffer('image/png');
@@ -56,5 +60,50 @@ class RenderManager {
56
60
  return this.lazyCanvas.canvas.toBuffer('image/png');
57
61
  }
58
62
  }
63
+ async renderAnimation() {
64
+ const encoder = new gifenc_1.GIFEncoder();
65
+ if (this.debug)
66
+ LazyUtil_1.LazyLog.log('info', `Rendering animation...\nData:`, this.lazyCanvas.animation.opts);
67
+ for (const layer of this.lazyCanvas.layers.toArray()) {
68
+ const ctx = await this.renderLayer(layer);
69
+ let { data, width, height } = ctx.getImageData(0, 0, this.lazyCanvas.width, this.lazyCanvas.height);
70
+ const palette = (0, gifenc_1.quantize)(data, this.lazyCanvas.animation.opts.maxColors, { format: this.lazyCanvas.animation.opts.colorSpace });
71
+ const index = (0, gifenc_1.applyPalette)(data, palette, this.lazyCanvas.animation.opts.colorSpace);
72
+ encoder.writeFrame(index, width, height, {
73
+ palette,
74
+ transparent: this.lazyCanvas.animation.opts.transparency,
75
+ delay: 1000 / this.lazyCanvas.animation.opts.frameRate,
76
+ loop: this.lazyCanvas.animation.opts.loop
77
+ });
78
+ if (this.lazyCanvas.animation.opts.clear)
79
+ ctx.clearRect(0, 0, this.lazyCanvas.width, this.lazyCanvas.height);
80
+ }
81
+ encoder.finish();
82
+ return encoder.bytesView();
83
+ }
84
+ /**
85
+ * This will render all the layers and return the rendered canvas buffer or ctx.
86
+ * @returns {Promise<Buffer | SKRSContext2D>}
87
+ */
88
+ async render() {
89
+ switch (this.lazyCanvas.exportType) {
90
+ case enum_1.Export.Buffer:
91
+ case "buffer":
92
+ if (this.lazyCanvas.animation.animated) {
93
+ return await this.renderAnimation();
94
+ }
95
+ else {
96
+ return await this.renderStatic(enum_1.Export.Buffer);
97
+ }
98
+ case enum_1.Export.CTX:
99
+ case "ctx":
100
+ return this.lazyCanvas.ctx;
101
+ case enum_1.Export.SVG:
102
+ case "svg":
103
+ return this.renderStatic(enum_1.Export.SVG);
104
+ default:
105
+ return this.renderStatic(enum_1.Export.Buffer);
106
+ }
107
+ }
59
108
  }
60
109
  exports.RenderManager = RenderManager;
@@ -2,6 +2,7 @@ import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
2
2
  import { LayersManager } from "../structures/managers/LayersManager";
3
3
  import { RenderManager } from "../structures/managers/RenderManager";
4
4
  import { FontsManager } from "../structures/managers/FontsManager";
5
+ import { AnimationManager } from "../structures/managers/AnimationManager";
5
6
  import { AnyExport } from "../";
6
7
 
7
8
  export interface ILazyCanvas {
@@ -12,5 +13,6 @@ export interface ILazyCanvas {
12
13
  layers: LayersManager;
13
14
  render: RenderManager;
14
15
  fonts: FontsManager;
16
+ animation: AnimationManager;
15
17
  exportType: AnyExport;
16
18
  }
@@ -0,0 +1,19 @@
1
+ import { ScaleType } from "../types";
2
+ import { LayerType } from "../enum";
3
+
4
+ export interface IClearLayer {
5
+ id: string;
6
+ type: LayerType;
7
+ zIndex: number;
8
+ visible: boolean;
9
+ props: IClearLayerProps;
10
+ }
11
+
12
+ export interface IClearLayerProps {
13
+ x: ScaleType;
14
+ y: ScaleType;
15
+ size: {
16
+ width: ScaleType;
17
+ height: ScaleType;
18
+ };
19
+ }
@@ -10,7 +10,8 @@ export declare enum LayerType {
10
10
  QuadraticCurve = "quadraticCurve",
11
11
  Morph = "morph",
12
12
  Text = "text",
13
- Group = "group"
13
+ Group = "group",
14
+ Clear = "clear"
14
15
  }
15
16
  export declare enum LayerScaleType {
16
17
  Pixel = "px",
@@ -74,7 +75,9 @@ export declare enum SaveFormat {
74
75
  PNG = "png",
75
76
  JPEG = "jpeg",
76
77
  JPG = "jpg",
77
- SVG = "svg"
78
+ SVG = "svg",
79
+ GIF = "gif",
80
+ WEBP = "webp"
78
81
  }
79
82
  export declare enum Centring {
80
83
  Start = "start",
@@ -128,3 +131,8 @@ export declare enum GlobalCompositeOperation {
128
131
  Color = "color",
129
132
  Luminosity = "luminosity"
130
133
  }
134
+ export declare enum ColorSpace {
135
+ RGB565 = "rgb565",
136
+ RGBA4444 = "rgba4444",
137
+ RGBA444 = "rgba444"
138
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.GlobalCompositeOperation = exports.LinkType = exports.PatternType = exports.Centring = exports.SaveFormat = exports.Export = exports.LineJoin = exports.LineCap = exports.TextDirection = exports.TextBaseline = exports.TextAlign = exports.FontWeight = exports.GradientType = exports.LayerScaleType = exports.LayerType = void 0;
3
+ exports.ColorSpace = exports.GlobalCompositeOperation = exports.LinkType = exports.PatternType = exports.Centring = exports.SaveFormat = exports.Export = exports.LineJoin = exports.LineCap = exports.TextDirection = exports.TextBaseline = exports.TextAlign = exports.FontWeight = exports.GradientType = exports.LayerScaleType = exports.LayerType = void 0;
4
4
  var LayerType;
5
5
  (function (LayerType) {
6
6
  LayerType["Base"] = "base";
@@ -15,6 +15,7 @@ var LayerType;
15
15
  LayerType["Morph"] = "morph";
16
16
  LayerType["Text"] = "text";
17
17
  LayerType["Group"] = "group";
18
+ LayerType["Clear"] = "clear";
18
19
  })(LayerType || (exports.LayerType = LayerType = {}));
19
20
  var LayerScaleType;
20
21
  (function (LayerScaleType) {
@@ -89,6 +90,8 @@ var SaveFormat;
89
90
  SaveFormat["JPEG"] = "jpeg";
90
91
  SaveFormat["JPG"] = "jpg";
91
92
  SaveFormat["SVG"] = "svg";
93
+ SaveFormat["GIF"] = "gif";
94
+ SaveFormat["WEBP"] = "webp";
92
95
  })(SaveFormat || (exports.SaveFormat = SaveFormat = {}));
93
96
  var Centring;
94
97
  (function (Centring) {
@@ -146,3 +149,9 @@ var GlobalCompositeOperation;
146
149
  GlobalCompositeOperation["Color"] = "color";
147
150
  GlobalCompositeOperation["Luminosity"] = "luminosity";
148
151
  })(GlobalCompositeOperation || (exports.GlobalCompositeOperation = GlobalCompositeOperation = {}));
152
+ var ColorSpace;
153
+ (function (ColorSpace) {
154
+ ColorSpace["RGB565"] = "rgb565";
155
+ ColorSpace["RGBA4444"] = "rgba4444";
156
+ ColorSpace["RGBA444"] = "rgba444";
157
+ })(ColorSpace || (exports.ColorSpace = ColorSpace = {}));
@@ -7,6 +7,7 @@ export type * from "./components/BezierLayer";
7
7
  export type * from "./components/QuadraticLayer";
8
8
  export type * from "./components/LineLayer";
9
9
  export type * from "./components/Group";
10
+ export type * from "./components/ClearLayer";
10
11
  export type * from "./helpers/Font";
11
12
  export type * from "./helpers/Gradient";
12
13
  export type * from "./helpers/Pattern";
@@ -14,4 +15,5 @@ export type * from "./helpers/Link";
14
15
  export type * from "./managers/LayersManager";
15
16
  export type * from "./managers/RenderManager";
16
17
  export type * from "./managers/FontsManager";
18
+ export type * from "./managers/AnimationManager";
17
19
  export type * from "./LazyCanvas";
@@ -0,0 +1,14 @@
1
+ import { AnyColorSpace } from "../types";
2
+
3
+ export interface IAnimationManager {
4
+ opts: {
5
+ frameRate: number;
6
+ maxColors: number;
7
+ colorSpace: AnyColorSpace;
8
+ loop: boolean;
9
+ transparency: boolean;
10
+ clear: boolean;
11
+ };
12
+ animated: boolean;
13
+ debug: boolean;
14
+ }
@@ -7,6 +7,7 @@ import { BezierLayer } from "../structures/components/BezierLayer";
7
7
  import { QuadraticLayer } from "../structures/components/QuadraticLayer";
8
8
  import { LineLayer } from "../structures/components/LineLayer";
9
9
  import { Group } from "../structures/components/Group";
10
+ import { ClearLayer } from "../structures/components/ClearLayer";
10
11
  import { Link } from "../structures/helpers/Link";
11
12
  import {
12
13
  FontWeight,
@@ -21,14 +22,15 @@ import {
21
22
  PatternType,
22
23
  SaveFormat,
23
24
  LinkType,
24
- GlobalCompositeOperation
25
+ GlobalCompositeOperation,
26
+ ColorSpace
25
27
  } from "./enum";
26
28
 
27
29
  export type ScaleType = string | number | 'vw' | 'vh' | 'vmin' | 'vmax' | Link;
28
30
 
29
31
  export type ColorType = string | Gradient | Pattern;
30
32
 
31
- export type AnyLayer = MorphLayer | ImageLayer | TextLayer | BezierLayer | QuadraticLayer | LineLayer | Group;
33
+ export type AnyLayer = MorphLayer | ImageLayer | TextLayer | BezierLayer | QuadraticLayer | LineLayer | ClearLayer | Group;
32
34
 
33
35
  export type AnyWeight = FontWeight | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | 950;
34
36
 
@@ -50,12 +52,14 @@ export type AnyCentring = Centring | 'start' | 'start-top' | 'start-bottom' | 'c
50
52
 
51
53
  export type AnyPatternType = PatternType | 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';
52
54
 
53
- export type AnySaveFormat = SaveFormat | 'png' | 'jpeg' | 'jpg' | 'svg';
55
+ export type AnySaveFormat = SaveFormat | 'png' | 'jpeg' | 'jpg' | 'svg' | 'gif' | 'webp';
54
56
 
55
57
  export type AnyLinkType = LinkType | 'width' | 'height' | 'x' | 'y';
56
58
 
57
59
  export type AnyGlobalCompositeOperation = GlobalCompositeOperation | 'source-over' | 'source-in' | 'source-out' | 'source-atop' | 'destination-over' | 'destination-in' | 'destination-out' | 'destination-atop' | 'lighter' | 'copy' | 'xor' | 'multiply' | 'screen' | 'overlay' | 'darken' | 'lighten' | 'color-dodge' | 'color-burn' | 'hard-light' | 'soft-light' | 'difference' | 'exclusion' | 'hue' | 'saturation' | 'color' | 'luminosity';
58
60
 
61
+ export type AnyColorSpace = ColorSpace | 'rgb565' | 'rgba4444' | 'rgba444';
62
+
59
63
  export type Point = {
60
64
  x: ScaleType;
61
65
  y: ScaleType;
@@ -1,5 +1,5 @@
1
- import { LayerType, SaveFormat } from "../types/enum";
2
- import { Transform, ScaleType, ColorType, PointNumber, AnyCentring, AnyTextAlign } from "../types";
1
+ import { LayerType } from "../types/enum";
2
+ import { Transform, ScaleType, ColorType, PointNumber, AnyCentring, AnyTextAlign, AnySaveFormat } from "../types";
3
3
  import { Gradient } from "../structures/helpers/Gradient";
4
4
  import { Canvas, SKRSContext2D } from "@napi-rs/canvas";
5
5
  import { Pattern } from "../structures/helpers/Pattern";
@@ -51,7 +51,7 @@ export declare function transform(ctx: SKRSContext2D, transform: Transform, laye
51
51
  fontSize: number;
52
52
  multiline: boolean;
53
53
  }): void;
54
- export declare function saveFile(buffer: any, extension: SaveFormat, name: string): Promise<void>;
54
+ export declare function saveFile(buffer: any, extension: AnySaveFormat, name: string): Promise<void>;
55
55
  export declare function generateRandomName(): string;
56
56
  export declare function isImageUrlValid(src: string): boolean;
57
57
  export declare function centring(centring: AnyCentring, type: LayerType, width: number, height: number, x: number, y: number): {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nmmty/lazycanvas",
3
- "version": "0.3.6",
3
+ "version": "0.4.0",
4
4
  "description": "A simple way to interact with @napi-rs/canvas in an advanced way!",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -9,6 +9,7 @@
9
9
  "centring": "tsc ./test/centring.ts && node ./test/centring.js",
10
10
  "logo": "tsc ./test/logo.ts && node ./test/logo.js",
11
11
  "text": "tsc ./test/text.ts && node ./test/text.js",
12
+ "animation": "tsc ./test/animation.ts && node ./test/animation.js",
12
13
  "doc": "tsx docgen.ts",
13
14
  "font": "tsc ./scripts/font-gen.ts && node ./scripts/font-gen.js",
14
15
  "build": "tsc"
@@ -32,6 +33,7 @@
32
33
  "homepage": "https://github.com/NMMTY/LazyCanvas#readme",
33
34
  "dependencies": {
34
35
  "@napi-rs/canvas": "^0.1.68",
36
+ "gifenc": "^1.0.3",
35
37
  "jimp": "0.22.12",
36
38
  "path": "^0.12.7",
37
39
  "svgson": "^5.3.1"
@@ -43,7 +45,10 @@
43
45
  "@types/node": "^22.10.2",
44
46
  "@types/react": "^19.0.2",
45
47
  "ava": "^6.2.0",
48
+ "eslint": "^9.23.0",
49
+ "eslint-config-neon": "^0.2.7",
46
50
  "globals": "^15.14.0",
51
+ "lodash.merge": "^4.6.2",
47
52
  "tslib": "^2.8.1",
48
53
  "tsx": "^4.19.2",
49
54
  "typescript": "^5.4.5"