Built with blockbuilder.org
forked from Igathi's block: MB zoom circles in and out
forked from Igathi's block: MB zoom circles in and out
xxxxxxxxxx
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
.feature {
fill: #ccc;
cursor: pointer;
}
.selected {
fill: #c1c1c1;
}
</style>
<body>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
// zoom in with image positioned at the center
// irrespective of their location and size
// Aug 14, 2019
var active = d3.select(null);
var objh = 20;
var width = window.innerWidth;
var height = window.innerHeight;
var cenx = width/2;
var ceny = height/2;
var jsonCircles = [
{ "x": cenx-10, "y": ceny-40, "radius": 10, "color" : "limegreen" },
{ "x": cenx-30, "y": ceny, "radius": 20, "color" : "purple"},
{ "x": cenx+30, "y": ceny, "radius": 30, "color" : "orangered"},
{ "x": cenx+70, "y": ceny+30, "radius": 5, "color" : "skyblue"}
];
var zoom = d3.zoom()
.on("zoom", zoomed);
var initialTransform = d3.zoomIdentity
.translate(0,0)
.scale(1.0);
var svg = d3.select("body").append("svg")
svg.attr("width", width)
.attr("height", height)
.on("click", stopped, true);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height)
.style("fill", "white")
.style("stroke", "black")
.on("click", reset);
var g = svg.append("g");
svg.call(zoom) // delete this line to disable free zooming
.call(zoom.transform, initialTransform);
var circles = g.selectAll("circle")
.data(jsonCircles)
.enter()
.append("circle")
.attr("cx", function (d) { return d.x; })
.attr("cy", function (d) { return d.y; })
.attr("r", function (d) { return d.radius;})
.style("fill", function(d) { return d.color;})
.attr("class", "feature")
.on("click", clicked);;
function clicked(d) {
// if (active.node() === this) return reset();
if (active.node() === this) {
d3.select(this).style("fill", d.color);
return reset();
}
active.classed("active", false);
active = d3.select(this).classed("active", true);
dr = d.radius
dx = d.radius
dy = d.radius
x = d.x
y = d.y
// active.attr("class", "selected")
// scale = Math.max(1, Math.min(height/objh/2.05, 0.9 / Math.max(dx / width, dy / height)))
scale = height/dr/2.05 // Same zoom level to all objects
translate = [width / 2 - scale * x, height / 2 - scale * y];
// console.log("width");
var transform = d3.zoomIdentity
.translate(translate[0], translate[1])
.scale(scale);
svg.transition()
.duration(1000)
.call(zoom.transform, transform);
d3.select(this).style("fill", "black");
}
function reset() {
// d3.select(this).style("fill", "blue");
active.classed("active", false);
active = d3.select(null);
svg.transition()
.duration(750)
.call(zoom.transform, initialTransform);
}
function zoomed() {
var transform = d3.event.transform;
g.style("stroke-width", 1.5 / transform.k + "px");
g.attr("transform", transform);
}
// If the drag behavior prevents the default click,
// also stop propagation so we don’t click-to-zoom.
function stopped() {
if (d3.event.defaultPrevented) d3.event.stopPropagation();
}
</script>
https://d3js.org/d3.v4.min.js