askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 56 minutes ago
History
make_throbbers.py · 14.1 KB · 310 lines
| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | """Loading animations for a browser's tab, toolbar and page area: drawn here from plain shapes, nothing copied. |
| 3 | |
| 4 | python3 make_throbbers.py writes svg/, apng/, gif/ and preview.html beside this file |
| 5 | |
| 6 | Each state is one small SVG on a 16x16 grid (so it reads at tab size and scales up cleanly), animated with |
| 7 | SMIL as a seamless loop. From each SVG the script also renders transparent APNGs and matted GIFs at 16, 32 |
| 8 | and 64 px (and @2x), in a light-ink set for dark tab strips and a dark-ink set for light ones. |
| 9 | |
| 10 | Needs: Python 3, Pillow, Playwright with Chromium (only for the PNG/GIF rendering; the SVGs need nothing). |
| 11 | """ |
| 12 | import math |
| 13 | import os |
| 14 | import sys |
| 15 | |
| 16 | HERE = os.path.dirname(os.path.abspath(__file__)) |
| 17 | ACCENT, OK, FAIL = '#3b9eff', '#30a46c', '#e5484d' |
| 18 | INKS = {'dark': '#e6e6e6', 'light': '#24292f'} # ink for a dark strip, ink for a light strip |
| 19 | MATTES = {'dark': '#202124', 'light': '#f1f3f4'} # what GIF edges are blended towards |
| 20 | FPS = 15 |
| 21 | |
| 22 | |
| 23 | def f(x): |
| 24 | return f'{x:.3f}'.rstrip('0').rstrip('.') |
| 25 | |
| 26 | |
| 27 | def samples(fn, n=24): |
| 28 | """n+1 evenly spaced samples of a function of loop time 0..1 (first == last when fn is periodic).""" |
| 29 | return [fn(i / n) for i in range(n + 1)] |
| 30 | |
| 31 | |
| 32 | def anim(attr, fn, dur, n=24): |
| 33 | vals = ';'.join(f(v) if isinstance(v, (int, float)) else v for v in samples(fn, n)) |
| 34 | return f'<animate attributeName="{attr}" dur="{dur}s" repeatCount="indefinite" values="{vals}"/>' |
| 35 | |
| 36 | |
| 37 | def anim_t(kind, fn, dur, n=24): |
| 38 | vals = ';'.join(samples(fn, n)) |
| 39 | return (f'<animateTransform attributeName="transform" type="{kind}" dur="{dur}s" repeatCount="indefinite" ' |
| 40 | f'values="{vals}"/>') |
| 41 | |
| 42 | |
| 43 | def ease(x): |
| 44 | x = min(1.0, max(0.0, x)) |
| 45 | return x * x * (3 - 2 * x) |
| 46 | |
| 47 | |
| 48 | def ramp(t, a, b): |
| 49 | """0 before a, 1 after b, eased between.""" |
| 50 | return ease((t - a) / (b - a)) if b > a else float(t >= b) |
| 51 | |
| 52 | |
| 53 | def pulse(t, centre, width): |
| 54 | """1 at centre, falling to 0 at +-width, on a loop.""" |
| 55 | d = abs(((t - centre + 0.5) % 1.0) - 0.5) |
| 56 | return ease(1 - d / width) if d < width else 0.0 |
| 57 | |
| 58 | |
| 59 | # ---- the states: name -> (seconds per loop, what it means, svg body with {ink} {acc} {ok} {fail}) ---- |
| 60 | |
| 61 | def connecting(): |
| 62 | d = 1.2 |
| 63 | dots = ''.join( |
| 64 | f'<circle cx="{x}" cy="8" r="0.95" fill="{{acc}}" stroke="none">' |
| 65 | + anim('opacity', lambda t, c=c: 0.15 + 0.85 * pulse(t, c, 0.28), d) + '</circle>' |
| 66 | for x, c in ((6, 0.15), (8, 0.4), (10, 0.65))) |
| 67 | return d, 'connecting (finding and reaching the server)', ( |
| 68 | '<circle cx="2.8" cy="8" r="1.5" fill="{ink}" stroke="none"/>' |
| 69 | '<circle cx="13.2" cy="8" r="1.5"/>' + dots) |
| 70 | |
| 71 | |
| 72 | def tls(): |
| 73 | d = 1.8 |
| 74 | def y(t): |
| 75 | return -1.9 * (1 - ramp(t, 0.30, 0.48)) if t < 0.9 else -1.9 * ramp(t, 0.9, 1.0) |
| 76 | return d, 'secure handshake', ( |
| 77 | '<rect x="3.8" y="7.6" width="8.4" height="6" rx="1.3"/>' |
| 78 | '<g><path d="M5.6 7.6 V5.6 a2.4 2.4 0 0 1 4.8 0 V7.6"/>' |
| 79 | + anim_t('translate', lambda t: f'0 {f(y(t))}', d) + '</g>' |
| 80 | '<circle cx="8" cy="10.6" r="1.1" fill="{acc}" stroke="none">' |
| 81 | + anim('opacity', lambda t: ramp(t, 0.42, 0.52) * (1 - ramp(t, 0.88, 0.96)), d) + '</circle>') |
| 82 | |
| 83 | |
| 84 | def waiting(): |
| 85 | d = 1.1 |
| 86 | c = 2 * math.pi * 5.2 |
| 87 | return d, 'waiting for the server', ( |
| 88 | '<circle cx="8" cy="8" r="5.2" opacity="0.25"/>' |
| 89 | f'<circle cx="8" cy="8" r="5.2" stroke="{{acc}}" stroke-dasharray="{f(c * 0.28)} {f(c * 0.72)}">' |
| 90 | + anim_t('rotate', lambda t: f'{f(360 * t)} 8 8', d) + '</circle>') |
| 91 | |
| 92 | |
| 93 | def receiving(): |
| 94 | d = 1.0 |
| 95 | return d, 'receiving data', ( |
| 96 | '<path d="M3 10.8 V13.2 H13 V10.8"/>' |
| 97 | '<g stroke="{acc}"><path d="M8 2.6 V9.4 M5.4 6.9 L8 9.5 L10.6 6.9"/>' |
| 98 | + anim_t('translate', lambda t: f'0 {f(-3.4 * (1 - ramp(t, 0.0, 0.6)))}', d) |
| 99 | + anim('opacity', lambda t: ramp(t, 0.0, 0.18) * (1 - ramp(t, 0.72, 0.95)), d) + '</g>') |
| 100 | |
| 101 | |
| 102 | def redirecting(): |
| 103 | d = 1.4 |
| 104 | path = 'M2.8 11.4 H7.6 a3.4 3.4 0 0 0 3.4 -3.4 V4.2' |
| 105 | return d, 'redirecting to another address', ( |
| 106 | f'<path d="{path}" opacity="0.35"/><path d="M8.7 6 L11 3.7 L13.3 6" opacity="0.35"/>' |
| 107 | '<circle r="1.35" fill="{acc}" stroke="none">' |
| 108 | f'<animateMotion dur="{d}s" repeatCount="indefinite" path="{path}" calcMode="linear"/>' |
| 109 | + anim('opacity', lambda t: ramp(t, 0.0, 0.12) * (1 - ramp(t, 0.85, 1.0)), d) + '</circle>') |
| 110 | |
| 111 | |
| 112 | def challenge(): |
| 113 | d = 1.0 |
| 114 | pts = [] |
| 115 | for i in range(8): # an 8-tooth gear, from four points a tooth |
| 116 | a = i * math.pi / 4 |
| 117 | for da, r in ((-0.20, 4.3), (-0.12, 6.1), (0.12, 6.1), (0.20, 4.3)): |
| 118 | pts.append(f'{f(8 + r * math.cos(a + da))} {f(8 + r * math.sin(a + da))}') |
| 119 | gear = 'M' + ' L'.join(pts) + ' Z' |
| 120 | return d, 'bot check (a short proof of work)', ( |
| 121 | f'<g><path d="{gear}" stroke-width="1.3"/>' |
| 122 | + anim_t('rotate', lambda t: f'{f(45 * t)} 8 8', d) + '</g>' # one tooth per loop: seamless |
| 123 | '<circle cx="8" cy="8" r="1.5" fill="{acc}" stroke="none">' |
| 124 | + anim('opacity', lambda t: 0.35 + 0.65 * pulse(t, 0.5, 0.5), d) + '</circle>') |
| 125 | |
| 126 | |
| 127 | def pay_pending(): |
| 128 | d = 2.4 |
| 129 | return d, 'payment needed (waiting for a yes)', ( |
| 130 | '<g><circle cx="8" cy="8" r="5.4" stroke-dasharray="2.3 1.94"/>' |
| 131 | + anim_t('rotate', lambda t: f'{f(45 * t)} 8 8', d) + '</g>' # the dash pattern repeats every 45 degrees |
| 132 | '<path d="M8 5.6 V10.4" stroke="{acc}" stroke-width="1.9">' |
| 133 | + anim('opacity', lambda t: 0.3 + 0.7 * pulse(t, 0.5, 0.5), d) + '</path>') |
| 134 | |
| 135 | |
| 136 | def pay_paying(): |
| 137 | d = 1.1 |
| 138 | return d, 'paying', ( |
| 139 | '<path d="M13.2 4.6 V11.4"/><path d="M2.6 8 H4.4" opacity="0.35"/>' |
| 140 | '<g><circle cx="5" cy="8" r="2.6" stroke="{acc}"/><path d="M5 6.9 V9.1" stroke="{acc}" stroke-width="1.3"/>' |
| 141 | + anim_t('translate', lambda t: f'{f(6.2 * ramp(t, 0.05, 0.8))} 0', d) |
| 142 | + anim('opacity', lambda t: ramp(t, 0.0, 0.12) * (1 - ramp(t, 0.68, 0.86)), d) + '</g>') |
| 143 | |
| 144 | |
| 145 | def pay_settled(): |
| 146 | d = 2.6 |
| 147 | length = 8.4 |
| 148 | return d, 'paid', ( |
| 149 | '<circle cx="8" cy="8" r="5.4" stroke="{ok}"/>' |
| 150 | # the gap is longer than the path and the start offset goes past its end, or the round cap leaves a dot |
| 151 | f'<path d="M5.2 8.3 L7.2 10.3 L10.9 6.1" stroke="{{ok}}" stroke-width="1.9" stroke-dasharray="{length} {length + 6}">' |
| 152 | + anim('stroke-dashoffset', lambda t: (length + 3) * (1 - ramp(t, 0.05, 0.28)) if t < 0.9 else (length + 3) * ramp(t, 0.9, 1.0), d) |
| 153 | + '</path>') |
| 154 | |
| 155 | |
| 156 | def summarizing(): |
| 157 | d = 2.0 |
| 158 | def line(y, a, b, to): |
| 159 | return (f'<path d="M3 {y} H13">' |
| 160 | + anim('d', lambda t: f'M3 {y} H{f(13 - (13 - to) * ramp(t, a, b) * (1 - ramp(t, 0.9, 1.0)))}', d) + '</path>') |
| 161 | star = 'M12.4 1.6 L13 3 L14.4 3.6 L13 4.2 L12.4 5.6 L11.8 4.2 L10.4 3.6 L11.8 3 Z' |
| 162 | return d, 'AI is summarizing the page', ( |
| 163 | line(6.2, 0.10, 0.34, 9) + line(9.4, 0.26, 0.50, 6.5) + line(12.6, 0.42, 0.66, 8) |
| 164 | + f'<path d="{star}" fill="{{acc}}" stroke="none">' |
| 165 | + anim('opacity', lambda t: 0.25 + 0.75 * pulse(t, 0.72, 0.22), d) + '</path>') |
| 166 | |
| 167 | |
| 168 | def slow(): |
| 169 | d = 3.2 |
| 170 | glass = ('M4.6 2.6 H11.4 M4.6 13.4 H11.4 ' |
| 171 | 'M5.4 2.6 C5.4 5.8 7.4 6.8 8 8 C8.6 6.8 10.6 5.8 10.6 2.6 ' |
| 172 | 'M5.4 13.4 C5.4 10.2 7.4 9.2 8 8 C8.6 9.2 10.6 10.2 10.6 13.4') |
| 173 | return d, 'still waiting (slow)', ( |
| 174 | f'<g><path d="{glass}" stroke-width="1.3"/>' |
| 175 | '<path d="M8 8.9 V11.6" stroke="{acc}" stroke-width="1.2">' |
| 176 | + anim('opacity', lambda t: (0.25 + 0.75 * pulse(t * 4 % 1, 0.5, 0.5)) * (1 - ramp(t, 0.72, 0.8)), d, 32) + '</path>' |
| 177 | + anim_t('rotate', lambda t: f'{f(180 * ramp(t, 0.8, 1.0))} 8 8', d, 32) + '</g>') # half a turn looks the same |
| 178 | |
| 179 | |
| 180 | def failed(): |
| 181 | d = 2.0 |
| 182 | def x(t): |
| 183 | return 0.7 * math.sin(t / 0.22 * 4 * math.pi) * (1 - t / 0.22) if t < 0.22 else 0.0 |
| 184 | return d, 'failed', ( |
| 185 | '<circle cx="8" cy="8" r="5.6" opacity="0.35"/>' |
| 186 | '<g stroke="{fail}" stroke-width="1.9"><path d="M5.8 5.8 L10.2 10.2 M10.2 5.8 L5.8 10.2"/>' |
| 187 | + anim_t('translate', lambda t: f'{f(x(t))} 0', d, 44) + '</g>') |
| 188 | |
| 189 | |
| 190 | def offline(): |
| 191 | d = 2.2 |
| 192 | return d, 'offline (no connection)', ( |
| 193 | '<circle cx="2.8" cy="8" r="1.5" fill="{ink}" stroke="none"/><circle cx="13.2" cy="8" r="1.5"/>' |
| 194 | '<path d="M4.6 8 H6.3 M9.7 8 H11.4" opacity="0.5"/>' |
| 195 | '<path d="M7 10.8 L9 5.2" stroke="{fail}" stroke-width="1.7">' |
| 196 | + anim('opacity', lambda t: 0.25 + 0.75 * pulse(t, 0.5, 0.5), d) + '</path>') |
| 197 | |
| 198 | |
| 199 | STATES = { |
| 200 | 'connecting': connecting, 'secure': tls, 'waiting': waiting, 'receiving': receiving, |
| 201 | 'redirecting': redirecting, 'challenge': challenge, 'pay-pending': pay_pending, 'pay-paying': pay_paying, |
| 202 | 'pay-settled': pay_settled, 'summarizing': summarizing, 'slow': slow, 'failed': failed, 'offline': offline, |
| 203 | } |
| 204 | |
| 205 | |
| 206 | def svg(name, theme=None, size=None): |
| 207 | """theme None: ink is currentColor and the three colours are CSS variables (for inlining in a page).""" |
| 208 | dur, title, body = STATES[name]() |
| 209 | if theme is None: |
| 210 | ink = 'currentColor' |
| 211 | body = (body.replace('fill="{acc}"', 'style="fill:var(--throb-accent,%s)"' % ACCENT) |
| 212 | .replace('stroke="{acc}"', 'style="stroke:var(--throb-accent,%s)"' % ACCENT) |
| 213 | .replace('stroke="{ok}"', 'style="stroke:var(--throb-ok,%s)"' % OK) |
| 214 | .replace('stroke="{fail}"', 'style="stroke:var(--throb-fail,%s)"' % FAIL)) |
| 215 | else: |
| 216 | ink = INKS[theme] |
| 217 | body = body.format(ink=ink, acc=ACCENT, ok=OK, fail=FAIL) |
| 218 | dims = f' width="{size}" height="{size}"' if size else '' |
| 219 | return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16"{dims} fill="none" stroke="{ink}" ' |
| 220 | f'stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" role="img" aria-label="{title}">' |
| 221 | f'<title>{title}</title>{body}</svg>'), dur, title |
| 222 | |
| 223 | |
| 224 | def write_svgs(): |
| 225 | os.makedirs(f'{HERE}/svg', exist_ok=True) |
| 226 | for name in STATES: |
| 227 | for theme in (None, 'dark', 'light'): |
| 228 | text, _dur, _title = svg(name, theme) |
| 229 | open(f'{HERE}/svg/{name}{"-" + theme if theme else ""}.svg', 'w').write(text + '\n') |
| 230 | |
| 231 | |
| 232 | def render(): |
| 233 | """Frames from Chromium (the SVG's own clock is stepped, so loops are exact), then APNG and GIF from Pillow.""" |
| 234 | from io import BytesIO |
| 235 | from PIL import Image |
| 236 | from playwright.sync_api import sync_playwright |
| 237 | os.makedirs(f'{HERE}/apng', exist_ok=True) |
| 238 | os.makedirs(f'{HERE}/gif', exist_ok=True) |
| 239 | big = 128 |
| 240 | with sync_playwright() as p: |
| 241 | browser = p.chromium.launch(headless=True) |
| 242 | page = browser.new_page(viewport={'width': 200, 'height': 200}, device_scale_factor=1) |
| 243 | for name in STATES: |
| 244 | for theme in ('dark', 'light'): |
| 245 | text, dur, _title = svg(name, theme, big) |
| 246 | page.set_content(f'<body style="margin:0;background:transparent">{text}</body>') |
| 247 | page.evaluate('document.querySelector("svg").pauseAnimations()') |
| 248 | n = max(12, round(dur * FPS)) |
| 249 | frames = [] |
| 250 | for i in range(n): |
| 251 | page.evaluate('t => document.querySelector("svg").setCurrentTime(t)', dur * i / n) |
| 252 | shot = page.locator('svg').screenshot(omit_background=True) |
| 253 | frames.append(Image.open(BytesIO(shot)).convert('RGBA')) |
| 254 | ms = round(dur * 1000 / n) |
| 255 | for size in (16, 32, 64): |
| 256 | for scale, tag in ((1, ''), (2, '@2x')): |
| 257 | px = size * scale |
| 258 | small = [fr.resize((px, px), Image.LANCZOS) for fr in frames] |
| 259 | small[0].save(f'{HERE}/apng/{name}-{theme}-{size}{tag}.png', save_all=True, |
| 260 | append_images=small[1:], duration=ms, loop=0, disposal=1, blend=0) |
| 261 | save_gif(small, MATTES[theme], ms, f'{HERE}/gif/{name}-{theme}-{size}{tag}.gif') |
| 262 | browser.close() |
| 263 | |
| 264 | |
| 265 | def save_gif(frames, matte, ms, path): |
| 266 | """GIF has one transparent colour and no soft edges: blend the edges towards the strip colour (matte), |
| 267 | then make only the nearly empty pixels transparent. Right on that strip colour, a faint rim on others.""" |
| 268 | from PIL import Image |
| 269 | w, h = frames[0].size |
| 270 | back = Image.new('RGBA', (w, h), matte) |
| 271 | flat = [Image.alpha_composite(back, fr).convert('RGB') for fr in frames] |
| 272 | sheet = Image.new('RGB', (w * len(flat), h)) |
| 273 | for i, im in enumerate(flat): |
| 274 | sheet.paste(im, (i * w, 0)) |
| 275 | palette = sheet.quantize(colors=255, method=Image.Quantize.MEDIANCUT) |
| 276 | out = [] |
| 277 | for im, fr in zip(flat, frames): |
| 278 | q = im.quantize(palette=palette, dither=Image.Dither.NONE) |
| 279 | alpha = fr.getchannel('A') |
| 280 | q.paste(255, mask=alpha.point(lambda a: 255 if a < 40 else 0)) |
| 281 | out.append(q) |
| 282 | out[0].save(path, save_all=True, append_images=out[1:], duration=ms, loop=0, transparency=255, disposal=2) |
| 283 | |
| 284 | |
| 285 | def preview(): |
| 286 | rows = [] |
| 287 | for name in STATES: |
| 288 | _text, dur, title = svg(name) |
| 289 | cells = '' |
| 290 | for theme in ('dark', 'light'): |
| 291 | ims = ''.join(f'<img src="svg/{name}-{theme}.svg" width="{s}" height="{s}">' for s in (16, 32, 64)) |
| 292 | ims += ''.join(f'<img src="apng/{name}-{theme}-{s}@2x.png" width="{s}" height="{s}">' for s in (16, 32)) |
| 293 | ims += f'<img src="gif/{name}-{theme}-16@2x.gif" width="16" height="16">' |
| 294 | cells += f'<td class="{theme}">{ims}</td>' |
| 295 | rows.append(f'<tr><th>{name}<br><small>{title}, {dur}s</small></th>{cells}</tr>') |
| 296 | open(f'{HERE}/preview.html', 'w').write( |
| 297 | '<!doctype html><meta charset="utf-8"><title>Loading animations: draft</title>' |
| 298 | '<style>body{font:14px system-ui;margin:24px}table{border-collapse:collapse}th{text-align:left;padding:6px 16px 6px 0;' |
| 299 | 'font-weight:600}small{font-weight:400;color:#777}td{padding:10px 14px}td img{margin-right:14px;vertical-align:middle}' |
| 300 | f'.dark{{background:{MATTES["dark"]}}}.light{{background:{MATTES["light"]}}}</style>' |
| 301 | '<h1>Loading animations: draft</h1><p>Each cell: SVG at 16, 32, 64; APNG at 16, 32; GIF at 16. ' |
| 302 | 'Left on a dark strip, right on a light one.</p><table>' + ''.join(rows) + '</table>\n') |
| 303 | |
| 304 | |
| 305 | if __name__ == '__main__': |
| 306 | write_svgs() |
| 307 | if '--svg-only' not in sys.argv: |
| 308 | render() |
| 309 | preview() |
| 310 | print('wrote', len(STATES), 'states to', HERE) |