Skip to content

Instantly share code, notes, and snippets.

@UmeshMaharshi30
Last active November 1, 2018 00:09
Show Gist options
  • Save UmeshMaharshi30/9cf20f567bceb1338338567c80ffe713 to your computer and use it in GitHub Desktop.
Save UmeshMaharshi30/9cf20f567bceb1338338567c80ffe713 to your computer and use it in GitHub Desktop.
D3 Zoom V5
<!DOCTYPE html>
<meta charset="utf-8">
<style>
#picture {
width: 340px;
height: 150px;
overflow: auto;
}
.parent {
border : 1px solid #000;
}
</style>
<body>
<script src="//d3js.org/d3.v5.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<div id="picture">
<svg width="330" height="140" class="parent"><g class="image"><image xlink:href="http://www.iconpng.com/png/beautiful_flat_color/computer.png" width="330" height="140" class="pictureWrapper"></image></g></svg>
</div>
<button onclick="reset()">Reset</button>
<script>
var parent = d3.select("#picture");
var zoom = d3.zoom()
.scaleExtent([0.5, 8])
.on("zoom", zoomed);
parent.call(zoom);
function zoomed() {
parent.select("svg").attr("transform", d3.event.transform);
}
function reset() {
parent.transition()
.duration(750)
.call(zoom.transform, d3.zoomIdentity);
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment