Commit f95bb6f2 by PLN (Algolia)

404: per-sketch stage (// stage: WxH comment or frontmatter, ratio or dims,…

404: per-sketch stage (// stage: WxH comment or frontmatter, ratio or dims, full_moon 1:1), ?art=<id> pins the piece across reloads
parent 03ba3ad4
...@@ -23,6 +23,19 @@ function withLocalAssets(source, rewrites) { ...@@ -23,6 +23,19 @@ function withLocalAssets(source, rewrites) {
return out; return out;
} }
/** Native stage of a sketch, from a `// stage:` comment in the source or a
* `stage:` frontmatter line — default 1280×720, the laptop format most
* pieces are composed for. `WxH` (1080x1080) is literal dimensions; `W:H`
* and `W/H` (1:1, 4/5) are ratios, scaled to 1280 wide. The void
* letterboxes to it instead of stretching. */
function stageOf(source, data) {
const m = source.match(/^\s*\/\/\s*stage:\s*(\d+)\s*([x×:/])\s*(\d+)\s*$/m) ??
(typeof data.stage === 'string' ? data.stage.match(/^(\d+)\s*([x×:/])\s*(\d+)$/) : null);
if (!m || Number(m[1]) <= 0 || Number(m[3]) <= 0) return { w: 1280, h: 720 };
if (m[2] === 'x' || m[2] === '×') return { w: Number(m[1]), h: Number(m[3]) };
return { w: 1280, h: Math.round((1280 * Number(m[3])) / Number(m[1])) };
}
async function loadSketches() { async function loadSketches() {
const dir = path.join(process.cwd(), 'content', 'hydras'); const dir = path.join(process.cwd(), 'content', 'hydras');
const manifest = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'public/images/hydra/manifest.json'), 'utf8')); const manifest = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'public/images/hydra/manifest.json'), 'utf8'));
...@@ -36,6 +49,7 @@ async function loadSketches() { ...@@ -36,6 +49,7 @@ async function loadSketches() {
out.push({ out.push({
id: f.replace(/\.md$/, ''), id: f.replace(/\.md$/, ''),
title: sketch.credits?.title || data.title || f.replace(/\.md$/, ''), title: sketch.credits?.title || data.title || f.replace(/\.md$/, ''),
stage: stageOf(sketch.source, data),
source: withLocalAssets(sketch.source, manifest[f.replace(/\.md$/, '')] ?? {}), source: withLocalAssets(sketch.source, manifest[f.replace(/\.md$/, '')] ?? {}),
}); });
} }
......
...@@ -30,8 +30,10 @@ const GENERATIVE_RATE = 0.2; ...@@ -30,8 +30,10 @@ const GENERATIVE_RATE = 0.2;
function generativePiece() { function generativePiece() {
const star = `/images/404/star-${String(1 + Math.floor(Math.random() * 31)).padStart(2, '0')}.jpg`; const star = `/images/404/star-${String(1 + Math.floor(Math.random() * 31)).padStart(2, '0')}.jpg`;
return { return {
id: null, id: 'poussiere-detoiles',
generative: true,
title: 'poussière d’étoiles', title: 'poussière d’étoiles',
stage: { w: STAGE_W, h: STAGE_H },
source: `// traitement DustyHalley sur une photo d'étoile au hasard source: `// traitement DustyHalley sur une photo d'étoile au hasard
s0.initImage("${star}") s0.initImage("${star}")
src(s0).out(o0) src(s0).out(o0)
...@@ -98,12 +100,15 @@ export default function NotFoundHydra({ sketches }) { ...@@ -98,12 +100,15 @@ export default function NotFoundHydra({ sketches }) {
const boot = (piece) => { const boot = (piece) => {
const canvas = canvasRef.current; const canvas = canvasRef.current;
if (!canvas) return false; if (!canvas) return false;
const { w, h } = piece.stage ?? { w: STAGE_W, h: STAGE_H };
try { try {
canvas.width = w;
canvas.height = h;
instance = new HydraModule.default({ instance = new HydraModule.default({
canvas, canvas,
detectAudio: false, detectAudio: false,
width: STAGE_W, width: w,
height: STAGE_H, height: h,
}); });
instance.eval(sanitize(piece.source)); instance.eval(sanitize(piece.source));
bootedRef.current.push(piece); bootedRef.current.push(piece);
...@@ -121,13 +126,23 @@ export default function NotFoundHydra({ sketches }) { ...@@ -121,13 +126,23 @@ export default function NotFoundHydra({ sketches }) {
import('hydra-synth') import('hydra-synth')
.then((Hydra) => { .then((Hydra) => {
HydraModule = Hydra; HydraModule = Hydra;
// one visit in five dreams (generative); the rest rotate the works // ?art=<id> pins the piece (reload keeps it); otherwise one visit
// via the shuffle-bag — a failing boot falls to the next candidate // in five dreams (generative) and the rest rotate via the
const candidates = Math.random() < GENERATIVE_RATE // shuffle-bag — a failing boot falls to the next candidate
? [generativePiece()] const wanted = new URLSearchParams(window.location.search).get('art');
: bag(sketches, 3); let candidates;
if (wanted === generativePiece().id) candidates = [generativePiece()];
else if (wanted && sketches.some((s) => s.id === wanted)) candidates = sketches.filter((s) => s.id === wanted);
else candidates = Math.random() < GENERATIVE_RATE ? [generativePiece()] : bag(sketches, 3);
for (const piece of candidates) { for (const piece of candidates) {
if (boot(piece)) break; if (boot(piece)) {
if (piece.id) {
const url = new URL(window.location.href);
url.searchParams.set('art', piece.id);
window.history.replaceState(null, '', url);
}
break;
}
} }
}) })
.catch((ex) => { .catch((ex) => {
...@@ -175,15 +190,13 @@ export default function NotFoundHydra({ sketches }) { ...@@ -175,15 +190,13 @@ export default function NotFoundHydra({ sketches }) {
> >
<canvas <canvas
ref={canvasRef} ref={canvasRef}
width={STAGE_W}
height={STAGE_H}
aria-hidden="true" aria-hidden="true"
style={{ style={{
aspectRatio: '16 / 9', aspectRatio: current?.stage ? `${current.stage.w} / ${current.stage.h}` : '16 / 9',
width: '100vw', width: '100vw',
height: 'auto', height: 'auto',
maxHeight: '100vh', maxHeight: '100vh',
maxWidth: 'calc(100vh * 16 / 9)', maxWidth: current?.stage ? `calc(100vh * ${current.stage.w} / ${current.stage.h})` : 'calc(100vh * 16 / 9)',
}} }}
/> />
<div <div
...@@ -202,7 +215,7 @@ export default function NotFoundHydra({ sketches }) { ...@@ -202,7 +215,7 @@ export default function NotFoundHydra({ sketches }) {
<a href="/" onClick={(e) => e.stopPropagation()} style={barLink}> <a href="/" onClick={(e) => e.stopPropagation()} style={barLink}>
← accueil ← accueil
</a> </a>
{current?.id ? ( {current?.id && !current.generative ? (
<a href={`/hydra/${current.id}`} onClick={openWork} style={barLink}> <a href={`/hydra/${current.id}`} onClick={openWork} style={barLink}>
{current.title} · h = code {current.title} · h = code
</a> </a>
......
--- ---
stage: "1:1"
title: Full M00N title: Full M00N
source: https://git.plnech.fr/pln/Hydra/raw/master/biarritz/pleine_lune.js source: https://git.plnech.fr/pln/Hydra/raw/master/biarritz/pleine_lune.js
link: https://nech.pl/fullmoon link: https://nech.pl/fullmoon
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment