1 | import { NumberUtils, Utils } from "../Utils";
|
2 | export class Retina {
|
3 | constructor(container) {
|
4 | this.container = container;
|
5 | }
|
6 | init() {
|
7 | const container = this.container;
|
8 | const options = container.actualOptions;
|
9 | this.pixelRatio = !options.detectRetina || Utils.isSsr() ? 1 : window.devicePixelRatio;
|
10 | const motionOptions = this.container.actualOptions.motion;
|
11 | if (motionOptions && (motionOptions.disable || motionOptions.reduce.value)) {
|
12 | if (Utils.isSsr() || typeof matchMedia === "undefined" || !matchMedia) {
|
13 | this.reduceFactor = 1;
|
14 | }
|
15 | else {
|
16 | const mediaQuery = matchMedia("(prefers-reduced-motion: reduce)");
|
17 | if (mediaQuery) {
|
18 | this.handleMotionChange(mediaQuery);
|
19 | const handleChange = () => {
|
20 | this.handleMotionChange(mediaQuery);
|
21 | container.refresh().catch(() => {
|
22 | });
|
23 | };
|
24 | if (mediaQuery.addEventListener !== undefined) {
|
25 | mediaQuery.addEventListener("change", handleChange);
|
26 | }
|
27 | else if (mediaQuery.addListener !== undefined) {
|
28 | mediaQuery.addListener(handleChange);
|
29 | }
|
30 | }
|
31 | }
|
32 | }
|
33 | else {
|
34 | this.reduceFactor = 1;
|
35 | }
|
36 | const ratio = this.pixelRatio;
|
37 | if (container.canvas.element) {
|
38 | const element = container.canvas.element;
|
39 | container.canvas.size.width = element.offsetWidth * ratio;
|
40 | container.canvas.size.height = element.offsetHeight * ratio;
|
41 | }
|
42 | const particles = options.particles;
|
43 | this.linksDistance = particles.links.distance * ratio;
|
44 | this.linksWidth = particles.links.width * ratio;
|
45 | this.sizeAnimationSpeed = particles.size.animation.speed * ratio;
|
46 | const modes = options.interactivity.modes;
|
47 | this.connectModeDistance = modes.connect.distance * ratio;
|
48 | this.connectModeRadius = modes.connect.radius * ratio;
|
49 | this.grabModeDistance = modes.grab.distance * ratio;
|
50 | this.repulseModeDistance = modes.repulse.distance * ratio;
|
51 | this.bounceModeDistance = modes.bounce.distance * ratio;
|
52 | this.attractModeDistance = modes.attract.distance * ratio;
|
53 | this.slowModeRadius = modes.slow.radius * ratio;
|
54 | this.bubbleModeDistance = modes.bubble.distance * ratio;
|
55 | if (modes.bubble.size) {
|
56 | this.bubbleModeSize = modes.bubble.size * ratio;
|
57 | }
|
58 | }
|
59 | initParticle(particle) {
|
60 | const particlesOptions = particle.options;
|
61 | const ratio = this.pixelRatio;
|
62 | particle.linksDistance = particlesOptions.links.distance * ratio;
|
63 | particle.linksWidth = particlesOptions.links.width * ratio;
|
64 | particle.moveDrift = NumberUtils.getRangeValue(particlesOptions.move.drift) * ratio;
|
65 | particle.moveSpeed = NumberUtils.getRangeValue(particlesOptions.move.speed) * ratio;
|
66 | particle.sizeAnimationSpeed = particlesOptions.size.animation.speed * ratio;
|
67 | particle.maxDistance = particlesOptions.move.distance * ratio;
|
68 | }
|
69 | handleMotionChange(mediaQuery) {
|
70 | const options = this.container.actualOptions;
|
71 | if (mediaQuery.matches) {
|
72 | const motion = options.motion;
|
73 | this.reduceFactor = motion.disable ? 0 : motion.reduce.value ? 1 / motion.reduce.factor : 1;
|
74 | }
|
75 | else {
|
76 | this.reduceFactor = 1;
|
77 | }
|
78 | }
|
79 | }
|