152 lines
5.1 KiB
Python
152 lines
5.1 KiB
Python
"""The drawing kit every animation module shares (tools/gen_animations.py and tools/streak_anims/*).
|
|
|
|
Coordinates: the origin (0, 0) is the checkbox's centre, one unit is the box's size, y points down
|
|
(so "up out of the box" is negative y). An animation is a function fn(i, t) -> [shape, ...] called
|
|
once per frame: i = the frame number, t = i / (frames - 1), 0..1. Every frame reseeds R (frames_of),
|
|
so the random wobble ("boil") differs frame to frame but the output is the same on every run.
|
|
|
|
Shapes (what P, L, C, NUM return; the format of shared/animations.json):
|
|
["p", w, x0, y0, x1, y1, ...] closed polygon; w = 0 filled, else stroke width
|
|
["l", w, x0, y0, ...] open polyline, stroke width w (round caps)
|
|
["c", w, cx, cy, r] circle; w = 0 filled, else stroke width
|
|
["k", "<colour name>"] the shapes after it use that colour (red / blue / yellow)
|
|
["n", w, cx, cy, h, rot] THE STREAK NUMBER, filled in by the painter (see NUM below and
|
|
routine/gui/anim_paint.py DIGITS): its digits as round-capped
|
|
strokes of width w, h tall, centred on (cx, cy), turned rot radians
|
|
"""
|
|
|
|
import math
|
|
import os
|
|
import random
|
|
|
|
FPS = 12
|
|
N = 12
|
|
COLORS = {"red": "#E8202A", "blue": "#1F5BD6", "yellow": "#FFC400"}
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
R = random.Random() # reseeded per frame: the boil differs frame to frame
|
|
|
|
|
|
def j(v, amt=0.05):
|
|
return v + R.uniform(-amt, amt)
|
|
|
|
|
|
def P(pts, w=0, boil=0.05):
|
|
out = ["p", w]
|
|
for x, y in pts:
|
|
out += [round(j(x, boil), 2), round(j(y, boil), 2)]
|
|
return out
|
|
|
|
|
|
def L(pts, w=0.18, boil=0.05):
|
|
out = ["l", w]
|
|
for x, y in pts:
|
|
out += [round(j(x, boil), 2), round(j(y, boil), 2)]
|
|
return out
|
|
|
|
|
|
def C(x, y, r, w=0, boil=0.05):
|
|
return ["c", w, round(j(x, boil), 2), round(j(y, boil), 2), round(max(0.02, j(r, boil * 0.6)), 2)]
|
|
|
|
|
|
def ring(cx, cy, r, n=12, rot=0.0, wob=0.06):
|
|
"""A hand-cut circle: a polygon whose radius wanders."""
|
|
return [(cx + (r + R.uniform(-wob, wob)) * math.cos(rot + 2 * math.pi * k / n),
|
|
cy + (r + R.uniform(-wob, wob)) * math.sin(rot + 2 * math.pi * k / n)) for k in range(n)]
|
|
|
|
|
|
def star(cx, cy, r1, r2, n=5, rot=-math.pi / 2):
|
|
pts = []
|
|
for k in range(2 * n):
|
|
r = r1 if k % 2 == 0 else r2
|
|
a = rot + math.pi * k / n
|
|
pts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
|
|
return pts
|
|
|
|
|
|
def rotate(pts, ang, cx=0.0, cy=0.0):
|
|
c, s = math.cos(ang), math.sin(ang)
|
|
return [(cx + (x - cx) * c - (y - cy) * s, cy + (x - cx) * s + (y - cy) * c) for x, y in pts]
|
|
|
|
|
|
def ease_out(t):
|
|
return 1 - (1 - t) ** 2
|
|
|
|
|
|
|
|
# ── the calm sets' helpers: slower, gentler ──
|
|
|
|
CALM_FRAMES = 24
|
|
CB = 0.025 # a gentler boil than the lively ones
|
|
|
|
|
|
def cp(pts, w=0):
|
|
return P(pts, w, CB)
|
|
|
|
|
|
def cl(pts, w=0.12):
|
|
return L(pts, w, CB)
|
|
|
|
|
|
def cc(x, y, r, w=0):
|
|
return C(x, y, r, w, CB)
|
|
|
|
|
|
def wavy(x0, x1, y, amp, ph, n=10, k=2.0):
|
|
return [(x0 + (x1 - x0) * m / n, y + amp * math.sin(ph + k * math.pi * m / n)) for m in range(n + 1)]
|
|
|
|
|
|
def inout(t, edge=0.2):
|
|
"""0 -> 1 over the first edge of the time, 1, then back to 0 over the last edge."""
|
|
return max(0.0, min(1.0, t / edge, (1 - t) / edge))
|
|
|
|
|
|
def oval(cx, cy, rx, ry, n=14, rot=0.0):
|
|
return rotate([(cx + rx * math.cos(2 * math.pi * k / n), cy + ry * math.sin(2 * math.pi * k / n))
|
|
for k in range(n)], rot, cx, cy)
|
|
|
|
|
|
def steam(x, top, i, t, n=3, h=1.4):
|
|
"""Soft curls of steam rising and thinning."""
|
|
s = []
|
|
g = inout(t, 0.3)
|
|
for k in range(n):
|
|
bx = x + (k - (n - 1) / 2) * 0.35
|
|
pts = [(bx + 0.12 * math.sin(i * 0.4 + k + m * 1.2), top - 0.15 - m * h / 5) for m in range(6)]
|
|
s.append(cl(pts, 0.03 + 0.07 * g))
|
|
return s
|
|
|
|
|
|
def frames_of(fn, n, seed):
|
|
"""fn's n frames; R reseeded per frame from seed, so every run gives the same output."""
|
|
out = []
|
|
for i in range(n):
|
|
R.seed(seed * 1000 + i)
|
|
out.append(fn(i, i / (n - 1)))
|
|
return out
|
|
|
|
|
|
# ── the streak animations (tools/streak_anims): 2 s, 24 frames, each shows the streak number ──
|
|
|
|
STREAK_FRAMES = 24
|
|
DIGIT_W, DIGIT_ADV = 0.6, 0.82 # a digit's width and advance, in digit heights (as anim_paint)
|
|
|
|
|
|
def num_width(h, digits=2):
|
|
"""How wide the number is drawn at height h with this many digits (for laying things out round it)."""
|
|
return h * ((digits - 1) * DIGIT_ADV + DIGIT_W)
|
|
|
|
|
|
def NUM(x, y, h, w=None, rot=0.0, boil=0.05):
|
|
"""The streak number, h tall, centred on (x, y), turned rot radians. The app fills in the real
|
|
number (7, 21, ..., 465, ...): lay things out for up to 3 digits (num_width(h, 3)).
|
|
w = the stroke width (default h * 0.17). It boils like everything else."""
|
|
w = h * 0.17 if w is None else w
|
|
return ["n", round(w, 3), round(j(x, boil), 2), round(j(y, boil), 2), round(max(0.05, j(h, boil * 0.5)), 2),
|
|
round(rot + R.uniform(-boil, boil) * 0.6, 3)]
|
|
|
|
|
|
def K(name):
|
|
"""Switch colour: the shapes after it are drawn in that primary (red / blue / yellow)."""
|
|
return ["k", name]
|