Skip to content

Instantly share code, notes, and snippets.

@wout
Last active July 29, 2021 18:04
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wout/5188265 to your computer and use it in GitHub Desktop.
Save wout/5188265 to your computer and use it in GitHub Desktop.
Zoom in and out with svg viewbox for http://svgjs.dev
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>svg.js</title>
<style type="text/css" media="screen">
#buttons {
position: absolute;
right: 0;
top: 0;
}
</style>
</head>
<body>
<div id="canvas"></div>
<div id="buttons">
<input type="button" name="in" value="Zoom in" id="in" onclick="zoomIn()">
<input type="button" name="out" value="Zoom out" id="out" onclick="zoomOut()">
</div>
<script src="https://raw.github.com/svgdotjs/svg.js/master/dist/svg.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var step = 10
, min = 100
, max = 600
var draw = SVG('canvas').size(max, max)
draw.image('http://svgjs.dev/images/shade.jpg').size(max, max)
function zoomIn() {
var box = draw.viewbox()
if (box.width > min)
draw.viewbox(box.x, box.y, box.width - step, box.height / box.width * (box.width - step))
}
function zoomOut() {
var box = draw.viewbox()
if (box.width < max)
draw.viewbox(box.x, box.y, box.width + step, box.height / box.width * (box.width + step))
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment