html - How to keep viewBox centered when "zooming" in SVGs? -
often use viewbox attribute "zoom" svg element. zoom accomplished of course using lower width , height values viewbox attribute - x , y values tricky figure out keeping scene centered.
below snippet viewbox="0 0 100 100". ( x y width height ) starting point. scene scale , starts @ top left corner.
<head> <link rel="stylesheet" href="https://codepen.io/basement/pen/oepkxy.css"> </head> <body> <iframe src="https://s.codepen.io/basement/debug/zdpvyv/pnavylzmjrqr" id="grid" ></iframe> <div class="wrp"> <!-- svg relevant code below--> <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 100 100" width="100" height="100" class="canvas" > <defs> <style type="text/css"> circle { fill: #0ff; fill-opacity: 0.25; stroke-width: 0.25; stroke: #0ee; } </style> </defs> <circle cx="37.5" cy="50" r="25" /> <circle cx="62.5" cy="50" r="25" /> <circle cx="50" cy="71.65" r="25" /> </svg> </div> </body> ( viewable in full page. snippets responsive )
zooming in
changing width , height values 50 in viewbox zooms scene. had adjust x , y values both half viewbox dimensions: 25 keep drawing centered.
viewbox="25 25 50 50"
<head> <link rel="stylesheet" href="https://codepen.io/basement/pen/oepkxy.css"> </head> <body> <iframe src="https://s.codepen.io/basement/debug/zdpvyv/pnavylzmjrqr" id="grid" ></iframe> <div class="wrp"> <!-- svg relevant code below--> <svg xmlns="http://www.w3.org/2000/svg" viewbox="25 25 50 50" width="100" height="100" class="canvas" > <defs> <style type="text/css"> circle { fill: #0ff; fill-opacity: 0.25; stroke-width: 0.25; stroke: #0ee; } </style> </defs> <circle cx="37.5" cy="50" r="25" /> <circle cx="62.5" cy="50" r="25" /> <circle cx="50" cy="71.65" r="25" /> </svg> </div> </body> halving width , height isn't consistent
following pattern of halving width , height center scene:
viewbox="12.5 12.5 25 25" should continue zoom , stay centered. doesn't stay centered. question formula or technique svg use can figure out mathematically x , y value need center specific width , height value in viewbox?
note: i'm aware of libraries snap.svg , raphaƫl. i'm avoiding libraries in effort understand fundamentals.
one more thing: i'm asking if have width , height value in viewbox how find center coordinates of scene. not vice versa.
external stylesheets included visual cognition. relevant code question pertaining svg elements.
your calculations wrong. if width , height of viewbox smaller, x , y values need bigger.
initial viewbox: 0 0 100 100 zoom x2: 25 25 50 50 zoom x4: 37.5 37.5 25 25 to x or y values, need subtract half new width or height halfway point of last (or original) viewbox.
zoom x2: centre - newwidth/2 = (100/2) - (50/2) = 25 zoom x4: (100/2) - (25/2) = 37.5, or (25 + 50/2) - (25/2) = 37.5 another waty calculate subtract current width original width , divide two.
zoom x4: (100 - 25) / 2 = 37.5 <head> <link rel="stylesheet" href="https://codepen.io/basement/pen/oepkxy.css"> </head> <body> <iframe src="https://s.codepen.io/basement/debug/zdpvyv/pnavylzmjrqr" id="grid" ></iframe> <div class="wrp"> <!-- svg relevant code below--> <svg xmlns="http://www.w3.org/2000/svg" viewbox="37.5 37.5 25 25" width="100" height="100" class="canvas" > <defs> <style type="text/css"> circle { fill: #0ff; fill-opacity: 0.25; stroke-width: 0.25; stroke: #0ee; } </style> </defs> <circle cx="37.5" cy="50" r="25" /> <circle cx="62.5" cy="50" r="25" /> <circle cx="50" cy="71.65" r="25" /> </svg> </div> </body>
Comments
Post a Comment