Open History to see the last commit
1/**
2 * Hand-drawn rendering utilities for the game canvas.
3 * These functions create a sketchy, hand-drawn aesthetic.
4 */
5
6/**
7 * Create a seeded random number generator.
8 */
9function createRandom(seed: number): () => number {
10 return () => {
11 seed = (seed * 1103515245 + 12345) & 0x7fffffff
12 return (seed / 0x7fffffff) * 2 - 1 // -1 to 1
13 }
14}
15
16/**
17 * Create a seeded random number generator returning 0-1.
18 */
19function createRandomPositive(seed: number): () => number {
20 return () => {
21 seed = (seed * 1103515245 + 12345) & 0x7fffffff
22 return seed / 0x7fffffff // 0 to 1
23 }
24}
25
26type RenderingContext2D = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D
27
28/**
29 * Draw a wobbly line using quadratic bezier curves.
30 * The line has a hand-drawn appearance with slight imperfections.
31 */
32export function drawWobblyLine(
33 ctx: RenderingContext2D,
34 x1: number,
35 y1: number,
36 x2: number,
37 y2: number,
38 wobbleAmount: number = 2,
39 seed: number = 0,
40): void {
41 const random = createRandom(seed)
42
43 const dx = x2 - x1
44 const dy = y2 - y1
45 const length = Math.sqrt(dx * dx + dy * dy)
46
47 if (length < 0.001) return
48
49 // Perpendicular direction for wobble
50 const px = -dy / length
51 const py = dx / length
52
53 // Control point at midpoint with wobble
54 const midX = (x1 + x2) / 2 + px * wobbleAmount * random()
55 const midY = (y1 + y2) / 2 + py * wobbleAmount * random()
56
57 ctx.beginPath()
58 ctx.moveTo(x1 + random() * 0.5, y1 + random() * 0.5)
59 ctx.quadraticCurveTo(midX, midY, x2 + random() * 0.5, y2 + random() * 0.5)
60 ctx.stroke()
61}
62
63/**
64 * Draw a slightly irregular dot (circle with wobble).
65 */
66export function drawWobblyDot(
67 ctx: RenderingContext2D,
68 x: number,
69 y: number,
70 radius: number,
71 seed: number = 0,
72): void {
73 const random = createRandom(seed)
74
75 ctx.beginPath()
76
77 // Draw slightly irregular circle using 8 points
78 const points = 8
79 for (let i = 0; i <= points; i++) {
80 const angle = (i / points) * Math.PI * 2
81 const r = radius * (1 + random() * 0.1)
82 const px = x + Math.cos(angle) * r
83 const py = y + Math.sin(angle) * r
84
85 if (i === 0) {
86 ctx.moveTo(px, py)
87 } else {
88 ctx.lineTo(px, py)
89 }
90 }
91
92 ctx.closePath()
93 ctx.fill()
94}
95
96/**
97 * Draw a watercolor-style box fill with layered, slightly transparent fills.
98 */
99export function drawWatercolorFill(
100 ctx: CanvasRenderingContext2D,
101 x: number,
102 y: number,
103 size: number,
104 color: string,
105 seed: number = 0,
106): void {
107 const random = createRandomPositive(seed)
108
109 // Main fill with some transparency variation
110 ctx.fillStyle = color
111
112 // Scale inset and wobble based on box size to prevent gaps at small sizes
113 // At large sizes (50px+), use full effect. At small sizes (4px), minimal effect.
114 const effectScale = Math.min(1, (size - 4) / 46) // 0 at 4px, 1 at 50px+
115 const baseInset = effectScale * 2 // 0-2px inset based on size
116 const wobbleScale = effectScale * 1 // 0-1px wobble based on size
117
118 // Reduce layers at smaller sizes for performance (3 layers not visible at small sizes)
119 const layers = size < 10 ? 1 : size < 25 ? 2 : 3
120 for (let layer = 0; layer < layers; layer++) {
121 ctx.globalAlpha = 0.3 + random() * 0.2
122
123 const inset = baseInset * (0.5 + random() * 0.5)
124 const wobble = wobbleScale
125
126 ctx.beginPath()
127 ctx.moveTo(x + inset + random() * wobble, y + inset + random() * wobble)
128 ctx.lineTo(x + size - inset + random() * wobble, y + inset + random() * wobble)
129 ctx.lineTo(x + size - inset + random() * wobble, y + size - inset + random() * wobble)
130 ctx.lineTo(x + inset + random() * wobble, y + size - inset + random() * wobble)
131 ctx.closePath()
132 ctx.fill()
133 }
134
135 ctx.globalAlpha = 1
136}