Skip to content

Commit

Permalink
fix(examples): rotating-voronoi changes after code review
Browse files Browse the repository at this point in the history
  • Loading branch information
nkint committed Jul 16, 2019
1 parent 94794c5 commit 4445fb9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
14 changes: 12 additions & 2 deletions examples/rotating-voronoi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,19 @@
},
"dependencies": {
"@thi.ng/api": "latest",
"@thi.ng/atom": "^3.0.1",
"@thi.ng/atom": "latest",
"@thi.ng/geom": "latest",
"@thi.ng/geom-api": "latest",
"@thi.ng/geom-resample": "latest",
"@thi.ng/geom-voronoi": "latest",
"@thi.ng/hdom": "latest",
"@thi.ng/hdom-canvas": "latest",
"@thi.ng/hiccup-svg": "latest",
"@thi.ng/math": "latest",
"@thi.ng/rstream": "latest",
"@thi.ng/transducers-hdom": "latest"
"@thi.ng/transducers": "latest",
"@thi.ng/transducers-hdom": "latest",
"@thi.ng/vectors": "latest"
},
"browserslist": [
"last 3 Chrome versions"
Expand Down
30 changes: 13 additions & 17 deletions examples/rotating-voronoi/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { serialize } from "@thi.ng/hiccup";
import { canvas } from "@thi.ng/hdom-canvas";
import { DVMesh } from "@thi.ng/geom-voronoi";
import { simplify } from "@thi.ng/geom-resample";
Expand All @@ -10,12 +9,13 @@ import {
svgDoc,
rect,
group,
points
points,
polygon,
asSvg
} from "@thi.ng/geom";
import { CubicOpts } from "@thi.ng/geom-api";
import { cartesian2, Vec } from "@thi.ng/vectors";
import { TAU, PI } from "@thi.ng/math";
import { convertTree } from "@thi.ng/hiccup-svg";
import { clearDOM } from "@thi.ng/hdom";
import { updateDOM } from "@thi.ng/transducers-hdom";
import {
Expand All @@ -36,6 +36,7 @@ import {
mapcat,
iterator
} from "@thi.ng/transducers";
import { SYSTEM } from "@thi.ng/random";
import { slider, checkbox } from "./controllers";
import { download } from "./download";

Expand All @@ -46,7 +47,7 @@ const radius = (width / 2) * 0.8;
const center = [width / 2, height / 2];

const rndInt = (min: number, max: number) =>
Math.trunc(min + Math.random() * (max - min));
SYSTEM.minmax(min, max) | 0;

const startingCircles: Array<[number, number, boolean]> = [
[radius / 1, rndInt(4, 20), true],
Expand Down Expand Up @@ -117,16 +118,12 @@ function computeVoronoi(state: AppState) {
const voronoi = [
rect([width, height], { fill: "black" }),

...cells.map((cell) => {
const cell0 = new Polygon(simplify(cell, 0.01, true));

const cubics = asCubic(cell0, opts);
const paths = pathFromCubics(cubics);

return group({}, [
withAttribs(paths, { fill: "white", "stroke-width": 1 })
]);
}),
group(
{ fill: "white", "stroke-width": 1 },
cells.map((cell) =>
pathFromCubics(asCubic(polygon(simplify(cell, 0.01, true)), opts)
))
),
points(doSave ? [] : startPoints, {
size: 4,
shape: "circle",
Expand All @@ -135,7 +132,7 @@ function computeVoronoi(state: AppState) {
];

if (doSave) {
const svg = convertTree(
const svg = asSvg(
svgDoc(
{
width,
Expand All @@ -146,8 +143,7 @@ function computeVoronoi(state: AppState) {
...voronoi
)
);
const finalSvg = serialize(svg);
download(`${new Date().getTime()}-voronoi.svg`, finalSvg);
download(`${new Date().getTime()}-voronoi.svg`, svg);
}

return voronoi;
Expand Down
15 changes: 3 additions & 12 deletions examples/rotating-voronoi/src/stream-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,19 @@ import {
fromDOMEvent,
sync
} from "@thi.ng/rstream";
import { Transducer, Reducer, map, comp, mapcat } from "@thi.ng/transducers";

function increment(initialValue: number = 0): Transducer<number, number> {
let frame = initialValue;
return (rfn: Reducer<number, number>) => [
() => rfn[0](),
(acc) => rfn[1](acc),
(acc) => rfn[2](acc, frame++)
];
}
import { map, mapcat, scan, add } from "@thi.ng/transducers";

export const keyStream = fromDOMEvent(document, "keyup");
export const keyStreamConditional = keyStream.transform(
comp(map((x) => [x.key, null]), mapcat((x) => x))
mapcat((x) => [x.key, null])
);

export const scaleStream = stream<number>();
export const animationStream = stream<boolean>();
export const frameStream = fromRAF();
export const frameStreamConditional = frameStream
.subscribe(sidechainToggle<number, boolean>(animationStream))
.transform(increment());
.transform(map(() => 1), scan(add()));

export type AppState = {
scaleValue: number;
Expand Down

0 comments on commit 4445fb9

Please sign in to comment.