UNPKG

16.3 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Canvas = void 0;
4const Utils_1 = require("../Utils");
5class Canvas {
6 constructor(container) {
7 this.container = container;
8 this.size = {
9 height: 0,
10 width: 0,
11 };
12 this.context = null;
13 this.generatedCanvas = false;
14 }
15 init() {
16 var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
17 this.resize();
18 const options = this.container.actualOptions;
19 const element = this.element;
20 if (element) {
21 if (options.fullScreen.enable) {
22 this.originalStyle = Utils_1.Utils.deepExtend({}, element.style);
23 element.style.position = "fixed";
24 element.style.zIndex = options.fullScreen.zIndex.toString(10);
25 element.style.top = "0";
26 element.style.left = "0";
27 element.style.width = "100%";
28 element.style.height = "100%";
29 }
30 else {
31 element.style.position = (_b = (_a = this.originalStyle) === null || _a === void 0 ? void 0 : _a.position) !== null && _b !== void 0 ? _b : "";
32 element.style.zIndex = (_d = (_c = this.originalStyle) === null || _c === void 0 ? void 0 : _c.zIndex) !== null && _d !== void 0 ? _d : "";
33 element.style.top = (_f = (_e = this.originalStyle) === null || _e === void 0 ? void 0 : _e.top) !== null && _f !== void 0 ? _f : "";
34 element.style.left = (_h = (_g = this.originalStyle) === null || _g === void 0 ? void 0 : _g.left) !== null && _h !== void 0 ? _h : "";
35 element.style.width = (_k = (_j = this.originalStyle) === null || _j === void 0 ? void 0 : _j.width) !== null && _k !== void 0 ? _k : "";
36 element.style.height = (_m = (_l = this.originalStyle) === null || _l === void 0 ? void 0 : _l.height) !== null && _m !== void 0 ? _m : "";
37 }
38 }
39 const cover = options.backgroundMask.cover;
40 const color = cover.color;
41 const trail = options.particles.move.trail;
42 const coverRgb = Utils_1.ColorUtils.colorToRgb(color);
43 this.coverColor =
44 coverRgb !== undefined
45 ? {
46 r: coverRgb.r,
47 g: coverRgb.g,
48 b: coverRgb.b,
49 a: cover.opacity,
50 }
51 : undefined;
52 this.trailFillColor = Utils_1.ColorUtils.colorToRgb(trail.fillColor);
53 this.initBackground();
54 this.paint();
55 }
56 loadCanvas(canvas, generatedCanvas) {
57 var _a;
58 if (!canvas.className) {
59 canvas.className = Utils_1.Constants.canvasClass;
60 }
61 if (this.generatedCanvas) {
62 (_a = this.element) === null || _a === void 0 ? void 0 : _a.remove();
63 }
64 this.generatedCanvas = generatedCanvas !== null && generatedCanvas !== void 0 ? generatedCanvas : this.generatedCanvas;
65 this.element = canvas;
66 this.originalStyle = Utils_1.Utils.deepExtend({}, this.element.style);
67 this.size.height = canvas.offsetHeight;
68 this.size.width = canvas.offsetWidth;
69 this.context = this.element.getContext("2d");
70 this.container.retina.init();
71 this.initBackground();
72 }
73 destroy() {
74 var _a;
75 if (this.generatedCanvas) {
76 (_a = this.element) === null || _a === void 0 ? void 0 : _a.remove();
77 }
78 if (this.context) {
79 Utils_1.CanvasUtils.clear(this.context, this.size);
80 }
81 }
82 paint() {
83 const options = this.container.actualOptions;
84 if (!this.context) {
85 return;
86 }
87 if (options.backgroundMask.enable && options.backgroundMask.cover && this.coverColor) {
88 Utils_1.CanvasUtils.clear(this.context, this.size);
89 this.paintBase(Utils_1.ColorUtils.getStyleFromRgb(this.coverColor, this.coverColor.a));
90 }
91 else {
92 this.paintBase();
93 }
94 }
95 clear() {
96 const options = this.container.actualOptions;
97 const trail = options.particles.move.trail;
98 if (options.backgroundMask.enable) {
99 this.paint();
100 }
101 else if (trail.enable && trail.length > 0 && this.trailFillColor) {
102 this.paintBase(Utils_1.ColorUtils.getStyleFromRgb(this.trailFillColor, 1 / trail.length));
103 }
104 else if (this.context) {
105 Utils_1.CanvasUtils.clear(this.context, this.size);
106 }
107 }
108 windowResize() {
109 if (!this.element) {
110 return;
111 }
112 const container = this.container;
113 this.resize();
114 container.actualOptions.setResponsive(this.size.width, container.retina.pixelRatio, container.options);
115 container.particles.setDensity();
116 for (const [, plugin] of container.plugins) {
117 if (plugin.resize !== undefined) {
118 plugin.resize();
119 }
120 }
121 }
122 resize() {
123 if (!this.element) {
124 return;
125 }
126 const container = this.container;
127 const pxRatio = container.retina.pixelRatio;
128 const size = container.canvas.size;
129 const oldSize = {
130 width: size.width,
131 height: size.height,
132 };
133 size.width = this.element.offsetWidth * pxRatio;
134 size.height = this.element.offsetHeight * pxRatio;
135 this.element.width = size.width;
136 this.element.height = size.height;
137 if (this.container.started) {
138 this.resizeFactor = {
139 width: size.width / oldSize.width,
140 height: size.height / oldSize.height,
141 };
142 }
143 }
144 drawConnectLine(p1, p2) {
145 var _a;
146 const ctx = this.context;
147 if (!ctx) {
148 return;
149 }
150 const lineStyle = this.lineStyle(p1, p2);
151 if (!lineStyle) {
152 return;
153 }
154 const pos1 = p1.getPosition();
155 const pos2 = p2.getPosition();
156 Utils_1.CanvasUtils.drawConnectLine(ctx, (_a = p1.linksWidth) !== null && _a !== void 0 ? _a : this.container.retina.linksWidth, lineStyle, pos1, pos2);
157 }
158 drawGrabLine(particle, lineColor, opacity, mousePos) {
159 var _a;
160 const container = this.container;
161 const ctx = container.canvas.context;
162 if (!ctx) {
163 return;
164 }
165 const beginPos = particle.getPosition();
166 Utils_1.CanvasUtils.drawGrabLine(ctx, (_a = particle.linksWidth) !== null && _a !== void 0 ? _a : container.retina.linksWidth, beginPos, mousePos, lineColor, opacity);
167 }
168 drawParticleShadow(particle, mousePos) {
169 if (!this.context) {
170 return;
171 }
172 Utils_1.CanvasUtils.drawParticleShadow(this.container, this.context, particle, mousePos);
173 }
174 drawLinkTriangle(p1, link1, link2) {
175 var _a;
176 const container = this.container;
177 const options = container.actualOptions;
178 const p2 = link1.destination;
179 const p3 = link2.destination;
180 const triangleOptions = p1.options.links.triangles;
181 const opacityTriangle = (_a = triangleOptions.opacity) !== null && _a !== void 0 ? _a : (link1.opacity + link2.opacity) / 2;
182 if (opacityTriangle <= 0) {
183 return;
184 }
185 const pos1 = p1.getPosition();
186 const pos2 = p2.getPosition();
187 const pos3 = p3.getPosition();
188 const ctx = this.context;
189 if (!ctx) {
190 return;
191 }
192 if (Utils_1.NumberUtils.getDistance(pos1, pos2) > container.retina.linksDistance ||
193 Utils_1.NumberUtils.getDistance(pos3, pos2) > container.retina.linksDistance ||
194 Utils_1.NumberUtils.getDistance(pos3, pos1) > container.retina.linksDistance) {
195 return;
196 }
197 let colorTriangle = Utils_1.ColorUtils.colorToRgb(triangleOptions.color);
198 if (!colorTriangle) {
199 const linksOptions = p1.options.links;
200 const linkColor = linksOptions.id !== undefined
201 ? container.particles.linksColors.get(linksOptions.id)
202 : container.particles.linksColor;
203 colorTriangle = Utils_1.ColorUtils.getLinkColor(p1, p2, linkColor);
204 }
205 if (!colorTriangle) {
206 return;
207 }
208 Utils_1.CanvasUtils.drawLinkTriangle(ctx, pos1, pos2, pos3, options.backgroundMask.enable, options.backgroundMask.composite, colorTriangle, opacityTriangle);
209 }
210 drawLinkLine(p1, link) {
211 var _a, _b;
212 const container = this.container;
213 const options = container.actualOptions;
214 const p2 = link.destination;
215 let opacity = link.opacity;
216 const pos1 = p1.getPosition();
217 const pos2 = p2.getPosition();
218 const ctx = this.context;
219 if (!ctx) {
220 return;
221 }
222 let colorLine;
223 const twinkle = p1.options.twinkle.lines;
224 if (twinkle.enable) {
225 const twinkleFreq = twinkle.frequency;
226 const twinkleRgb = Utils_1.ColorUtils.colorToRgb(twinkle.color);
227 const twinkling = Math.random() < twinkleFreq;
228 if (twinkling && twinkleRgb !== undefined) {
229 colorLine = twinkleRgb;
230 opacity = twinkle.opacity;
231 }
232 }
233 if (!colorLine) {
234 const linksOptions = p1.options.links;
235 const linkColor = linksOptions.id !== undefined
236 ? container.particles.linksColors.get(linksOptions.id)
237 : container.particles.linksColor;
238 colorLine = Utils_1.ColorUtils.getLinkColor(p1, p2, linkColor);
239 }
240 if (!colorLine) {
241 return;
242 }
243 const width = (_a = p1.linksWidth) !== null && _a !== void 0 ? _a : container.retina.linksWidth;
244 const maxDistance = (_b = p1.linksDistance) !== null && _b !== void 0 ? _b : container.retina.linksDistance;
245 Utils_1.CanvasUtils.drawLinkLine(ctx, width, pos1, pos2, maxDistance, container.canvas.size, p1.options.links.warp, options.backgroundMask.enable, options.backgroundMask.composite, colorLine, opacity, p1.options.links.shadow);
246 }
247 drawParticle(particle, delta) {
248 var _a, _b, _c, _d;
249 if (((_a = particle.image) === null || _a === void 0 ? void 0 : _a.loaded) === false || particle.spawning || particle.destroyed) {
250 return;
251 }
252 const pfColor = particle.getFillColor();
253 const psColor = (_b = particle.getStrokeColor()) !== null && _b !== void 0 ? _b : pfColor;
254 if (!pfColor && !psColor) {
255 return;
256 }
257 const options = this.container.actualOptions;
258 const pOptions = particle.options;
259 const twinkle = pOptions.twinkle.particles;
260 const twinkleFreq = twinkle.frequency;
261 const twinkleRgb = Utils_1.ColorUtils.colorToRgb(twinkle.color);
262 const twinkling = twinkle.enable && Math.random() < twinkleFreq;
263 const radius = particle.getRadius();
264 const opacity = twinkling ? twinkle.opacity : (_c = particle.bubble.opacity) !== null && _c !== void 0 ? _c : particle.opacity.value;
265 const infectionStage = particle.infecter.infectionStage;
266 const infection = options.infection;
267 const infectionStages = infection.stages;
268 const infectionColor = infectionStage !== undefined ? infectionStages[infectionStage].color : undefined;
269 const infectionRgb = Utils_1.ColorUtils.colorToRgb(infectionColor);
270 const fColor = twinkling && twinkleRgb !== undefined
271 ? twinkleRgb
272 : infectionRgb !== null && infectionRgb !== void 0 ? infectionRgb : (pfColor ? Utils_1.ColorUtils.hslToRgb(pfColor) : undefined);
273 const sColor = twinkling && twinkleRgb !== undefined
274 ? twinkleRgb
275 : infectionRgb !== null && infectionRgb !== void 0 ? infectionRgb : (psColor ? Utils_1.ColorUtils.hslToRgb(psColor) : undefined);
276 const fillColorValue = fColor !== undefined ? Utils_1.ColorUtils.getStyleFromRgb(fColor, opacity) : undefined;
277 if (!this.context || (!fillColorValue && !sColor)) {
278 return;
279 }
280 const strokeColorValue = sColor !== undefined
281 ? Utils_1.ColorUtils.getStyleFromRgb(sColor, (_d = particle.stroke.opacity) !== null && _d !== void 0 ? _d : opacity)
282 : fillColorValue;
283 this.drawParticleLinks(particle);
284 if (radius > 0) {
285 Utils_1.CanvasUtils.drawParticle(this.container, this.context, particle, delta, fillColorValue, strokeColorValue, options.backgroundMask.enable, options.backgroundMask.composite, radius, opacity, particle.options.shadow);
286 }
287 }
288 drawParticleLinks(particle) {
289 if (!this.context) {
290 return;
291 }
292 const container = this.container;
293 const particles = container.particles;
294 const pOptions = particle.options;
295 if (particle.links.length > 0) {
296 this.context.save();
297 const p1Links = particle.links.filter((l) => {
298 const linkFreq = container.particles.getLinkFrequency(particle, l.destination);
299 return linkFreq <= pOptions.links.frequency;
300 });
301 for (const link of p1Links) {
302 const p2 = link.destination;
303 if (pOptions.links.triangles.enable) {
304 const links = p1Links.map((l) => l.destination);
305 const vertices = p2.links.filter((t) => {
306 const linkFreq = container.particles.getLinkFrequency(p2, t.destination);
307 return linkFreq <= p2.options.links.frequency && links.indexOf(t.destination) >= 0;
308 });
309 if (vertices.length) {
310 for (const vertex of vertices) {
311 const p3 = vertex.destination;
312 const triangleFreq = particles.getTriangleFrequency(particle, p2, p3);
313 if (triangleFreq > pOptions.links.triangles.frequency) {
314 continue;
315 }
316 this.drawLinkTriangle(particle, link, vertex);
317 }
318 }
319 }
320 if (link.opacity > 0 && container.retina.linksWidth > 0) {
321 this.drawLinkLine(particle, link);
322 }
323 }
324 this.context.restore();
325 }
326 }
327 drawPlugin(plugin, delta) {
328 if (!this.context) {
329 return;
330 }
331 Utils_1.CanvasUtils.drawPlugin(this.context, plugin, delta);
332 }
333 drawLight(mousePos) {
334 if (!this.context) {
335 return;
336 }
337 Utils_1.CanvasUtils.drawLight(this.container, this.context, mousePos);
338 }
339 paintBase(baseColor) {
340 if (!this.context) {
341 return;
342 }
343 Utils_1.CanvasUtils.paintBase(this.context, this.size, baseColor);
344 }
345 lineStyle(p1, p2) {
346 if (!this.context) {
347 return;
348 }
349 const options = this.container.actualOptions;
350 const connectOptions = options.interactivity.modes.connect;
351 return Utils_1.CanvasUtils.gradient(this.context, p1, p2, connectOptions.links.opacity);
352 }
353 initBackground() {
354 const options = this.container.actualOptions;
355 const background = options.background;
356 const element = this.element;
357 const elementStyle = element === null || element === void 0 ? void 0 : element.style;
358 if (!elementStyle) {
359 return;
360 }
361 if (background.color) {
362 const color = Utils_1.ColorUtils.colorToRgb(background.color);
363 elementStyle.backgroundColor = color ? Utils_1.ColorUtils.getStyleFromRgb(color, background.opacity) : "";
364 }
365 else {
366 elementStyle.backgroundColor = "";
367 }
368 elementStyle.backgroundImage = background.image || "";
369 elementStyle.backgroundPosition = background.position || "";
370 elementStyle.backgroundRepeat = background.repeat || "";
371 elementStyle.backgroundSize = background.size || "";
372 }
373}
374exports.Canvas = Canvas;