13

I am pretty new to SVG and I am stuck on how to scale a shape inside an SVG element.

Can someone explain to me why the following doesn't work? I would like to scale up the circle by 50%.

Here is my jsfiddle with the example which doesn't scale the circle?

https://jsfiddle.net/q2buo2x7/

<svg height="150" width="150" id="outer">
    <circle id="inner" cx="25" cy="20" r="20" fill="orange"/>
</svg>

<script>
    function zoom() {
        var outer = document.getElementById('outer');
        outer.setAttribute("currentScale", 1.5);
    }
    zoom();
</script>
2
  • Please see this thorough answer: stackoverflow.com/questions/19484707/…
    – kaaposc
    Commented Jun 22, 2015 at 20:12
  • You might want to accept one of the answers which helped you the most. This helps the community better to focus on question having unaccepted answers .
    – RBT
    Commented Jun 28, 2017 at 2:27

3 Answers 3

13
    function zoomIn1() {
        var outer = document.getElementById("outer");
        outer.setAttribute("currentScale", 1.5);
    }
    function zoomIn2() {
        var outer = document.getElementById("outer");
        outer.currentScale = 1.5;
    }
    function zoomIn3() { // Correct way
        var inner = document.getElementById("inner");
        inner.setAttribute("transform", "scale(1.5)");
    }

In IE zoomIn1, zoomIn2 and zoomIn3 will work.

In Chrome zoomIn1 will do nothing, zoomIn2 will zoom the whole page and zoomIn3 will do what you want. So for your Microsoft Exam your answer is correct, but in real live: use the transform attribute as stated in zoomIn3.

11

You can not scale the top-level svg object. To do so, you will need to change the viewBox.

Where did you get the idea to use currentScale as an attribute?

To scale the circle you need to change its transform attribute:

<svg height="150" width="150" id="outer">
    <circle id="inner" cx="25" cy="20" r="20" fill="orange"/>
</svg>

<script>
    function zoom() {
        var circle = document.getElementById('inner');
        circle.setAttribute("transform", "scale(1.5)");
    }
    zoom();
</script>

Read more: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform

7
  • 2
    I have recently failed my html5 CSS and JavaScript exam and this was a question I wasn't sure on. I can't remember the exacts but I remember currentScale was on one of the answers. I guess that wasn't the correct answer. Commented Jun 22, 2015 at 20:10
  • 2
    I remember one which had the inner.r = inner.r * 1.5, I have tried this and this also didn't work so I was a bit stumped. Commented Jun 22, 2015 at 20:12
  • samples.msdn.microsoft.com/workshop/samples/svg/zoomAndPan/… try this example with IE and with chrome, you will see that it only works with IE. This uses the currentScale on SVG level.
    – Tito
    Commented Oct 21, 2016 at 12:27
  • 2
    This is sad that this is IE specific.. Anyway, the question is here: briefmenow.org/microsoft/…
    – Dalibor
    Commented Mar 21, 2018 at 21:36
  • What if the element already has a transform attribute set? Does this overwrite?
    – Ulf Aslak
    Commented Mar 17, 2021 at 7:50
1

It depends what you mean by "scale up the circle". You can apply a transform as per Brennan's answer. That will scale up everything about the circle, such as its stroke size, fill, etc.

Or you can just increase the radius of the circle, if that's all you need:

function zoom() {
    var inner = document.getElementById('inner');
    inner.r.baseVal.value *= 1.5;
}
zoom();
<svg height="150" width="150" id="outer">
    <circle id="inner" cx="25" cy="20" r="20" fill="orange"/>
</svg>

Not the answer you're looking for? Browse other questions tagged or ask your own question.