158 lines
3.9 KiB
TypeScript
158 lines
3.9 KiB
TypeScript
import confetti from 'canvas-confetti';
|
|
|
|
// Web Audio API Synthesizer
|
|
let audioCtx: AudioContext | null = null;
|
|
|
|
function getAudioContext(): AudioContext | null {
|
|
if (typeof window === 'undefined') return null;
|
|
if (!audioCtx) {
|
|
const AudioContextClass = window.AudioContext || (window as any).webkitAudioContext;
|
|
if (AudioContextClass) {
|
|
audioCtx = new AudioContextClass();
|
|
}
|
|
}
|
|
if (audioCtx && audioCtx.state === 'suspended') {
|
|
audioCtx.resume();
|
|
}
|
|
return audioCtx;
|
|
}
|
|
|
|
export function playGoldenKissSound() {
|
|
try {
|
|
const ctx = getAudioContext();
|
|
if (!ctx) return;
|
|
|
|
const notes = [523.25, 659.25, 783.99, 1046.5]; // C5, E5, G5, C6
|
|
notes.forEach((freq, i) => {
|
|
const osc = ctx.createOscillator();
|
|
const gain = ctx.createGain();
|
|
osc.type = 'triangle';
|
|
osc.frequency.setValueAtTime(freq, ctx.currentTime + i * 0.08);
|
|
|
|
gain.gain.setValueAtTime(0, ctx.currentTime + i * 0.08);
|
|
gain.gain.linearRampToValueAtTime(0.3, ctx.currentTime + i * 0.08 + 0.02);
|
|
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + i * 0.08 + 0.5);
|
|
|
|
osc.connect(gain);
|
|
gain.connect(ctx.destination);
|
|
|
|
osc.start(ctx.currentTime + i * 0.08);
|
|
osc.stop(ctx.currentTime + i * 0.08 + 0.6);
|
|
});
|
|
} catch (e) {
|
|
console.debug('Audio not allowed yet:', e);
|
|
}
|
|
}
|
|
|
|
export function playToastSound() {
|
|
try {
|
|
const ctx = getAudioContext();
|
|
if (!ctx) return;
|
|
|
|
// Glass clink high chime
|
|
const osc = ctx.createOscillator();
|
|
const gain = ctx.createGain();
|
|
osc.type = 'sine';
|
|
osc.frequency.setValueAtTime(2400, ctx.currentTime);
|
|
osc.frequency.exponentialRampToValueAtTime(1800, ctx.currentTime + 0.4);
|
|
|
|
gain.gain.setValueAtTime(0.25, ctx.currentTime);
|
|
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.4);
|
|
|
|
osc.connect(gain);
|
|
gain.connect(ctx.destination);
|
|
|
|
osc.start();
|
|
osc.stop(ctx.currentTime + 0.4);
|
|
} catch (e) {
|
|
console.debug('Audio error:', e);
|
|
}
|
|
}
|
|
|
|
export function playCatchClickSound() {
|
|
try {
|
|
const ctx = getAudioContext();
|
|
if (!ctx) return;
|
|
|
|
const osc = ctx.createOscillator();
|
|
const gain = ctx.createGain();
|
|
osc.type = 'sine';
|
|
osc.frequency.setValueAtTime(880, ctx.currentTime);
|
|
gain.gain.setValueAtTime(0.2, ctx.currentTime);
|
|
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.15);
|
|
|
|
osc.connect(gain);
|
|
gain.connect(ctx.destination);
|
|
|
|
osc.start();
|
|
osc.stop(ctx.currentTime + 0.15);
|
|
} catch (e) {
|
|
console.debug('Audio error:', e);
|
|
}
|
|
}
|
|
|
|
// Web Vibration API
|
|
export function vibrateKiss() {
|
|
if (typeof navigator !== 'undefined' && 'vibrate' in navigator) {
|
|
try {
|
|
navigator.vibrate([100, 50, 150]);
|
|
} catch {
|
|
// Ignore vibration errors
|
|
}
|
|
}
|
|
}
|
|
|
|
export function vibrateGolden() {
|
|
if (typeof navigator !== 'undefined' && 'vibrate' in navigator) {
|
|
try {
|
|
navigator.vibrate([200, 100, 200, 100, 400]);
|
|
} catch {
|
|
// Ignore
|
|
}
|
|
}
|
|
}
|
|
|
|
export function vibrateToast() {
|
|
if (typeof navigator !== 'undefined' && 'vibrate' in navigator) {
|
|
try {
|
|
navigator.vibrate([80, 40, 80]);
|
|
} catch {
|
|
// Ignore
|
|
}
|
|
}
|
|
}
|
|
|
|
// Confetti Blast
|
|
export function triggerCelebrationConfetti() {
|
|
try {
|
|
confetti({
|
|
particleCount: 80,
|
|
spread: 70,
|
|
origin: { y: 0.6 },
|
|
colors: ['#f59e0b', '#ec4899', '#8b5cf6', '#10b981', '#ffffff'],
|
|
});
|
|
} catch (e) {
|
|
console.debug('Confetti error:', e);
|
|
}
|
|
}
|
|
|
|
export function triggerGoldenKissConfetti() {
|
|
try {
|
|
const end = Date.now() + 2000;
|
|
const interval: any = setInterval(() => {
|
|
if (Date.now() > end) {
|
|
return clearInterval(interval);
|
|
}
|
|
confetti({
|
|
startVelocity: 30,
|
|
spread: 360,
|
|
ticks: 60,
|
|
origin: { x: Math.random(), y: Math.random() * 0.4 },
|
|
colors: ['#fbbf24', '#f59e0b', '#d97706', '#f43f5e', '#ffffff'],
|
|
});
|
|
}, 250);
|
|
} catch (e) {
|
|
console.debug('Confetti error:', e);
|
|
}
|
|
}
|