Detect browser support for CSS-animated SVG
Asked Answered
L

4

21

I am playing around with CSS-animated SVG elements and came across the problem that even though all technologies, which are used, are supported by some browsers the combination is not, i.e. CSS-animated DIVs work but SVG elements don't. I am wondering if there is a way to detect if a browser is capable of animating SVG elements using CSS.

Here is a jsFiddle with an example. It works in the latest versions of Chrome, Firefox and Safari. But when opening it with e.g. Firefox 5 only the div rotates while the rect doesn't.

Landowner answered 22/7, 2015 at 15:47 Comment(12)
Have you tried to investigate Modernizr for that purpose?Storage
I haven't found any test that checks whether CSS animations are working with SVGs.Landowner
@Storage I just looked as well and didn't see any way to detect if CSS animations works with SVG either. This is good question. F Lekschas, did you ever figure this out?Strontia
@Strontia Unfortunately I haven't found anything.Landowner
Too bad. Going to add a bounty to the question for the fun of itStrontia
There is an issue on the Modernizr repo.Durgy
it's probably more an issue about supporting transform property ;Quiteris
@Quiteris You're right, see jsfiddle.net/x4go2uaL in IE11 and connect.microsoft.com/IE/feedback/details/811744/…. Any interest in editing the question a little F Lekschas?Strontia
I think the question is pretty clear. Also, your example doesn't work in FF5 even though FF5 is able to animate a DIV using the same piece of CSS. jsfiddle.net/v1oh3d99/2 So it's not only an issue about transform.Landowner
Good point. Seems like it's a multi-sided issue thenStrontia
does animating through <animateTransform> work on FF5 (it should) ?Quiteris
I belive the process to check support would be pretty complicated and I think most modern browsers support svg animations, so maybe you could just do a browser version check and return the svg animation or the ugly div one.Truncate
O
1

You can add an event listener to check for the completion of an animation iteration, and within the corresponding event handler set a flag like supportsSVGKeyFramedAnimatedProps = true (if the iteration never completes then it is not animating).

elem.addEventListener('animationiteration', eventHandler, false)

This would allow you to 'fall forward' to your SVG animation, instead of providing a fallback.

Ostwald answered 31/8, 2016 at 10:20 Comment(1)
Not sure what you mean with "fall forward" as I would fallback to SMIL probably but the idea is neat! I would probably use animationstart though as that should only trigger once.Landowner
T
1

I am wondering if there is a way to detect if a browser is capable of animating SVG elements using CSS

Simple Answer: Yes you can as stated by @jhpratt.

You can detect if a browser supports CSS-Functionality with only CSS. The @supports CSS at-rule lets you specify declarations that depend on a browser's support for one or more specific CSS features. This is called a feature query.

Example:

@supports (display: flex) {
  div {
    display: flex;
  }
}
@supports not (display: flex) {
  div {
    float: right;
  }
}

MDN Link: https://developer.mozilla.org/de/docs/Web/CSS/@supports

Long Answer:

You will always have some cross-browser issues. The problem you have encountered is bothering every Webdeveloper. Still there are ways to get around with this Browser-Support-Problem:

1. You can check "can I use" for compatibility:

Link: http://caniuse.com/ It is recommend to look up any functionality which is questionable like animations.

2. Use an autoprefixer in your workflow:

With the help of an autoprefixer you don't have to worry most of the time about using CSS with a prefix like -moz-, -webkit-, etc. This tiny helper will do the trick for you, you can even tell some autoprefixers which browsers you want to support.

3. User 3rd - Party libraries:

There are many libraries out there which you can use to detect the browser and version. If you want to be sure that your animation is secure to use, you can simply use the provided animation from the libraries and of course look the compatibility up before on their respective websites.

Some Big Names:

there are many more, jsut search the world wide web.

4. Use CSS Hacks to detect specific Browsers:

It is possible to use so called CSS-Hacks. They are specific CSS calls, which only apply on certain browsers.

Some examples:

Internet Explorer/Edge 8 only: @media \0screen {}

firefox ≥ 3.6 only: @media screen and (-moz-images-in-menus:0) {}

Opera ≤ 9.27 AND Safari 2: html:first-child .selector {}

You can look up more Browserhacks here: http://browserhacks.com/

Conclusion:

It is possible to detect specific browsers, but it is not possible to detect if the brwoser is supporting the given feature with only CSS. That is why you will always have some hard times with browser support.

Hope this helps. Regards

Toodleoo answered 31/5, 2016 at 12:17 Comment(3)
Note that you can absolutely check if a feature is supported in CSS only. It's the @supports rule, and is widely supported.Deafmute
Those are all great tips in general but do you have a specific example where any of this helps to detect CSS animation support for SVG elements?Landowner
@FLekschas do you want to create a kind of a test to automate your check? Or do you just want to be sure that an animation will work and if not have an alternative? Whatever it is you can simply check it here: developer.mozilla.org/en-US/docs/Web/CSS/@supports#ExamplesToodleoo
O
1

You can add an event listener to check for the completion of an animation iteration, and within the corresponding event handler set a flag like supportsSVGKeyFramedAnimatedProps = true (if the iteration never completes then it is not animating).

elem.addEventListener('animationiteration', eventHandler, false)

This would allow you to 'fall forward' to your SVG animation, instead of providing a fallback.

Ostwald answered 31/8, 2016 at 10:20 Comment(1)
Not sure what you mean with "fall forward" as I would fallback to SMIL probably but the idea is neat! I would probably use animationstart though as that should only trigger once.Landowner
U
0

I believe that the SMIL animations detections in modernizr should do it. https://modernizr.com/download?smil-setclasses

I'm using it in a pretty involved set of css/SVG chart animations. Just wrap a fallback in the following tag:

.no-smil{    }

http://codepen.io/msbtterswrth/pen/greWzy

Upstart answered 5/5, 2016 at 19:45 Comment(2)
This question is about CSS-based SVG animations not SMIL-based animations.Landowner
i know, but i've had success in using that test for css animations as well. I had the same issue, where i could not get my svgs to transform in certain browsers and this test helped me to successfully detect those and code around them.Upstart
G
-1

I haven't done exactly what you're looking for, but something similar (providing an animated clip-path as defined by SVG when the browser supports it and falling back when it doesn't). You can use media queries looking for pixel ratios to determine if a broswer is moz or webkit and provide the fallback animation outside the media query and provide the preferred animation in media queries that indicate a browser that will support it.

//fallback animation here

@media (-webkit-min-device-pixel-ratio: 0) {
  // webkit animation here 
}

As for older versions of Firefox? I don't know how to do that in CSS, but I'm not sure going back more than a few versions of Firefox or Chrome is a common use case.

Gasper answered 21/9, 2015 at 19:25 Comment(1)
The point of my question isn't really to figure out how to support one specific browser such as FF5. I am curious if someone knows a way to generally check for CSS-based SVG animation support across browser.Landowner

© 2022 - 2024 — McMap. All rights reserved.