Debian Codegraph @

example/index.tsx

raw
1 import confetti from "canvas-confetti"
2 import { tinykeys } from "../src/tinykeys"
3
4 tinykeys(window, {
5 "Shift+D": () => {
6 alert("The 'Shift' and 'd' keys were pressed at the same time")
7 },
8 "y e e t": () => {
9 alert("The keys 'y', 'e', 'e', and 't' were pressed in order")
10 },
11 "$mod+([1-9])": event => {
12 event.preventDefault()
13 alert(`Either 'Control+${event.key}' or 'Meta+${event.key}' were pressed`)
14 },
15 })
16
17 const KonamiCode = [
18 "ArrowUp",
19 "ArrowUp",
20 "ArrowDown",
21 "ArrowDown",
22 "ArrowLeft",
23 "ArrowRight",
24 "ArrowLeft",
25 "ArrowRight",
26 "KeyB",
27 "KeyA",
28 "Enter",
29 ].join(" ")
30
31 tinykeys(window, {
32 [KonamiCode]: () => {
33 const duration = 5 * 1000
34 const animationEnd = Date.now() + duration
35 const defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 0 }
36
37 function randomInRange(min: number, max: number) {
38 return Math.random() * (max - min) + min
39 }
40
41 const interval = setInterval(() => {
42 const timeLeft = animationEnd - Date.now()
43
44 if (timeLeft <= 0) {
45 return clearInterval(interval)
46 }
47
48 const particleCount = 50 * (timeLeft / duration)
49 // since particles fall down, start a bit higher than random
50 void confetti({
51 ...defaults,
52 particleCount,
53 origin: { x: randomInRange(0.1, 0.3), y: Math.random() - 0.2 },
54 })
55 void confetti({
56 ...defaults,
57 particleCount,
58 origin: { x: randomInRange(0.7, 0.9), y: Math.random() - 0.2 },
59 })
60 }, 250)
61 },
62 })