05カーソル座標値でマスを直接オン・オフする描画オートマトン
ポインタが格子上を動く間、座標値をマス番号に変換してそのマスの生死値を強制的に反転させ、手を離すと反転させた配置のまま規則表に従って世代が自動的に進む。
使いどころパターン手描きデモ、教育用シミュレーター体験版
autoSeedpointermoveSURVIVE_MIN

<main class="stage">
<div class="rv is-demo" tabindex="0" role="button" aria-label="シード描画進行中 — ポインターでマスを描くか、スペースキーで停止、R で再シード">
<canvas class="rv__cv" width="480" height="300"></canvas>
<p class="rv__hint">ポインターで描画・スペースキーで停止・R で再シード</p>
</div>
</main>@use "../../../../site/static/demo-tokens" as *;
@include stage($stage-ink);
.rv {
position: relative;
width: min(92vw, calc(160vh - 12.8vw));
aspect-ratio: 480 / 300;
margin: 0 auto;
border-radius: $r-card;
overflow: hidden;
cursor: crosshair;
touch-action: none;
box-shadow: $shadow-raised;
outline-offset: 4px;
&:focus-visible {
outline: 3px solid $subject-cream;
}
}
.rv__cv {
display: block;
width: 100%;
height: 100%;
}
.rv__hint {
position: absolute;
left: $sp-3;
bottom: $sp-3;
margin: 0;
padding: $sp-1 $sp-3;
border-radius: $r-pill;
font-size: 12px;
line-height: 1.4;
font-weight: 500;
background: rgba(255, 247, 230, .85);
color: $subject-ink;
pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
.rv {
cursor: crosshair;
}
}html, body {
margin: 0;
overflow: hidden;
}
.stage {
box-sizing: border-box;
min-height: 100vh;
display: grid;
place-items: center;
margin: 0;
background: #17141a;
font-family: -apple-system, "Segoe UI", "Noto Sans KR", sans-serif;
padding: clamp(8px, 4vw, 24px);
}
.rv {
position: relative;
width: min(92vw, 160vh - 12.8vw);
aspect-ratio: 480/300;
margin: 0 auto;
border-radius: 16px;
overflow: hidden;
cursor: crosshair;
touch-action: none;
box-shadow: 0 14px 24px rgba(23, 20, 26, 0.32);
outline-offset: 4px;
}
.rv:focus-visible {
outline: 3px solid #fff7e6;
}
.rv__cv {
display: block;
width: 100%;
height: 100%;
}
.rv__hint {
position: absolute;
left: 12px;
bottom: 12px;
margin: 0;
padding: 4px 12px;
border-radius: 999px;
font-size: 12px;
line-height: 1.4;
font-weight: 500;
background: rgba(255, 247, 230, 0.85);
color: #17141a;
pointer-events: none;
}
@media (prefers-reduced-motion: reduce) {
.rv {
cursor: crosshair;
}
}(function(){
'use strict';
// 395-09(진행률 채움 로더)와 다른 축: 포인터 좌표 값이 칸의 생사를 직접 뒤집는 계산이 주인공.
// 항목 05 는 click 대신 pointerdown·pointermove 로 씨앗을 그린다 — 손을 떼면 규칙표로 자동 진행.
function makeLcg(seed){
let s = seed >>> 0;
return function(){
s = (s * 1664525 + 1013904223) >>> 0;
return s / 4294967296;
};
}
const W = 480, H = 300;
const CELL = 10, COLS = W / CELL, ROWS = H / CELL;
const INK = '#17141a';
const ALIVE_COLOR = '#93a4ca';
let SEED = 50501;
let rnd = makeLcg(SEED);
const SURVIVE_MIN = 2, SURVIVE_MAX = 3, BIRTH_COUNT = 3;
let grid = new Uint8Array(COLS * ROWS);
let next = new Uint8Array(COLS * ROWS);
function idx(x, y){ return y * COLS + x; }
function autoSeed(){
grid.fill(0);
for (let y = 8; y <= 21; y++){
for (let x = 14; x <= 33; x++){
if (rnd() < 0.34) grid[idx(x, y)] = 1;
}
}
}
function neighborCount(x, y){
let n = 0;
for (let dy = -1; dy <= 1; dy++){
for (let dx = -1; dx <= 1; dx++){
if (dx === 0 && dy === 0) continue;
const nx = x + dx, ny = y + dy;
if (nx < 0 || ny < 0 || nx >= COLS || ny >= ROWS) continue;
n += grid[idx(nx, ny)];
}
}
return n;
}
function step(){
for (let y = 0; y < ROWS; y++){
for (let x = 0; x < COLS; x++){
const alive = grid[idx(x, y)] === 1;
const n = neighborCount(x, y);
let willLive = 0;
if (alive){
willLive = (n >= SURVIVE_MIN && n <= SURVIVE_MAX) ? 1 : 0;
} else {
willLive = (n === BIRTH_COUNT) ? 1 : 0;
}
next[idx(x, y)] = willLive;
}
}
const tmp = grid; grid = next; next = tmp;
}
const cvEl = document.querySelector('.rv__cv');
const ctx = cvEl.getContext('2d');
const rootEl = document.querySelector('.rv');
function render(){
ctx.fillStyle = INK;
ctx.fillRect(0, 0, W, H);
ctx.fillStyle = ALIVE_COLOR;
for (let y = 0; y < ROWS; y++){
for (let x = 0; x < COLS; x++){
if (grid[idx(x, y)] === 1){
ctx.fillRect(x * CELL + 1, y * CELL + 1, CELL - 1, CELL - 1);
}
}
}
}
const ARIA_PLAY = 'シード描画進行中 — ポインターでマスを描くか、スペースキーで停止、R で再シード';
const ARIA_DRAW = 'シード描画中 — 手を離すとルール表で自動進行';
const ARIA_PAUSE = 'シード描画停止中 — ポインターでマスを描くか、スペースキーで再生、R で再シード';
function updateAria(){
rootEl.setAttribute('aria-label', isDrawing ? ARIA_DRAW : (paused ? ARIA_PAUSE : ARIA_PLAY));
}
let paused = false;
let isDrawing = false;
const reduceMQ = window.matchMedia('(prefers-reduced-motion: reduce)');
function human(){
if (rootEl.classList.contains('is-demo')){
rootEl.classList.remove('is-demo');
grid.fill(0);
}
}
function reseed(){
SEED = (SEED * 1103515245 + 12345) >>> 0;
rnd = makeLcg(SEED || 1);
autoSeed();
paused = false;
}
function cellFromEvent(e){
const rect = cvEl.getBoundingClientRect();
const px = (e.clientX - rect.left) * (W / rect.width);
const py = (e.clientY - rect.top) * (H / rect.height);
const cx = Math.max(0, Math.min(COLS - 1, Math.floor(px / CELL)));
const cy = Math.max(0, Math.min(ROWS - 1, Math.floor(py / CELL)));
return [cx, cy];
}
function paintAt(e){
const c = cellFromEvent(e);
grid[idx(c[0], c[1])] = 1;
render();
}
function onPointerDown(e){
human();
isDrawing = true;
paused = true;
updateAria();
paintAt(e);
}
function onPointerMove(e){
if (!isDrawing) return;
paintAt(e);
}
function onPointerUp(){
if (!isDrawing) return;
isDrawing = false;
paused = false;
updateAria();
render();
}
function onKey(e){
if (e.key === 'r' || e.key === 'R'){
human();
reseed();
render();
updateAria();
return;
}
if (e.key !== 'Enter' && e.key !== ' ') return;
e.preventDefault();
human();
if (reduceMQ.matches){
step();
render();
} else {
paused = !paused;
updateAria();
}
}
const GEN_INTERVAL_MS = 85;
let acc = 0, last = 0;
function frame(now){
if (!reduceMQ.matches){
if (!last) last = now;
const dt = now - last;
last = now;
if (!paused && !isDrawing){
acc += dt;
while (acc >= GEN_INTERVAL_MS){
step();
acc -= GEN_INTERVAL_MS;
}
render();
}
requestAnimationFrame(frame);
}
}
function onResize(){ render(); }
rootEl.addEventListener('pointerdown', onPointerDown);
rootEl.addEventListener('pointermove', onPointerMove);
window.addEventListener('pointerup', onPointerUp);
rootEl.addEventListener('keydown', onKey);
window.addEventListener('resize', onResize);
autoSeed();
updateAria();
render();
if (!reduceMQ.matches){
requestAnimationFrame(frame);
}
})();import { useEffect, useRef } from 'react';
import styles from './PointerSeedDraw.module.scss';
function ctx2d(cv: HTMLCanvasElement): CanvasRenderingContext2D {
const c = cv.getContext('2d');
if (!c) throw new Error('no 2d context');
return c;
}
function makeLcg(seed: number): () => number {
let s = seed >>> 0;
return function(){
s = (s * 1664525 + 1013904223) >>> 0;
return s / 4294967296;
};
}
const W = 480, H = 300;
const CELL = 10, COLS = W / CELL, ROWS = H / CELL;
const INK = '#17141a';
const ALIVE_COLOR = '#93a4ca';
let SEED = 50501;
let rnd = makeLcg(SEED);
const SURVIVE_MIN = 2, SURVIVE_MAX = 3, BIRTH_COUNT = 3;
let grid = new Uint8Array(COLS * ROWS);
let next = new Uint8Array(COLS * ROWS);
function idx(x: number, y: number): number { return y * COLS + x; }
function autoSeed(): void {
grid.fill(0);
for (let y = 8; y <= 21; y++){
for (let x = 14; x <= 33; x++){
if (rnd() < 0.34) grid[idx(x, y)] = 1;
}
}
}
function neighborCount(x: number, y: number): number {
let n = 0;
for (let dy = -1; dy <= 1; dy++){
for (let dx = -1; dx <= 1; dx++){
if (dx === 0 && dy === 0) continue;
const nx = x + dx, ny = y + dy;
if (nx < 0 || ny < 0 || nx >= COLS || ny >= ROWS) continue;
n += grid[idx(nx, ny)];
}
}
return n;
}
function step(): void {
for (let y = 0; y < ROWS; y++){
for (let x = 0; x < COLS; x++){
const alive = grid[idx(x, y)] === 1;
const n = neighborCount(x, y);
let willLive = 0;
if (alive){
willLive = (n >= SURVIVE_MIN && n <= SURVIVE_MAX) ? 1 : 0;
} else {
willLive = (n === BIRTH_COUNT) ? 1 : 0;
}
next[idx(x, y)] = willLive;
}
}
const tmp = grid; grid = next; next = tmp;
}
function render(ctx: CanvasRenderingContext2D): void {
ctx.fillStyle = INK;
ctx.fillRect(0, 0, W, H);
ctx.fillStyle = ALIVE_COLOR;
for (let y = 0; y < ROWS; y++){
for (let x = 0; x < COLS; x++){
if (grid[idx(x, y)] === 1){
ctx.fillRect(x * CELL + 1, y * CELL + 1, CELL - 1, CELL - 1);
}
}
}
}
const ARIA_PLAY = '씨앗 그리기 진행 중 — 포인터로 칸을 그리거나 스페이스바로 정지, R 로 재시드';
const ARIA_DRAW = '씨앗 그리는 중 — 손을 떼면 규칙표로 자동 진행';
const ARIA_PAUSE = '씨앗 그리기 정지됨 — 포인터로 칸을 그리거나 스페이스바로 재생, R 로 재시드';
export default function PointerSeedDraw(){
const rootRef = useRef<HTMLDivElement | null>(null);
const cvRef = useRef<HTMLCanvasElement | null>(null);
useEffect(() => {
const root = rootRef.current;
const cv = cvRef.current;
if (!root || !cv) return;
const rootEl: HTMLDivElement = root;
const cvEl: HTMLCanvasElement = cv;
const ctx = ctx2d(cvEl);
function updateAria(): void {
rootEl.setAttribute('aria-label', isDrawing ? ARIA_DRAW : (paused ? ARIA_PAUSE : ARIA_PLAY));
}
let paused = false;
let isDrawing = false;
const reduceMQ = window.matchMedia('(prefers-reduced-motion: reduce)');
function human(): void {
if (rootEl.classList.contains('is-demo')){
rootEl.classList.remove('is-demo');
grid.fill(0);
}
}
function reseed(): void {
SEED = (SEED * 1103515245 + 12345) >>> 0;
rnd = makeLcg(SEED || 1);
autoSeed();
paused = false;
}
function cellFromEvent(e: PointerEvent): [number, number] {
const rect = cvEl.getBoundingClientRect();
const px = (e.clientX - rect.left) * (W / rect.width);
const py = (e.clientY - rect.top) * (H / rect.height);
const cx = Math.max(0, Math.min(COLS - 1, Math.floor(px / CELL)));
const cy = Math.max(0, Math.min(ROWS - 1, Math.floor(py / CELL)));
return [cx, cy];
}
function paintAt(e: PointerEvent): void {
const c = cellFromEvent(e);
grid[idx(c[0], c[1])] = 1;
render(ctx);
}
function onPointerDown(e: PointerEvent): void {
human();
isDrawing = true;
paused = true;
updateAria();
paintAt(e);
}
function onPointerMove(e: PointerEvent): void {
if (!isDrawing) return;
paintAt(e);
}
function onPointerUp(): void {
if (!isDrawing) return;
isDrawing = false;
paused = false;
updateAria();
render(ctx);
}
function onKey(e: KeyboardEvent): void {
if (e.key === 'r' || e.key === 'R'){
human();
reseed();
render(ctx);
updateAria();
return;
}
if (e.key !== 'Enter' && e.key !== ' ') return;
e.preventDefault();
human();
if (reduceMQ.matches){
step();
render(ctx);
} else {
paused = !paused;
updateAria();
}
}
const GEN_INTERVAL_MS = 85;
let acc = 0, last = 0, rafId = 0;
function frame(now: number): void {
if (!reduceMQ.matches){
if (!last) last = now;
const dt = now - last;
last = now;
if (!paused && !isDrawing){
acc += dt;
while (acc >= GEN_INTERVAL_MS){
step();
acc -= GEN_INTERVAL_MS;
}
render(ctx);
}
rafId = requestAnimationFrame(frame);
}
}
function onResize(): void { render(ctx); }
rootEl.addEventListener('pointerdown', onPointerDown);
rootEl.addEventListener('pointermove', onPointerMove);
window.addEventListener('pointerup', onPointerUp);
rootEl.addEventListener('keydown', onKey);
window.addEventListener('resize', onResize);
autoSeed();
updateAria();
render(ctx);
if (!reduceMQ.matches){
rafId = requestAnimationFrame(frame);
}
return () => {
rootEl.removeEventListener('pointerdown', onPointerDown);
rootEl.removeEventListener('pointermove', onPointerMove);
window.removeEventListener('pointerup', onPointerUp);
rootEl.removeEventListener('keydown', onKey);
window.removeEventListener('resize', onResize);
if (rafId) cancelAnimationFrame(rafId);
};
}, []);
return (
<main className={styles.stage}>
<div ref={rootRef} className={`${styles.rv} is-demo`} tabIndex={0} role="button" aria-label="씨앗 그리기 진행 중 — 포인터로 칸을 그리거나 스페이스바로 정지, R 로 재시드">
<canvas ref={cvRef} className={styles.rv__cv} width={480} height={300} />
<p className={styles.rv__hint}>포인터로 그리기 · 스페이스바 정지 · R 재시드</p>
</div>
</main>
);
}05 カーソル座標値でマスを直接オン・オフする描画オートマトン — 計測 累積面積 16.988% · 動いたフレーム 23/23GODRICH オリジナル · SCSS と React は zip に同梱 · ライフゲーム9種 — キャンバスで世代を計算
このデモが属する記事
ライフゲーム9種 — キャンバスで世代を計算ライフゲームのルールを計算だけで動かす9種のキャンバス実装です。格子は勝手に動いているように見えるが、実際には毎世代、生存隣接数という一つの値が生死を分け、その値が積み重なった年齢の分だけ色が薄れ、同じ値が配線を伝って信号のように流れる — この記事のデモをすべて見る


