175 lines
9.0 KiB
JavaScript
175 lines
9.0 KiB
JavaScript
"use strict";
|
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
});
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.Repulser = void 0;
|
|
const tsparticles_engine_1 = require("tsparticles-engine");
|
|
const Repulse_1 = require("./Options/Classes/Repulse");
|
|
class Repulser extends tsparticles_engine_1.ExternalInteractorBase {
|
|
constructor(engine, container) {
|
|
super(container);
|
|
this._engine = engine;
|
|
if (!container.repulse) {
|
|
container.repulse = { particles: [] };
|
|
}
|
|
this.handleClickMode = (mode) => {
|
|
const options = this.container.actualOptions, repulse = options.interactivity.modes.repulse;
|
|
if (!repulse || mode !== "repulse") {
|
|
return;
|
|
}
|
|
if (!container.repulse) {
|
|
container.repulse = { particles: [] };
|
|
}
|
|
container.repulse.clicking = true;
|
|
container.repulse.count = 0;
|
|
for (const particle of container.repulse.particles) {
|
|
if (!this.isEnabled(particle)) {
|
|
continue;
|
|
}
|
|
particle.velocity.setTo(particle.initialVelocity);
|
|
}
|
|
container.repulse.particles = [];
|
|
container.repulse.finish = false;
|
|
setTimeout(() => {
|
|
if (!container.destroyed) {
|
|
if (!container.repulse) {
|
|
container.repulse = { particles: [] };
|
|
}
|
|
container.repulse.clicking = false;
|
|
}
|
|
}, repulse.duration * 1000);
|
|
};
|
|
}
|
|
clear() {
|
|
}
|
|
init() {
|
|
const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
|
|
if (!repulse) {
|
|
return;
|
|
}
|
|
container.retina.repulseModeDistance = repulse.distance * container.retina.pixelRatio;
|
|
}
|
|
interact() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const container = this.container, options = container.actualOptions, mouseMoveStatus = container.interactivity.status === tsparticles_engine_1.mouseMoveEvent, events = options.interactivity.events, hoverEnabled = events.onHover.enable, hoverMode = events.onHover.mode, clickEnabled = events.onClick.enable, clickMode = events.onClick.mode, divs = events.onDiv;
|
|
if (mouseMoveStatus && hoverEnabled && (0, tsparticles_engine_1.isInArray)("repulse", hoverMode)) {
|
|
this.hoverRepulse();
|
|
}
|
|
else if (clickEnabled && (0, tsparticles_engine_1.isInArray)("repulse", clickMode)) {
|
|
this.clickRepulse();
|
|
}
|
|
else {
|
|
(0, tsparticles_engine_1.divModeExecute)("repulse", divs, (selector, div) => this.singleSelectorRepulse(selector, div));
|
|
}
|
|
});
|
|
}
|
|
isEnabled(particle) {
|
|
var _a;
|
|
const container = this.container, options = container.actualOptions, mouse = container.interactivity.mouse, events = ((_a = particle === null || particle === void 0 ? void 0 : particle.interactivity) !== null && _a !== void 0 ? _a : options.interactivity).events, divs = events.onDiv, divRepulse = (0, tsparticles_engine_1.isDivModeEnabled)("repulse", divs);
|
|
if (!(divRepulse || (events.onHover.enable && mouse.position) || (events.onClick.enable && mouse.clickPosition))) {
|
|
return false;
|
|
}
|
|
const hoverMode = events.onHover.mode, clickMode = events.onClick.mode;
|
|
return (0, tsparticles_engine_1.isInArray)("repulse", hoverMode) || (0, tsparticles_engine_1.isInArray)("repulse", clickMode) || divRepulse;
|
|
}
|
|
loadModeOptions(options, ...sources) {
|
|
if (!options.repulse) {
|
|
options.repulse = new Repulse_1.Repulse();
|
|
}
|
|
for (const source of sources) {
|
|
options.repulse.load(source === null || source === void 0 ? void 0 : source.repulse);
|
|
}
|
|
}
|
|
reset() {
|
|
}
|
|
clickRepulse() {
|
|
const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
|
|
if (!repulse) {
|
|
return;
|
|
}
|
|
if (!container.repulse) {
|
|
container.repulse = { particles: [] };
|
|
}
|
|
if (!container.repulse.finish) {
|
|
if (!container.repulse.count) {
|
|
container.repulse.count = 0;
|
|
}
|
|
container.repulse.count++;
|
|
if (container.repulse.count === container.particles.count) {
|
|
container.repulse.finish = true;
|
|
}
|
|
}
|
|
if (container.repulse.clicking) {
|
|
const repulseDistance = container.retina.repulseModeDistance;
|
|
if (!repulseDistance || repulseDistance < 0) {
|
|
return;
|
|
}
|
|
const repulseRadius = Math.pow(repulseDistance / 6, 3), mouseClickPos = container.interactivity.mouse.clickPosition;
|
|
if (mouseClickPos === undefined) {
|
|
return;
|
|
}
|
|
const range = new tsparticles_engine_1.Circle(mouseClickPos.x, mouseClickPos.y, repulseRadius), query = container.particles.quadTree.query(range, (p) => this.isEnabled(p));
|
|
for (const particle of query) {
|
|
const { dx, dy, distance } = (0, tsparticles_engine_1.getDistances)(mouseClickPos, particle.position), d = Math.pow(distance, 2), velocity = repulse.speed, force = (-repulseRadius * velocity) / d;
|
|
if (d <= repulseRadius) {
|
|
container.repulse.particles.push(particle);
|
|
const vect = tsparticles_engine_1.Vector.create(dx, dy);
|
|
vect.length = force;
|
|
particle.velocity.setTo(vect);
|
|
}
|
|
}
|
|
}
|
|
else if (container.repulse.clicking === false) {
|
|
for (const particle of container.repulse.particles) {
|
|
particle.velocity.setTo(particle.initialVelocity);
|
|
}
|
|
container.repulse.particles = [];
|
|
}
|
|
}
|
|
hoverRepulse() {
|
|
const container = this.container, mousePos = container.interactivity.mouse.position, repulseRadius = container.retina.repulseModeDistance;
|
|
if (!repulseRadius || repulseRadius < 0 || !mousePos) {
|
|
return;
|
|
}
|
|
this.processRepulse(mousePos, repulseRadius, new tsparticles_engine_1.Circle(mousePos.x, mousePos.y, repulseRadius));
|
|
}
|
|
processRepulse(position, repulseRadius, area, divRepulse) {
|
|
var _a;
|
|
const container = this.container, query = container.particles.quadTree.query(area, (p) => this.isEnabled(p)), repulseOptions = container.actualOptions.interactivity.modes.repulse;
|
|
if (!repulseOptions) {
|
|
return;
|
|
}
|
|
for (const particle of query) {
|
|
const { dx, dy, distance } = (0, tsparticles_engine_1.getDistances)(particle.position, position), velocity = ((_a = divRepulse === null || divRepulse === void 0 ? void 0 : divRepulse.speed) !== null && _a !== void 0 ? _a : repulseOptions.speed) * repulseOptions.factor, repulseFactor = (0, tsparticles_engine_1.clamp)((0, tsparticles_engine_1.getEasing)(repulseOptions.easing)(1 - distance / repulseRadius) * velocity, 0, repulseOptions.maxSpeed), normVec = tsparticles_engine_1.Vector.create(distance === 0 ? velocity : (dx / distance) * repulseFactor, distance === 0 ? velocity : (dy / distance) * repulseFactor);
|
|
particle.position.addTo(normVec);
|
|
}
|
|
}
|
|
singleSelectorRepulse(selector, div) {
|
|
const container = this.container, repulse = container.actualOptions.interactivity.modes.repulse;
|
|
if (!repulse) {
|
|
return;
|
|
}
|
|
const query = document.querySelectorAll(selector);
|
|
if (!query.length) {
|
|
return;
|
|
}
|
|
query.forEach((item) => {
|
|
const elem = item, pxRatio = container.retina.pixelRatio, pos = {
|
|
x: (elem.offsetLeft + elem.offsetWidth / 2) * pxRatio,
|
|
y: (elem.offsetTop + elem.offsetHeight / 2) * pxRatio,
|
|
}, repulseRadius = (elem.offsetWidth / 2) * pxRatio, area = div.type === "circle"
|
|
? new tsparticles_engine_1.Circle(pos.x, pos.y, repulseRadius)
|
|
: new tsparticles_engine_1.Rectangle(elem.offsetLeft * pxRatio, elem.offsetTop * pxRatio, elem.offsetWidth * pxRatio, elem.offsetHeight * pxRatio), divs = repulse.divs, divRepulse = (0, tsparticles_engine_1.divMode)(divs, elem);
|
|
this.processRepulse(pos, repulseRadius, area, divRepulse);
|
|
});
|
|
}
|
|
}
|
|
exports.Repulser = Repulser;
|