kinetic-slider
Version:
A WebGL-powered kinetic slider component using PIXI.js
111 lines (107 loc) • 3.74 kB
JavaScript
;
var pixiFilters = require('pixi-filters');
var ShaderResourceManager = require('../managers/ShaderResourceManager.cjs');
function createGodrayFilter(config) {
const shaderManager = ShaderResourceManager.ShaderResourceManager.getInstance();
const options = {};
if (config.alpha !== void 0) options.alpha = config.alpha;
if (config.angle !== void 0) options.angle = config.angle;
if (config.center !== void 0) options.center = config.center;
if (config.centerX !== void 0) options.centerX = config.centerX;
if (config.centerY !== void 0) options.centerY = config.centerY;
if (config.gain !== void 0) options.gain = config.gain;
if (config.lacunarity !== void 0) options.lacunarity = config.lacunarity;
if (config.parallel !== void 0) options.parallel = config.parallel;
if (config.time !== void 0) options.time = config.time;
const angleStr = (options.angle || 30).toString();
const parallelStr = options.parallel ? "parallel" : "radial";
const shaderKey = `godray-filter-${angleStr}-${parallelStr}`;
const filter = new pixiFilters.GodrayFilter(options);
try {
shaderManager.registerFilter(filter, shaderKey);
} catch (error) {
console.warn("Error registering godray filter with shader manager:", error);
}
let animationActive = false;
let animationFrameId = null;
let lastTime = 0;
const updateIntensity = (intensity) => {
const normalizedIntensity = Math.max(0, Math.min(10, intensity));
if (config.primaryProperty) {
switch (config.primaryProperty) {
case "gain":
filter.gain = normalizedIntensity / 10;
break;
case "alpha":
filter.alpha = normalizedIntensity / 10;
break;
case "lacunarity":
filter.lacunarity = 0.5 + normalizedIntensity / 2;
break;
case "angle":
filter.angle = normalizedIntensity * 36;
break;
default:
filter.gain = normalizedIntensity / 10;
}
} else {
filter.gain = normalizedIntensity / 10;
}
if (config.animate) {
if (normalizedIntensity > 0 && !animationActive) {
startAnimation();
} else if (normalizedIntensity === 0 && animationActive) {
stopAnimation();
}
}
};
const startAnimation = () => {
if (animationActive) return;
animationActive = true;
lastTime = Date.now();
const animate = () => {
const now = Date.now();
const delta = (now - lastTime) / 1e3;
lastTime = now;
const animationSpeed = config.animationSpeed || 0.01;
filter.time += delta * animationSpeed;
animationFrameId = requestAnimationFrame(animate);
};
animate();
};
const stopAnimation = () => {
if (!animationActive) return;
animationActive = false;
if (animationFrameId !== null) {
cancelAnimationFrame(animationFrameId);
animationFrameId = null;
}
};
updateIntensity(config.intensity);
if (config.animate && config.intensity > 0) {
startAnimation();
}
const reset = () => {
if ("enabled" in filter) {
filter.enabled = config.enabled !== void 0 ? config.enabled : true;
}
if ("alpha" in filter) {
filter.alpha = config.alpha !== void 0 ? config.alpha : 1;
}
if (config.intensity !== void 0) {
updateIntensity(config.intensity);
}
};
const dispose = () => {
stopAnimation();
try {
shaderManager.releaseFilter(filter, shaderKey);
} catch (error) {
console.warn("Error releasing godray filter shader:", error);
}
filter.destroy();
};
return { filter, updateIntensity, reset, dispose };
}
exports.createGodrayFilter = createGodrayFilter;
//# sourceMappingURL=godrayFilter.cjs.map