Skip to content

Instantly share code, notes, and snippets.

@nicmcd
Last active June 7, 2018 07:22
Show Gist options
  • Save nicmcd/4c730aa64c04bf100a58f224b90c2ff4 to your computer and use it in GitHub Desktop.
Save nicmcd/4c730aa64c04bf100a58f224b90c2ff4 to your computer and use it in GitHub Desktop.
D3 zoom problem FIXED!
<!DOCTYPE html>
<html>
<head>
<style>
body, html {
width: 100%;
height: 100%;
margin: 0;
}
svg {
position: absolute;
top: 0;
left: 0;
}
</style>
<script type="text/javascript" src="http://d3js.org/d3.v5.js"></script>
<script type="text/javascript">
function load() {
var svg = d3.select("body")
.append("svg")
.attr("width", "100%")
.attr("height", "100%");
var zoom = d3.zoom().on("zoom", function() {
vis.attr("transform", d3.event.transform);
});
var vis = svg.append("g").attr("id", "vis");
svg.call(zoom);
d3.select("body").on("keypress", function() {
svg.transition().duration(500).call(zoom.transform, d3.zoomIdentity)
});
vis.append("rect")
.attr("x", 10)
.attr("y", 10)
.attr("width", 100)
.attr("height", 100);
}
</script>
</head>
<body onload="load()">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment