From a376abe336d29fac71a80f41851369be58330bf4 Mon Sep 17 00:00:00 2001 From: Mike Lynch Date: Sat, 3 Jan 2026 08:22:58 +1100 Subject: [PATCH] Rearranged things --- genuary01.py => 01/genuary01.py | 0 genuary01.svg => 01/genuary01.svg | 0 {genuary02 => 02}/observablehq.config.js | 0 {genuary02 => 02}/package-lock.json | 0 {genuary02 => 02}/package.json | 0 {genuary02 => 02}/src/index.md | 74 +++++++++++++++++------ {genuary02 => 02}/src/observable.png | Bin 7 files changed, 57 insertions(+), 17 deletions(-) rename genuary01.py => 01/genuary01.py (100%) rename genuary01.svg => 01/genuary01.svg (100%) rename {genuary02 => 02}/observablehq.config.js (100%) rename {genuary02 => 02}/package-lock.json (100%) rename {genuary02 => 02}/package.json (100%) rename {genuary02 => 02}/src/index.md (60%) rename {genuary02 => 02}/src/observable.png (100%) diff --git a/genuary01.py b/01/genuary01.py similarity index 100% rename from genuary01.py rename to 01/genuary01.py diff --git a/genuary01.svg b/01/genuary01.svg similarity index 100% rename from genuary01.svg rename to 01/genuary01.svg diff --git a/genuary02/observablehq.config.js b/02/observablehq.config.js similarity index 100% rename from genuary02/observablehq.config.js rename to 02/observablehq.config.js diff --git a/genuary02/package-lock.json b/02/package-lock.json similarity index 100% rename from genuary02/package-lock.json rename to 02/package-lock.json diff --git a/genuary02/package.json b/02/package.json similarity index 100% rename from genuary02/package.json rename to 02/package.json diff --git a/genuary02/src/index.md b/02/src/index.md similarity index 60% rename from genuary02/src/index.md rename to 02/src/index.md index 349f14c..5a70bd4 100644 --- a/genuary02/src/index.md +++ b/02/src/index.md @@ -1,4 +1,10 @@ -# Genuary02 +# Genuary26 - 2 + +Prompt: [Twelve principles of animation](https://en.wikipedia.org/wiki/Twelve_basic_principles_of_animation) + +[Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) jazzed up with some d3 transitions. + +[Mike Lynch](https://etc.mikelynch.org/) ```js @@ -9,10 +15,24 @@ const CELL = 10; const CELLW = 80; const CELLH = 60; const GEN_TIME = 1000; -const INIT_PROB = 0.5 +const INIT_PROB = 0.5; + +const restart = { restart: false }; ``` +```js +const trigger = view(Inputs.button("Restart")); +``` + + +```js +trigger; +restart["restart"] = true; +``` + + + ```js @@ -26,7 +46,7 @@ function neighbours(i, j, id) { } -const test_g = (async function* () { +const show_grid = (async function* () { const grid = []; for( let j = 0; j < CELLH; j++ ) { @@ -41,6 +61,10 @@ const test_g = (async function* () { let i = 0; while ( true ) { + if( restart["restart"] ) { + grid.forEach((c) => c.live = Math.random() > INIT_PROB); + restart["restart"] = false + } yield grid.filter((d) => d.live); i++; const ngrid = []; @@ -56,19 +80,17 @@ const test_g = (async function* () { grid[id].live = ngrid[id]; } await new Promise((resolve) => setTimeout(resolve, GEN_TIME)); - } + } })(); - ``` ```js // Set up the svg canvas - const svg = d3.create("svg") .attr("width", WIDTH) .attr("height", HEIGHT) @@ -88,7 +110,7 @@ const bg_g = svg.append("g") .attr("id", "background"); bg_g.selectAll("rect") - .data( [ { bg: "yellow" } ] ) + .data( [ { bg: "white" } ] ) .join("rect") .attr("x", 0) .attr("y", 0) @@ -98,19 +120,37 @@ bg_g.selectAll("rect") ; const cells_g = svg.append("g") - .attr("id", "cells") - .attr("clip-path", "url(#clipRect)"); - -cells_g.selectAll("circle") - .data(test_g) - .join("circle") - .attr("cx", (d) => d.x) - .attr("cy", (d) => d.y) - .attr("r", (d) => d.live ? CELL * 0.4 : 0) - .attr("fill", "blue"); + .attr("id", "cells"); +// .attr("clip-path", "url(#clipRect)"); display(svg.node()); +const ease = d3.easeElastic.period(0.4).amplitude(3); +``` + +```js + +cells_g.selectAll("circle") + .data(show_grid, d => d.id) + .join( + enter => enter.append("circle") + .attr("cx", (d) => d.x) + .attr("cy", (d) => d.y) + .attr("fill", "green") + .transition() + .ease(ease) + .duration(1000) + .attr("r", (d) => 10), + update => update.attr("fill", "blue"), + exit => exit + .transition() + .duration(2000) + .attr("r", 0) + .attr("cy", (d) => d.y + 400) + .remove() + ); + + ``` diff --git a/genuary02/src/observable.png b/02/src/observable.png similarity index 100% rename from genuary02/src/observable.png rename to 02/src/observable.png