Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS translate SVG group with percentages

Tags:

css

svg

Right now, I seem to be able to apply translate and translate3d, on a group inside a SVG, using px but not using percentages. I'm wondering if it's at all possible to use %? The reason I need them, is because I want to animate the content of several SVG's via CSS. (Using a JS library such as snap or svgjs is giving me a whole bunch of other headaches such as scaling issues in Chrome, so I'd rather avoid using those.)

I created a fiddle to illustrate the behavior: http://jsfiddle.net/f4MU3/4/

CSS:

.cloud-svg .group {
    -webkit-transform:translate3d(50%,50%,0);
    -moz-transform:translate3d(50%,50%,0);
    transform:translate3d(50%,50%,0);
}

HTML:

<div class="container">
<div class="cloud">
    <svg class="cloud-svg" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" x="0px" y="0px" viewBox="0 0 107.4 50.6" enable-background="new 0 0 107.4 50.6" xml:space="preserve" preserveAspectRatio="xMinYMin meet" width="100%" height="100%">
        <svg x="0%" y="0%" viewBox="0 0 107.4 50.6">
            <g class="group">
                <path d="M74.6 20.6c3.4-5.3 8.5-7.2 14.1-7.9c4.3-0.6 8.7-0.3 12.8 1.7c1.9 0.9 3.2 2.2 3.8 4.1 c0.8 3.1 1.8 6.2 1.9 9.5c0.1 5.8-0.6 11.5 0.2 17.3c0.4 2.8-0.5 3.5-3.3 3.5c-5.2-0.1-10.3-0.5-15.5-0.8c-0.6 0-1.3-0.1-1.9-0.3 c-2-0.9-2.7 3.8-5.2 0.9c-1.7-1.9-4.3-1-6.5-1c-2.1 0.1-4.7 0.2-6.1-0.4c-6.5-3-12.8-0.4-19.1-0.4c-3.9 0-7.7 0.1-11.5-0.9 c-2.9-0.7-5.8-0.2-8.6 0.5c-8.3 2.3-16.7 3.3-25.3 4.1c-2.6 0.2-3.9-0.8-4.1-3.5c-0.2-2.6-0.1-5.2 0.6-7.7 C3.2 30.2 7.6 21.9 14 14.9c4.2-4.6 10.2-5.8 16.3-6.1c1.9-0.1 3.1 1.6 4.6 2.5c1.5 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C92.9 43.4 94 43.5 95.1 43.5z"></path>
            </g>
        </svg>
    </svg>
</div>

(this is somewhat related to this question but that can't be used for animation.)

Strangely enough, I've sometimes seen it work with percentages in Chrome and Safari but not in Firefox. Can't seem to be able to recreate it though.

like image 906
Rembrand Avatar asked May 13 '14 21:05

Rembrand


1 Answers

As for it sometimes working in Chrome/Safari - I think this is due to a bug in the Webkit/Blink which has been plaguing developers for a while. For some reason, the translation isn't being rendered initially, but if you tweak the values in the developer panel, for example, you can see that the cloud is translated as you would expect.

You can also force it to render by changing the style dynamically through JavaScript after a trivial timeout (0ms). For example, if you remove your CSS rule altogether and do this instead, it seems to work:

Demo link

function fix() {
    var e = document.querySelector(".group");
    e.style.transform = "translate3d(50%,50%,0)";
}
setTimeout(fix, 0)

This isn't an ideal workaround, but other solutions didn't seem to work for me. Unfortunately it also doesn't help with Firefox, but perhaps it will help in the direction of a solution.

like image 172
cjol Avatar answered Sep 30 '22 12:09

cjol