Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom in SVG with currentScale

Tags:

svg

zooming

I've a web page in which there is a SVG box <svg id='graph'>...</svg>.

I would like to give to the user the possibility to zoom in the SVG box.

I use for this purpose document.getElementById("graph").currentScale*=2; if I want to double the size of the box, for example.

The problem is that all the window is resized, even the HTML elements outside the box.

Do you know the origin of this problem please ?

like image 433
Arnaud Avatar asked Feb 16 '23 09:02

Arnaud


2 Answers

In order to apply zoom to only svg , use this...

<svg>
<g transform="scale(2)">

</g>
</svg>
like image 55
RashFlash Avatar answered Feb 23 '23 20:02

RashFlash


This may be off the mark without knowing which browser you're using, but you may not be able to implement currentScale if you're using anything other than Internet Explorer or Microsoft Edge. You can test this for yourself by trying this JSBin in different browsers and see where it scales.

Diving into this deeper, a Google search of "SVG currentScale" found these bug threads from Google and Mozilla discussing similar issues. The Mozilla thread discusses it was a choice made specifically for consistency's sake. SVG's currentScale only applies to container elements and not to inner elements. This could interfere with functions like zoomAndPan used on inner SVG elements. Additionally, better cross-browser solutions are already in place such as scale() via the transform property.

like image 44
Charles D. Villard Avatar answered Feb 23 '23 18:02

Charles D. Villard