Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<br>

241 pieces for React, Next.js, Astro or plain HTML.<br>
242 pieces for React, Next.js, Astro or plain HTML.<br>
The banner above is drawn by ascii.rest: [make your own](https://ascii.rest/banner/).

[ascii.rest](https://ascii.rest) · [docs](https://ascii.rest/docs/) · [install](#install) · [make your own](#make-your-own) · [pieces](#pieces) · [contributing](CONTRIBUTING.md)
Expand All @@ -38,7 +38,7 @@ The banner above is drawn by ascii.rest: [make your own](https://ascii.rest/bann

<br>

Written in TypeScript by [@bas3line](https://github.com/bas3line). 241 pieces, from full-colour scenes to loaders, charts, language logos, Linux distros, spinning shapes and physics. The donut above turns on a page with one tag: `<ascii-art piece="donut"></ascii-art>`. See them all at [ascii.rest](https://ascii.rest).
Written in TypeScript by [@bas3line](https://github.com/bas3line). 242 pieces, from full-colour scenes to loaders, charts, language logos, Linux distros, spinning shapes and physics. The donut above turns on a page with one tag: `<ascii-art piece="donut"></ascii-art>`. See them all at [ascii.rest](https://ascii.rest).

<p align="center">
<a href="https://ascii.rest/night-coast/"><img src="https://ascii.rest/og/night-coast.png" width="32%" alt="night coast"></a>
Expand Down Expand Up @@ -345,7 +345,7 @@ pre.art { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberatio
| physics | [bouncing balls](https://ascii.rest/bouncing-balls/), [chladni plate](https://ascii.rest/chladni/), [double pendulum](https://ascii.rest/double-pendulum/), [falling sand](https://ascii.rest/falling-sand/), [flag](https://ascii.rest/flag/), [fountain](https://ascii.rest/fountain/), [harmonograph](https://ascii.rest/harmonograph/), [lorenz attractor](https://ascii.rest/lorenz/), [newton's cradle](https://ascii.rest/newtons-cradle/), [pendulum wave](https://ascii.rest/pendulum-wave/), [plucked string](https://ascii.rest/plucked-string/), [pond ripples](https://ascii.rest/pond-ripples/), [smoke](https://ascii.rest/smoke/), [wave interference](https://ascii.rest/wave-interference/) |
| nature | [aurora](https://ascii.rest/aurora/), [bonsai](https://ascii.rest/bonsai/), [campfire](https://ascii.rest/campfire/), [cherry blossom](https://ascii.rest/cherry-blossom/), [contour map](https://ascii.rest/contour-map/), [fern](https://ascii.rest/fern/), [fireflies](https://ascii.rest/fireflies/), [fractal tree](https://ascii.rest/fractal-tree/), [landscape](https://ascii.rest/landscape/), [lightning](https://ascii.rest/lightning/), [rain](https://ascii.rest/rain/), [ruled mountains](https://ascii.rest/ruled-mountains/), [sea swell](https://ascii.rest/sea-swell/), [snowfall](https://ascii.rest/snowfall/), [sunrise](https://ascii.rest/sunrise/), [wind](https://ascii.rest/wind/) |
| creatures | [aquarium](https://ascii.rest/aquarium/), [butterfly](https://ascii.rest/butterfly/), [cat](https://ascii.rest/cat/), [fox](https://ascii.rest/fox/), [jellyfish](https://ascii.rest/jellyfish/), [owl](https://ascii.rest/owl/), [snake](https://ascii.rest/snake/), [spider](https://ascii.rest/spider/), [starlings](https://ascii.rest/starlings/), [whale](https://ascii.rest/whale/) |
| objects | [analog clock](https://ascii.rest/analog-clock/), [candle](https://ascii.rest/candle/), [coffee](https://ascii.rest/coffee/), [ferris wheel](https://ascii.rest/ferris-wheel/), [hawa mahal](https://ascii.rest/hawa-mahal/), [hourglass](https://ascii.rest/hourglass/), [kite](https://ascii.rest/kite/), [lava lamp](https://ascii.rest/lava-lamp/), [lighthouse](https://ascii.rest/lighthouse/), [skyline](https://ascii.rest/skyline/), [sundial](https://ascii.rest/sundial/), [train](https://ascii.rest/train/), [vinyl](https://ascii.rest/vinyl/), [windmill](https://ascii.rest/windmill/) |
| objects | [analog clock](https://ascii.rest/analog-clock/), [basketball](https://ascii.rest/basketball/), [candle](https://ascii.rest/candle/), [coffee](https://ascii.rest/coffee/), [ferris wheel](https://ascii.rest/ferris-wheel/), [hawa mahal](https://ascii.rest/hawa-mahal/), [hourglass](https://ascii.rest/hourglass/), [kite](https://ascii.rest/kite/), [lava lamp](https://ascii.rest/lava-lamp/), [lighthouse](https://ascii.rest/lighthouse/), [skyline](https://ascii.rest/skyline/), [sundial](https://ascii.rest/sundial/), [train](https://ascii.rest/train/), [vinyl](https://ascii.rest/vinyl/), [windmill](https://ascii.rest/windmill/) |
| generative | [epicycles](https://ascii.rest/epicycles/), [flow field](https://ascii.rest/flow-field/), [glider gun](https://ascii.rest/glider-gun/), [hilbert curve](https://ascii.rest/hilbert-curve/), [julia set](https://ascii.rest/julia-set/), [langton's ant](https://ascii.rest/langtons-ant/), [mandelbrot](https://ascii.rest/mandelbrot/), [maze](https://ascii.rest/maze/), [plasma](https://ascii.rest/plasma/), [reaction diffusion](https://ascii.rest/reaction-diffusion/), [rule 30](https://ascii.rest/rule-30/), [sierpinski](https://ascii.rest/sierpinski/), [voronoi](https://ascii.rest/voronoi/) |
| effects | [doom fire](https://ascii.rest/doom-fire/), [fireworks](https://ascii.rest/fireworks/), [matrix rain](https://ascii.rest/matrix-rain/), [rotozoomer](https://ascii.rest/rotozoomer/), [sparks](https://ascii.rest/sparks/), [synthwave](https://ascii.rest/synthwave/), [tunnel](https://ascii.rest/tunnel/), [tv static](https://ascii.rest/tv-static/) |

Expand Down
4 changes: 2 additions & 2 deletions skills/ascii-rest/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
name: ascii-rest
description: Add animated ascii art with ascii.rest to web pages, GitHub READMEs and terminals. 241 ready pieces (scenes, spinners, charts, language, company and Linux distro logos, 3D shapes, weather, creatures), text in big block-letter banners, any image or logo turned to ascii, and a kit for making new ones. Use when the user wants ascii art, a retro or terminal look, a text animation or loader, an animated README banner or logo, a CLI splash screen, or names ascii.rest, in React, Next.js, Astro, Vue, Svelte, plain HTML, a README or a terminal.
description: Add animated ascii art with ascii.rest to web pages, GitHub READMEs and terminals. 242 ready pieces (scenes, spinners, charts, language, company and Linux distro logos, 3D shapes, weather, creatures), text in big block-letter banners, any image or logo turned to ascii, and a kit for making new ones. Use when the user wants ascii art, a retro or terminal look, a text animation or loader, an animated README banner or logo, a CLI splash screen, or names ascii.rest, in React, Next.js, Astro, Vue, Svelte, plain HTML, a README or a terminal.
---

<!-- Generated by scripts/skill.ts from scripts/skill/SKILL.md. Edit that, then run npm run gen. -->

# ascii.rest

ascii.rest is an npm package, `ascii.rest`, with no dependencies: 241 animated pieces of ascii art, each one a small TypeScript module, plus banners (any text in block letters) and a kit to make more. Every piece plays only while it is on screen and holds one still frame for readers who prefer reduced motion, so it needs no extra code for either.
ascii.rest is an npm package, `ascii.rest`, with no dependencies: 242 animated pieces of ascii art, each one a small TypeScript module, plus banners (any text in block letters) and a kit to make more. Every piece plays only while it is on screen and holds one still frame for readers who prefer reduced motion, so it needs no extra code for either.

## Work in this order

Expand Down
5 changes: 3 additions & 2 deletions skills/ascii-rest/references/pieces.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!-- Generated by scripts/skill.ts from each piece's meta. Run npm run gen after adding one. -->
# Every piece

241 pieces. The name is what `<Ascii piece="…">`, `<ascii-art piece="…">`, and `npx ascii.rest …` take; the export is what `import { … } from "ascii.rest/pieces"` gives. A **canvas** piece has colours of its own and draws on a `<canvas>`: size it with `width`. A **text** piece draws in a `<pre>` in the page's colour: size it with `font-size`. Options are the piece's own, with their defaults; pass them as `options`. Each piece plays at https://ascii.rest/<name>/. Only logos, companies and distros have hosted README SVGs, at https://ascii.rest/svg/<name>.svg and .dark.svg; any other piece needs `svg()` from `ascii.rest/svg` and a committed file.
242 pieces. The name is what `<Ascii piece="…">`, `<ascii-art piece="…">`, and `npx ascii.rest …` take; the export is what `import { … } from "ascii.rest/pieces"` gives. A **canvas** piece has colours of its own and draws on a `<canvas>`: size it with `width`. A **text** piece draws in a `<pre>` in the page's colour: size it with `font-size`. Options are the piece's own, with their defaults; pass them as `options`. Each piece plays at https://ascii.rest/<name>/. Only logos, companies and distros have hosted README SVGs, at https://ascii.rest/svg/<name>.svg and .dark.svg; any other piece needs `svg()` from `ascii.rest/svg` and a committed file.

## scenes (34)

Expand Down Expand Up @@ -269,11 +269,12 @@
| `starlings` | `starlings` | text | 64×16 | 30 | a murmuration wheeling about, split by a passing falcon | |
| `whale` | `whale` | text | 72×18 | 15 | a whale blows, arches its back and dives, fluke last | |

## objects (14)
## objects (15)

| name | export | draws | size | fps | what you see | options |
| --- | --- | --- | --- | --- | --- | --- |
| `analog-clock` | `analogClock` | text | 47×23 | 8 | a round clock face showing the local time, seconds ticking | `{"numerals":true,"seconds":true}` |
| `basketball` | `basketball` | text | 64×32 | 30 | a basketball spinning and bouncing lower with each landing | `{"radius":11.5,"gravity":47,"launchSpeed":23,"restitution":0.74,"restSeconds":0.65,"maxSpread":0.055,"maxSquash":0.08,"ramp":" .:-=+*#%@"}` |
| `candle` | `candle` | text | 40×30 | 15 | a flickering candle burning down in its holder, then renewed | |
| `coffee` | `coffee` | text | 52×24 | 20 | steam curling up from a cup of coffee on its saucer | |
| `ferris-wheel` | `ferrisWheel` | text | 64×26 | 10 | a wheel turning on its a-frame, cabins hanging upright | |
Expand Down
23 changes: 23 additions & 0 deletions src/basketball.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import * as basketball from "./pieces/basketball.ts";
import { loopOf, svg } from "./svg.ts";

test("basketball SVG uses the effective loop without changing configured defaults", () => {
const { createBasketball } = basketball;
const piece = createBasketball({ gravity: 60, restSeconds: 1 });
const overrides = { restSeconds: 5 };
const original = piece.meta.loop;
const expected = createBasketball({ gravity: 60, ...overrides }).meta.loop;

assert.equal(loopOf(piece, overrides).every, expected);
assert(svg(piece, { options: overrides, fps: 2 }).includes(`animation:${expected}s `));
assert(svg(piece, { options: overrides, seconds: 2, fps: 2 }).includes("animation:2s "));
assert.equal(loopOf(piece).every, original);
assert.equal(piece.meta.options?.restSeconds, 1);
assert.equal(loopOf(basketball, overrides).every, createBasketball(overrides).meta.loop);
assert.throws(
() => svg(piece, { options: { gravity: 1, launchSpeed: 60, restitution: 0.95 } }),
/at most 30 seconds/,
);
});
1 change: 1 addition & 0 deletions src/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const load = {
"aurora-fjord": () => import("./pieces/aurora-fjord.ts"),
"autumn": () => import("./pieces/autumn.ts"),
"bar-chart": () => import("./pieces/bar-chart.ts"),
"basketball": () => import("./pieces/basketball.ts"),
"big-text": () => import("./pieces/big-text.ts"),
"black-hole": () => import("./pieces/black-hole.ts"),
"black-prism": () => import("./pieces/black-prism.ts"),
Expand Down
187 changes: 187 additions & 0 deletions src/pieces/basketball.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/*
* basketball: a spinning ball that bounces lower each time and squashes
* against the floor, with seams drawn on the rotating surface.
*/
import type { Frame, Meta, Piece } from "../types.ts";

/** The bounces and final rest must fit within a 30-second loop. */
export interface BasketballOptions {
[key: string]: unknown;
/** Uncompressed radius in character rows (1 to 15), fitted to the frame. */
radius: number;
/** Downward acceleration in rows per second squared (1 to 300). */
gravity: number;
/** First takeoff speed in rows per second (1 to 60). */
launchSpeed: number;
/** Fraction of takeoff speed retained for the next bounce (0 to 0.95). */
restitution: number;
/** Seconds on the floor after the final bounce (0 to 10). */
restSeconds: number;
/** Maximum horizontal expansion as a fraction of the radius (0 to 1). */
maxSpread: number;
/** Maximum vertical compression as a fraction of the radius (0 to 0.9). */
maxSquash: number;
/** Shading glyphs from lightest to densest, starting with a space. */
ramp: string;
}

const DEFAULTS = {
radius: 11.5,
gravity: 47,
launchSpeed: 23,
restitution: 0.74,
restSeconds: 0.65,
maxSpread: 0.055,
maxSquash: 0.08,
ramp: " .:-=+*#%@",
} satisfies BasketballOptions;

const MAX_LOOP_SECONDS = 30;
const TAU = 2 * Math.PI;
const SAMPLE_X = [-0.125, 0.125];
const SAMPLE_Y = [-0.25, 0.25];
// Fixed seam orientation: screen-plane tilt and pitch, in radians.
const TILT_COS = Math.cos(0.35), TILT_SIN = Math.sin(0.35);
const PITCH_COS = Math.cos(0.245), PITCH_SIN = Math.sin(0.245);

function bounceSchedule({ gravity, launchSpeed, restitution, restSeconds }: BasketballOptions) {
const bounces: { start: number; land: number; end: number; speed: number }[] = [];
let end = 0;
let speed = launchSpeed;
// The first bounce always runs; later bounces stop below 4 rows per second.
do {
const start = end;
const land = start + (2 * speed) / gravity;
// Ground contact lasts 0.10 to 0.18 seconds, depending on impact speed.
end = land + 0.1 + 0.08 * (speed / launchSpeed);
bounces.push({ start, land, end, speed });
speed *= restitution;
} while (speed >= 4);
const loop = end + restSeconds;
// SVG export samples the whole loop before checking its byte budget.
if (loop > MAX_LOOP_SECONDS) {
throw new RangeError(`basketball: the bounce and rest loop must be at most ${MAX_LOOP_SECONDS} seconds`);
}
return { bounces, loop };
}

export const meta = {
name: "basketball",
category: "objects",
note: "a basketball spinning and bouncing lower with each landing",
cols: 64,
rows: 32,
fps: 30,
loop: bounceSchedule(DEFAULTS).loop,
options: DEFAULTS,
} satisfies Meta<BasketballOptions>;

function bounded(value: unknown, fallback: number, min: number, max: number): number {
return typeof value === "number" && Number.isFinite(value)
? Math.min(max, Math.max(min, value))
: fallback;
}

function settings(options: Partial<BasketballOptions>, defaults = DEFAULTS): BasketballOptions {
const ramp = options.ramp;
const validRamp = typeof ramp === "string" && ramp.startsWith(" ") && ramp.trimEnd().length > 0
&& !/[^\x20-\x7e·°\u2500-\u259f]/u.test(ramp);
return {
radius: bounded(options.radius, defaults.radius, 1, meta.rows / 2 - 1),
gravity: bounded(options.gravity, defaults.gravity, 1, 300),
launchSpeed: bounded(options.launchSpeed, defaults.launchSpeed, 1, 60),
restitution: bounded(options.restitution, defaults.restitution, 0, 0.95),
restSeconds: bounded(options.restSeconds, defaults.restSeconds, 0, 10),
maxSpread: bounded(options.maxSpread, defaults.maxSpread, 0, 1),
maxSquash: bounded(options.maxSquash, defaults.maxSquash, 0, 0.9),
ramp: validRamp ? ramp : defaults.ramp,
};
}

export default function basketball(options: Partial<BasketballOptions> = {}): Frame {
return drawBasketball(settings(options));
}

/** Compute the same loop that the renderer uses for these options. */
export function loopDuration(options: Partial<BasketballOptions> = {}): number {
return bounceSchedule(settings(options)).loop;
}

/** Configure a piece with its own defaults for playback and SVG export. */
export function createBasketball(options: Partial<BasketballOptions> = {}): Piece<BasketballOptions> {
const config = settings(options);
return {
meta: { ...meta, loop: bounceSchedule(config).loop, options: config },
loopDuration: (overrides = {}) => bounceSchedule(settings(overrides, config)).loop,
default: (overrides = {}) => drawBasketball(settings(overrides, config)),
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
};
}

function drawBasketball(config: BasketballOptions): Frame {
const { bounces, loop } = bounceSchedule(config);
const { cols, rows } = meta;
const { gravity, launchSpeed, maxSpread, maxSquash, ramp } = config;
const floor = rows - 1;
// Reserve room for the first jump and the widest landing. Scale the
// trajectory uniformly if its peak exceeds the available vertical space.
const radius = Math.min(config.radius, cols / (4 * (1 + maxSpread)));
const heightScale = Math.min(1, (floor - 2 * radius) / (launchSpeed ** 2 / (2 * gravity)));
const left = Array.from({ length: cols }, (_, c) => (c + 0.5 - cols / 2) / 2);
const output = new Array<string>(rows);

return (t, { paper = false } = {}) => {
const elapsed = ((t % loop) + loop) % loop;
const bounce = bounces.find(({ end }) => elapsed < end);
let height = 0;
let squash = 0;
if (bounce) {
if (elapsed < bounce.land) {
const air = elapsed - bounce.start;
height = air * (bounce.speed - gravity * air / 2) * heightScale;
} else {
const contact = (elapsed - bounce.land) / (bounce.end - bounce.land);
squash = (bounce.speed / launchSpeed) * Math.sin(Math.PI * contact);
}
}

// Keep the bottom of the ellipse on the floor throughout contact.
const rx = radius * (1 + maxSpread * squash);
const ry = radius * (1 - maxSquash * squash);
const cy = floor - height - ry;
// Start with a visible seam and complete one turn per motion period.
const angle = 0.55 + (TAU * elapsed) / loop;
const sin = Math.sin(angle), cos = Math.cos(angle);

for (let r = 0; r < rows; r++) {
let line = "";
for (const x of left) {
let ink = 0;
// Four subcell samples keep the silhouette and narrow seams legible.
for (const dy of SAMPLE_Y) {
const v = (cy - r - 0.5 - dy) / ry;
for (const dx of SAMPLE_X) {
const u = (x + dx) / rx;
const depth = 1 - u * u - v * v;
if (depth <= 0) continue;
const w = Math.sqrt(depth);

// Undo the tilt and spin before measuring distance to the seams.
const tiltedX = TILT_COS * u + TILT_SIN * v;
const tiltedY = TILT_COS * v - TILT_SIN * u;
const seamX = tiltedX * cos - w * sin;
const seamY = PITCH_COS * tiltedY - PITCH_SIN * (tiltedX * sin + w * cos);
const distance = Math.min(Math.abs(seamX), Math.abs(seamY), Math.abs(Math.abs(seamX) - 0.69));
const seam = Math.min(1, Math.max(0, (distance - 0.018) / 0.048));
const light = Math.max(0, -0.45 * u + 0.5 * v + 0.74 * w);
const tone = 0.24 + 0.72 * light;
const surface = tone * (0.36 + 0.64 * seam);
ink += paper ? 1.05 - surface : surface;
}
}
line += ramp[Math.round(ink * (ramp.length - 1) / 4)];
}
output[r] = line;
}
return output.join("\n");
};
}
1 change: 1 addition & 0 deletions src/pieces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * as aurora from "./aurora.ts";
export * as auroraFjord from "./aurora-fjord.ts";
export * as autumn from "./autumn.ts";
export * as barChart from "./bar-chart.ts";
export * as basketball from "./basketball.ts";
export * as bigText from "./big-text.ts";
export * as blackHole from "./black-hole.ts";
export * as blackPrism from "./black-prism.ts";
Expand Down
Loading
Loading