main init
This commit is contained in:
72
Library/web/kit/kit-fx.js
Normal file
72
Library/web/kit/kit-fx.js
Normal file
@@ -0,0 +1,72 @@
|
||||
/* Kit fx (v1) - small juice helpers for canvas games. From _Tools\Library\web\kit.
|
||||
|
||||
const fx = Kit.fx();
|
||||
fx.burst(x, y, '#ffd83a', 16, 180) particles (count, speed px/s)
|
||||
fx.float(x, y, '+10', '#fff') rising, fading text
|
||||
fx.shake(8) screen shake strength in px (decays)
|
||||
fx.update(dt) in loop update
|
||||
fx.apply(ctx) ... draw world ... ctx.restore() (apply saves + offsets for shake)
|
||||
fx.draw(ctx) particles + texts on top
|
||||
Kit.tween(from, to, t, Kit.ease.outBack) */
|
||||
(function (root) {
|
||||
'use strict';
|
||||
const Kit = (root.Kit = root.Kit || {});
|
||||
|
||||
Kit.ease = {
|
||||
linear: t => t,
|
||||
outQuad: t => 1 - (1 - t) * (1 - t),
|
||||
inOutQuad: t => (t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2),
|
||||
outCubic: t => 1 - Math.pow(1 - t, 3),
|
||||
outBack: t => 1 + 2.70158 * Math.pow(t - 1, 3) + 1.70158 * Math.pow(t - 1, 2),
|
||||
outElastic: t => (t === 0 || t === 1 ? t : Math.pow(2, -10 * t) * Math.sin((t * 10 - 0.75) * (2 * Math.PI / 3)) + 1),
|
||||
};
|
||||
Kit.tween = (a, b, t, ease = Kit.ease.outQuad) => a + (b - a) * ease(Math.max(0, Math.min(1, t)));
|
||||
|
||||
Kit.fx = () => {
|
||||
const parts = [], texts = [];
|
||||
let shake = 0;
|
||||
return {
|
||||
burst(x, y, color = '#fff', n = 14, speed = 160, life = 0.6) {
|
||||
for (let i = 0; i < n; i++) {
|
||||
const a = Math.random() * Math.PI * 2, v = speed * (0.4 + Math.random() * 0.8);
|
||||
parts.push({ x, y, vx: Math.cos(a) * v, vy: Math.sin(a) * v, life, max: life, color, r: 2 + Math.random() * 2.5 });
|
||||
}
|
||||
},
|
||||
float(x, y, text, color = '#fff', size = 18) { texts.push({ x, y, text, color, size, life: 0.9, max: 0.9 }); },
|
||||
shake(px) { shake = Math.max(shake, px); },
|
||||
update(dt) {
|
||||
for (let i = parts.length - 1; i >= 0; i--) {
|
||||
const p = parts[i];
|
||||
p.life -= dt; if (p.life <= 0) { parts.splice(i, 1); continue; }
|
||||
p.x += p.vx * dt; p.y += p.vy * dt; p.vx *= 0.96; p.vy = p.vy * 0.96 + 220 * dt;
|
||||
}
|
||||
for (let i = texts.length - 1; i >= 0; i--) {
|
||||
const t = texts[i];
|
||||
t.life -= dt; if (t.life <= 0) { texts.splice(i, 1); continue; }
|
||||
t.y -= 40 * dt;
|
||||
}
|
||||
shake = Math.max(0, shake - 40 * dt);
|
||||
},
|
||||
apply(ctx) {
|
||||
ctx.save();
|
||||
if (shake > 0) ctx.translate((Math.random() * 2 - 1) * shake, (Math.random() * 2 - 1) * shake);
|
||||
},
|
||||
draw(ctx) {
|
||||
for (const p of parts) {
|
||||
ctx.globalAlpha = p.life / p.max;
|
||||
ctx.fillStyle = p.color;
|
||||
ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); ctx.fill();
|
||||
}
|
||||
ctx.textAlign = 'center'; ctx.textBaseline = 'middle';
|
||||
for (const t of texts) {
|
||||
ctx.globalAlpha = Math.min(1, t.life / t.max * 2);
|
||||
ctx.font = `bold ${t.size}px system-ui, sans-serif`;
|
||||
ctx.lineWidth = 4; ctx.strokeStyle = 'rgba(0,0,0,.6)'; ctx.strokeText(t.text, t.x, t.y);
|
||||
ctx.fillStyle = t.color; ctx.fillText(t.text, t.x, t.y);
|
||||
}
|
||||
ctx.globalAlpha = 1;
|
||||
},
|
||||
clear() { parts.length = 0; texts.length = 0; shake = 0; },
|
||||
};
|
||||
};
|
||||
})(typeof window !== 'undefined' ? window : globalThis);
|
||||
Reference in New Issue
Block a user