Last active
November 1, 2018 00:09
D3 Zoom V5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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