96 lines
5.1 KiB
JavaScript
96 lines
5.1 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.Bouncer = void 0;
|
|
const tsparticles_engine_1 = require("tsparticles-engine");
|
|
const Bounce_1 = require("./Options/Classes/Bounce");
|
|
class Bouncer extends tsparticles_engine_1.ExternalInteractorBase {
|
|
constructor(container) {
|
|
super(container);
|
|
}
|
|
clear() {
|
|
}
|
|
init() {
|
|
const container = this.container, bounce = container.actualOptions.interactivity.modes.bounce;
|
|
if (!bounce) {
|
|
return;
|
|
}
|
|
container.retina.bounceModeDistance = bounce.distance * container.retina.pixelRatio;
|
|
}
|
|
interact() {
|
|
return __awaiter(this, void 0, void 0, function* () {
|
|
const container = this.container, options = container.actualOptions, events = options.interactivity.events, mouseMoveStatus = container.interactivity.status === tsparticles_engine_1.mouseMoveEvent, hoverEnabled = events.onHover.enable, hoverMode = events.onHover.mode, divs = events.onDiv;
|
|
if (mouseMoveStatus && hoverEnabled && (0, tsparticles_engine_1.isInArray)("bounce", hoverMode)) {
|
|
this.processMouseBounce();
|
|
}
|
|
else {
|
|
(0, tsparticles_engine_1.divModeExecute)("bounce", divs, (selector, div) => this.singleSelectorBounce(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;
|
|
return ((mouse.position && events.onHover.enable && (0, tsparticles_engine_1.isInArray)("bounce", events.onHover.mode)) ||
|
|
(0, tsparticles_engine_1.isDivModeEnabled)("bounce", divs));
|
|
}
|
|
loadModeOptions(options, ...sources) {
|
|
if (!options.bounce) {
|
|
options.bounce = new Bounce_1.Bounce();
|
|
}
|
|
for (const source of sources) {
|
|
options.bounce.load(source === null || source === void 0 ? void 0 : source.bounce);
|
|
}
|
|
}
|
|
reset() {
|
|
}
|
|
processBounce(position, radius, area) {
|
|
const query = this.container.particles.quadTree.query(area, (p) => this.isEnabled(p));
|
|
for (const particle of query) {
|
|
if (area instanceof tsparticles_engine_1.Circle) {
|
|
(0, tsparticles_engine_1.circleBounce)((0, tsparticles_engine_1.circleBounceDataFromParticle)(particle), {
|
|
position,
|
|
radius,
|
|
mass: (Math.pow(radius, 2) * Math.PI) / 2,
|
|
velocity: tsparticles_engine_1.Vector.origin,
|
|
factor: tsparticles_engine_1.Vector.origin,
|
|
});
|
|
}
|
|
else if (area instanceof tsparticles_engine_1.Rectangle) {
|
|
(0, tsparticles_engine_1.rectBounce)(particle, (0, tsparticles_engine_1.calculateBounds)(position, radius));
|
|
}
|
|
}
|
|
}
|
|
processMouseBounce() {
|
|
const container = this.container, pxRatio = container.retina.pixelRatio, tolerance = 10 * pxRatio, mousePos = container.interactivity.mouse.position, radius = container.retina.bounceModeDistance;
|
|
if (!radius || radius < 0 || !mousePos) {
|
|
return;
|
|
}
|
|
this.processBounce(mousePos, radius, new tsparticles_engine_1.Circle(mousePos.x, mousePos.y, radius + tolerance));
|
|
}
|
|
singleSelectorBounce(selector, div) {
|
|
const container = this.container, 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,
|
|
}, radius = (elem.offsetWidth / 2) * pxRatio, tolerance = 10 * pxRatio, area = div.type === "circle"
|
|
? new tsparticles_engine_1.Circle(pos.x, pos.y, radius + tolerance)
|
|
: new tsparticles_engine_1.Rectangle(elem.offsetLeft * pxRatio - tolerance, elem.offsetTop * pxRatio - tolerance, elem.offsetWidth * pxRatio + tolerance * 2, elem.offsetHeight * pxRatio + tolerance * 2);
|
|
this.processBounce(pos, radius, area);
|
|
});
|
|
}
|
|
}
|
|
exports.Bouncer = Bouncer;
|