{
const text = await FileAttachment("desk_playground_3.svg").text();
const document = new DOMParser().parseFromString(text, "image/svg+xml");
const map_svg = d3.select(document.documentElement).remove();
const theMap = map_svg.node();
const svg = d3
.create("svg")
.attr("width", theMap.getAttribute("width"))
.attr("height", theMap.getAttribute("height"))
.attr("id", theMap.getAttribute("id"))
.attr("class", "modified")
.attr("viewBox", theMap.getAttribute("viewBox"))
.style("cursor", "crosshair");
try {
const deskSymbol = d3.select("svg.modified symbol#desk");
const oldVB = decompose_viewbox(deskSymbol.attr("viewBox"));
const desk_width = oldVB.width;
const desk_height = oldVB.height;
if(true) {
const desks = d3.selectAll("svg.modified use");
desks.each((_, i, collection) => {
const el = d3.select(collection[i]);
if(true) {
const initial_transform = extract_transform(el.attr("transform"));
const newX = initial_transform.x + desk_width * 0.5;
const newY = initial_transform.y + desk_height * 0.5;
const newTranslate = `translate(${newX} ${newY})`;
el.attr("transform", newTranslate);
el.classed("translated", true);
console.log(
el.attr("xlink:href"),
el.classed("translated"),
initial_transform,
newTranslate,
el.attr("transform")
)
}
});
}
// modify viewbox of symbol for the desk
if(false){
if (deskSymbol.classed("viewbox_moved")) {
console.log("viewbox already moved, not doing it again");
} else {
deskSymbol
.attr("viewBox", translate_viewbox(deskSymbol.attr("viewBox"), 0, 0, 3))
.attr("width", null)
.attr("height", null)
.classed("viewbox_moved", true);
console.log("viewBox", deskSymbol.attr("viewBox"));
}
}
// Move the contents of the symbol definition back into the right place again.
if(false) {
deskSymbol.selectChildren().each((_, i, collection) => {
const el = d3.select(collection[i]);
const vb = decompose_viewbox(deskSymbol.attr("viewBox"));
const x_trans = vb.width / 2;
const y_trans = vb.height / 2;
const trans = `translate(${-x_trans} ${-y_trans})`;
el.attr("transform", trans);
console.log(el, "trans", trans);
});
}
// and by here, everything should look the same
} catch (error) {
console.error(error, "probably the first time through?");
/* I don't know what's going on here. The svg is modified every time this cell
runs, rather than reloading it, and running the transform on a fresh version.
I assume it's a caching thing. There's a correct way to either force a fresh
load, or to do a deep copy, or something?
*/
}
//The rest of this is handling the zooming, and some decorations
// x and y are scales that project the data space to the ‘unzoomed’ pixel referential
const x = d3.scaleLinear([0, 1], [0, 1]);
const y = d3.scaleLinear([0, 1], [0, 1]);
const g = svg.append("g");
g.node().appendChild(theMap);
console.log("data", data);
const origin_points = g
.selectAll("circle")
.data(data)
.join("circle")
.attr("cx", (d) => Math.round(x(d.x)))
.attr("cy", (d) => Math.round(y(d.y)))
.attr("r", 1)
.attr("style", "fill: none; stroke: blue; stroke-width: 1;");
let transform;
const zoom = d3.zoom().on("zoom", (e) => {
g.attr("transform", (transform = e.transform));
g.style("stroke-width", 3 / Math.sqrt(transform.k));
});
return svg
.call(zoom)
.call(zoom.transform, d3.zoomIdentity)
.node();
}