UNPKG

5.78 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.alterHsl = exports.drawParticlePlugin = exports.drawPlugin = exports.drawShapeAfterDraw = exports.drawShape = exports.drawEffect = exports.drawParticle = exports.clear = exports.paintImage = exports.paintBase = exports.drawLine = void 0;
4const AlterType_js_1 = require("../Enums/Types/AlterType.js");
5const ColorUtils_js_1 = require("./ColorUtils.js");
6const origin = { x: 0, y: 0 }, defaultTransform = {
7 a: 1,
8 b: 0,
9 c: 0,
10 d: 1,
11};
12function drawLine(context, begin, end) {
13 context.beginPath();
14 context.moveTo(begin.x, begin.y);
15 context.lineTo(end.x, end.y);
16 context.closePath();
17}
18exports.drawLine = drawLine;
19function paintBase(context, dimension, baseColor) {
20 context.fillStyle = baseColor ?? "rgba(0,0,0,0)";
21 context.fillRect(origin.x, origin.y, dimension.width, dimension.height);
22}
23exports.paintBase = paintBase;
24function paintImage(context, dimension, image, opacity) {
25 if (!image) {
26 return;
27 }
28 context.globalAlpha = opacity;
29 context.drawImage(image, origin.x, origin.y, dimension.width, dimension.height);
30 context.globalAlpha = 1;
31}
32exports.paintImage = paintImage;
33function clear(context, dimension) {
34 context.clearRect(origin.x, origin.y, dimension.width, dimension.height);
35}
36exports.clear = clear;
37function drawParticle(data) {
38 const { container, context, particle, delta, colorStyles, backgroundMask, composite, radius, opacity, shadow, transform, } = data, pos = particle.getPosition(), defaultAngle = 0, angle = particle.rotation + (particle.pathRotation ? particle.velocity.angle : defaultAngle), rotateData = {
39 sin: Math.sin(angle),
40 cos: Math.cos(angle),
41 }, rotating = !!angle, identity = 1, transformData = {
42 a: rotateData.cos * (transform.a ?? defaultTransform.a),
43 b: rotating ? rotateData.sin * (transform.b ?? identity) : transform.b ?? defaultTransform.b,
44 c: rotating ? -rotateData.sin * (transform.c ?? identity) : transform.c ?? defaultTransform.c,
45 d: rotateData.cos * (transform.d ?? defaultTransform.d),
46 };
47 context.setTransform(transformData.a, transformData.b, transformData.c, transformData.d, pos.x, pos.y);
48 if (backgroundMask) {
49 context.globalCompositeOperation = composite;
50 }
51 const shadowColor = particle.shadowColor;
52 if (shadow.enable && shadowColor) {
53 context.shadowBlur = shadow.blur;
54 context.shadowColor = (0, ColorUtils_js_1.getStyleFromRgb)(shadowColor);
55 context.shadowOffsetX = shadow.offset.x;
56 context.shadowOffsetY = shadow.offset.y;
57 }
58 if (colorStyles.fill) {
59 context.fillStyle = colorStyles.fill;
60 }
61 const minStrokeWidth = 0, strokeWidth = particle.strokeWidth ?? minStrokeWidth;
62 context.lineWidth = strokeWidth;
63 if (colorStyles.stroke) {
64 context.strokeStyle = colorStyles.stroke;
65 }
66 const drawData = {
67 container,
68 context,
69 particle,
70 radius,
71 opacity,
72 delta,
73 transformData,
74 strokeWidth,
75 };
76 drawShape(drawData);
77 drawShapeAfterDraw(drawData);
78 drawEffect(drawData);
79 context.globalCompositeOperation = "source-over";
80 context.resetTransform();
81}
82exports.drawParticle = drawParticle;
83function drawEffect(data) {
84 const { container, context, particle, radius, opacity, delta, transformData } = data;
85 if (!particle.effect) {
86 return;
87 }
88 const drawer = container.effectDrawers.get(particle.effect);
89 if (!drawer) {
90 return;
91 }
92 drawer.draw({
93 context,
94 particle,
95 radius,
96 opacity,
97 delta,
98 pixelRatio: container.retina.pixelRatio,
99 transformData: { ...transformData },
100 });
101}
102exports.drawEffect = drawEffect;
103function drawShape(data) {
104 const { container, context, particle, radius, opacity, delta, strokeWidth, transformData } = data, minStrokeWidth = 0;
105 if (!particle.shape) {
106 return;
107 }
108 const drawer = container.shapeDrawers.get(particle.shape);
109 if (!drawer) {
110 return;
111 }
112 context.beginPath();
113 drawer.draw({
114 context,
115 particle,
116 radius,
117 opacity,
118 delta,
119 pixelRatio: container.retina.pixelRatio,
120 transformData: { ...transformData },
121 });
122 if (particle.shapeClose) {
123 context.closePath();
124 }
125 if (strokeWidth > minStrokeWidth) {
126 context.stroke();
127 }
128 if (particle.shapeFill) {
129 context.fill();
130 }
131}
132exports.drawShape = drawShape;
133function drawShapeAfterDraw(data) {
134 const { container, context, particle, radius, opacity, delta, transformData } = data;
135 if (!particle.shape) {
136 return;
137 }
138 const drawer = container.shapeDrawers.get(particle.shape);
139 if (!drawer?.afterDraw) {
140 return;
141 }
142 drawer.afterDraw({
143 context,
144 particle,
145 radius,
146 opacity,
147 delta,
148 pixelRatio: container.retina.pixelRatio,
149 transformData: { ...transformData },
150 });
151}
152exports.drawShapeAfterDraw = drawShapeAfterDraw;
153function drawPlugin(context, plugin, delta) {
154 if (!plugin.draw) {
155 return;
156 }
157 plugin.draw(context, delta);
158}
159exports.drawPlugin = drawPlugin;
160function drawParticlePlugin(context, plugin, particle, delta) {
161 if (!plugin.drawParticle) {
162 return;
163 }
164 plugin.drawParticle(context, particle, delta);
165}
166exports.drawParticlePlugin = drawParticlePlugin;
167function alterHsl(color, type, value) {
168 const lFactor = 1;
169 return {
170 h: color.h,
171 s: color.s,
172 l: color.l + (type === AlterType_js_1.AlterType.darken ? -lFactor : lFactor) * value,
173 };
174}
175exports.alterHsl = alterHsl;