From 6aa329f5d310b9051ccdb431f6939b0dec76eeea Mon Sep 17 00:00:00 2001 From: Bairui Su Date: Fri, 11 Oct 2024 22:57:11 -0400 Subject: [PATCH] Rename and refactor API (#143) --- README.md | 20 +++++++++++++++----- index.html | 24 ++++++++---------------- package.json | 4 ++-- src/cell.js | 10 ---------- src/context/index.js | 4 ++-- src/context/init.js | 1 + src/context/point.js | 7 +++++-- src/context/{run.js => render.js} | 2 +- src/index.js | 2 +- test/apps/star.js | 25 ++++++++++--------------- test/index.html | 3 +-- vite.config.js | 2 +- 12 files changed, 47 insertions(+), 57 deletions(-) delete mode 100644 src/cell.js rename src/context/{run.js => render.js} (97%) diff --git a/README.md b/README.md index 10d8e60..c70c3fe 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,25 @@ -# Charming Cell +# Charming Terminal -The P5 like JavaScript API for ASCII art. +The terminal renderer for Charming. > [!NOTE] > The current next branch is implementing the new proposal API for production use. Please refer to the [python branch](https://github.com/charming-art/charming-cell/tree/python) for the released Python version. +## Get started -## Resources 📚 +```js +import * as cm from "@charming-art/terminal"; -- Documentation - https://charmingjs.org/cell/ -- Examples - https://observablehq.com/d/18b3d6f3affff5bb +const context = await new cm.Context().init({mode: "double", width: 520, height: 520}); +const I = Array.from({length: 240}, (_, i) => i); +const A = I.map((i) => (i / 240) * 2 * Math.PI); +const X = A.map((t) => context.cols() / 2 + 12 * Math.cos(t) * Math.cos(t * 3)); +const Y = A.map((t) => context.rows() / 2 + 12 * Math.sin(t) * Math.cos(t * 3)); +const S = I.map(() => cm.wide("🌟")); +context.point(I, {x: X, y: Y, stroke: S}); + +document.body.append(context.render()); +``` ## License 📄 diff --git a/index.html b/index.html index 6f9bc39..c580dd0 100644 --- a/index.html +++ b/index.html @@ -9,23 +9,15 @@ diff --git a/package.json b/package.json index 8e794e1..33d4df4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@charming-art/cell", - "description": "The creative coding language for ASCII Art.", + "name": "@charming-art/terminal", + "description": "The terminal renderer for Charming.", "version": "0.0.1", "author": { "name": "pearmini", diff --git a/src/cell.js b/src/cell.js deleted file mode 100644 index bd21193..0000000 --- a/src/cell.js +++ /dev/null @@ -1,10 +0,0 @@ -import {Context} from "./context/index.js"; - -export async function cell(App) { - const ctx = new Context(); - const {setup, ...options} = App(ctx); - await ctx.init(options); - ctx.setup(setup); - ctx.run(); - return ctx.node(); -} diff --git a/src/context/index.js b/src/context/index.js index 0ec498f..e5f94b5 100644 --- a/src/context/index.js +++ b/src/context/index.js @@ -1,5 +1,5 @@ import {context_init} from "./init.js"; -import {context_run} from "./run.js"; +import {context_render} from "./render.js"; import {context_stroke} from "./stroke.js"; import {context_point} from "./point.js"; import {context_setup} from "./setup.js"; @@ -19,7 +19,7 @@ export function Context() { Object.defineProperties(Context.prototype, { setup: {value: context_setup, writable: true, configurable: true}, init: {value: context_init, writable: true, configurable: true}, - run: {value: context_run, writable: true, configurable: true}, + render: {value: context_render, writable: true, configurable: true}, stroke: {value: context_stroke, writable: true, configurable: true}, point: {value: context_point, writable: true, configurable: true}, rows: {value: context_rows, writable: true, configurable: true}, diff --git a/src/context/init.js b/src/context/init.js index a8e5c61..b90d39d 100644 --- a/src/context/init.js +++ b/src/context/init.js @@ -12,4 +12,5 @@ export async function context_init(options = {}) { this._terminal = terminal; this._renderer = renderer; this._terminal.background("#000"); + return this; } diff --git a/src/context/point.js b/src/context/point.js index d7d1738..dfadec3 100644 --- a/src/context/point.js +++ b/src/context/point.js @@ -1,4 +1,7 @@ -export function context_point(x, y) { - this._renderer.point(x, y); +export function context_point(I, {x: X, y: Y, stroke: S}) { + for (const i of I) { + this.stroke(S[i]); + this._renderer.point(X[i], Y[i]); + } return this; } diff --git a/src/context/run.js b/src/context/render.js similarity index 97% rename from src/context/run.js rename to src/context/render.js index 99546a0..04bff44 100644 --- a/src/context/run.js +++ b/src/context/render.js @@ -33,7 +33,7 @@ function draw() { } } -export function context_run() { +export function context_render() { this._setup?.(this); draw.call(this); return this._terminal.node(); diff --git a/src/index.js b/src/index.js index b68d092..eec98a9 100644 --- a/src/index.js +++ b/src/index.js @@ -1,2 +1,2 @@ -export {cell} from "./cell.js"; export {wide} from "./wide.js"; +export {Context} from "./context/index.js"; diff --git a/test/apps/star.js b/test/apps/star.js index 7a97d75..19f3321 100644 --- a/test/apps/star.js +++ b/test/apps/star.js @@ -1,17 +1,12 @@ -import * as Cell from "@charming-art/cell"; +import * as cm from "@charming-art/terminal"; -export function Star(ctx) { - return { - mode: "double", - width: 520, - height: 520, - setup() { - for (let t = 0; t <= Math.PI * 2; t += Math.PI / 120) { - const x = ctx.cols() / 2 + 12 * Math.cos(t) * Math.cos(t * 3); - const y = ctx.rows() / 2 + 12 * Math.sin(t) * Math.cos(t * 3); - ctx.stroke(Cell.wide("🌟")); - ctx.point(x, y); - } - }, - }; +export async function Star() { + const context = await new cm.Context().init({mode: "double", width: 520, height: 520}); + const I = Array.from({length: 240}, (_, i) => i); + const A = I.map((i) => (i / 240) * 2 * Math.PI); + const X = A.map((t) => context.cols() / 2 + 12 * Math.cos(t) * Math.cos(t * 3)); + const Y = A.map((t) => context.rows() / 2 + 12 * Math.sin(t) * Math.cos(t * 3)); + const S = I.map(() => cm.wide("🌟")); + context.point(I, {x: X, y: Y, stroke: S}); + return context.render(); } diff --git a/test/index.html b/test/index.html index cd5fc8b..c328b5f 100644 --- a/test/index.html +++ b/test/index.html @@ -3,7 +3,6 @@