Compiling reactive SVG #456
-
Hi, Do reactive variables need to be within the $compile function and do they even need to be directly within the arguments or can they be living outside of it? import * as html from "@thi.ng/hiccup-html";
import { convertTree } from "@thi.ng/hiccup-svg";
import { $compile } from "@thi.ng/rdom";
import * as geom from "@thi.ng/geom";
import * as vector from "@thi.ng/vectors";
// delaunator is an external package, it is faster than @thi.ng/geom-voronoi
import Delaunator from 'delaunator'
import { DVMesh } from "@thi.ng/geom-voronoi";
import { partition, repeatedly2d } from "@thi.ng/transducers";
import { reactive } from "@thi.ng/rstream";
const COUNT = 10
const pts = reactive([...repeatedly2d((x,y) => [x*500/COUNT-250,y*500/COUNT-250], COUNT,COUNT), [10,5],[10,75],[30,18],[40,18],[40,100]])
$compile(
html.div({},
pts.map((x)=>{
const coords = x.flatMap((pt)=>pt)
const delaunay = new Delaunator(coords)
const points = vector.Vec2.mapBuffer(delaunay.coords, delaunay.coords.length/2)
return convertTree(
geom.svgDoc(
{width: "500px", height: "500px", viewBox: `-250 -250 500 500`,
onmousedown: (e)=>{
// can i also use pts.transform here?
pts.next(pts.deref()!.concat([Math.random()*100, Math.random()*100]))
}
},
geom.group({ stroke: "red", 'stroke-width':0.1 }, [...partition(3, delaunay.triangles)].map((t) => geom.polygon(t.map((index)=>points[index]))))
))
}) //.deref() shows the svg, without it I only see svg,[object, Object]
,
html.h1({}, "Hello world! 👋")
)
).mount(document.body); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @dennemark — that's quite a lot of questions in one go 😉 - but I've just added some more notes which are hopefully helping... New commit: 1d70c04 To copy from those docs:
The issue: If you're just placing a reactive value as child inside the component tree, like you did (i.e. not using any of these wrappers), then that value will only be auto-wrapped using $sub(), which only ever treats it as text and/or does a dumb stringification of that value (each time it changes) and therefore produces only garbage here... Both the docs for $compile() and $sub are mentioning that (i.e. $sub's using only $text() aka setting So the tl;dr version/solution for you is: Use $replace() to wrap the entire import { $replace } from "@thi.ng/rdom";
$compile(
html.div({},
$replace(pts.map((x)=>{ ... }))
)
).mount(document.body); Hope that makes more sense now! Also let me know if not... :) |
Beta Was this translation helpful? Give feedback.
Hi @dennemark — that's quite a lot of questions in one go 😉 - but I've just added some more notes which are hopefully helping...
New commit: 1d70c04
To copy from those docs:
The issue: If you're just placing a reactive value as child inside the component tree, like you did (i.e. not using any of these wrappers), then that value will on…