Now with interactive settings!
This commit is contained in:
parent
b797f66721
commit
8a3ed2e733
2 changed files with 81 additions and 40 deletions
14
index.html
14
index.html
|
|
@ -5,7 +5,19 @@
|
|||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Thi.ng rdom</title>
|
||||
<style>html, body { padding: 0; margin: 0; min-height: 100vh; }</style>
|
||||
<style>
|
||||
html, body { padding: 0; margin: 0; min-height: 100vh; }
|
||||
body {
|
||||
background: #eee;
|
||||
color: #222;
|
||||
font-family: sans-serif;
|
||||
padding: 40px;
|
||||
}
|
||||
|
||||
canvas {
|
||||
border: 1px solid #99b;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="app"></div>
|
||||
|
|
|
|||
55
src/main.ts
55
src/main.ts
|
|
@ -1,12 +1,18 @@
|
|||
import { line, group, translate, asSvg, svgDoc } from "@thi.ng/geom";
|
||||
import { $canvas } from "@thi.ng/rdom-canvas";
|
||||
import { reactive } from "@thi.ng/rstream";
|
||||
import { cycle, takeWhile, comp, mapIndexed, transduce, push, map } from "@thi.ng/transducers"
|
||||
import { reactive, sync, } from "@thi.ng/rstream";
|
||||
import { $compile, $input } from '@thi.ng/rdom'
|
||||
import { cycle, takeWhile, comp, mapIndexed, transduce, push } from "@thi.ng/transducers"
|
||||
|
||||
const xs = cycle([0,1,0,1,0,1])
|
||||
const ys = cycle([1,0,1,1,0,1])
|
||||
const xPattern = "101110100101100"
|
||||
const yPattern = "101001111001110000101"
|
||||
|
||||
const cellSize = 25
|
||||
const parsePattern = (pattern: string) => pattern.split('').map(x => Number.parseInt(x, 10))
|
||||
|
||||
const xs = reactive(xPattern).map((pattern) => parsePattern(pattern))
|
||||
const ys = reactive(yPattern).map((pattern) => parsePattern(pattern))
|
||||
|
||||
const cellSize = 12
|
||||
|
||||
// available size for a2: 420 * 594
|
||||
const width = 420
|
||||
|
|
@ -33,9 +39,14 @@ const startPoints = (pattern: Iterable<number>, maxCoord: number) =>
|
|||
takeWhile(([coord, _]) => coord <= maxCoord),
|
||||
),
|
||||
push(),
|
||||
pattern
|
||||
cycle(pattern)
|
||||
)
|
||||
|
||||
const scene = sync({
|
||||
src: { xs, ys, }
|
||||
}).map(({ xs, ys }) => {
|
||||
console.log({ xs, ys })
|
||||
|
||||
const xStarts = startPoints(xs, width - 2 * xPadding)
|
||||
const yStarts = startPoints(ys, height - 2 * yPadding)
|
||||
|
||||
|
|
@ -49,8 +60,6 @@ const yLines = xStarts.flatMap(
|
|||
.filter(v => v != null)
|
||||
)
|
||||
|
||||
console.log({ xStarts, yStarts })
|
||||
|
||||
const xLines = yStarts.flatMap(
|
||||
([yCoord, val]: [number, number]) =>
|
||||
Array
|
||||
|
|
@ -61,16 +70,36 @@ const xLines = yStarts.flatMap(
|
|||
.filter(v => v != null)
|
||||
)
|
||||
|
||||
const scene = reactive(
|
||||
translate(
|
||||
return translate(
|
||||
group(
|
||||
{ __background: '#eee', stroke: '#222' },
|
||||
[...yLines, ...xLines]
|
||||
),
|
||||
[xPadding, yPadding]
|
||||
)
|
||||
)
|
||||
})
|
||||
|
||||
$canvas(scene, size).mount(document.body)
|
||||
$compile(
|
||||
["main",
|
||||
{},
|
||||
["h1", {}, "Hitomezashi Patterns"],
|
||||
$canvas(scene, size),
|
||||
["h2", {}, "Settings"],
|
||||
["div", {}, ["label", {}, "X Pattern: ",
|
||||
["input", {
|
||||
type: "text",
|
||||
value: xPattern,
|
||||
oninput: $input(ys)
|
||||
}]]],
|
||||
["div", {}, ["label", {}, "Y Pattern: ",
|
||||
["input", {
|
||||
type: "text",
|
||||
value: yPattern,
|
||||
oninput: $input(ys)
|
||||
}]]],
|
||||
["h2", {}, "SVG Export"],
|
||||
["div", {}, ["textarea", {}, asSvg(svgDoc({ viewBox: `0 0 ${size[0]} ${size[1]}` }, scene.deref()))]],
|
||||
]
|
||||
).mount(document.body)
|
||||
|
||||
// console.log(asSvg(svgDoc({ viewBox: `0 0 ${size[0]} ${size[1]}` }, scene.deref())))
|
||||
// console.log()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue