|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +// create namespace |
| 6 | +if (typeof Mozilla == 'undefined') { |
| 7 | + var Mozilla = {}; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Feature detect for animating SVG line paths using CSS |
| 12 | + * Detection: |
| 13 | + * 1.) browser supports inline SVG |
| 14 | + * 2.) browser supports CSS animations |
| 15 | + * 3.) browser can animate stroke-dashoffset via a CSS keyframe animation |
| 16 | + * |
| 17 | + * Required less file: 'css/base/svg-animation-check.less' |
| 18 | + */ |
| 19 | +Mozilla.SVGAnimCheck = function() { |
| 20 | + if (!Mozilla.SVGAnimCheck.supportsInlineSVG() || !Mozilla.SVGAnimCheck.supportsCSSAnimations()) { |
| 21 | + return false; |
| 22 | + } else { |
| 23 | + return Mozilla.SVGAnimCheck.supportsCSSAnimatedPaths(); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +/** |
| 28 | + * Detect CSS animation support (only checks for webkit and unprefixed) |
| 29 | + */ |
| 30 | +Mozilla.SVGAnimCheck.supportsCSSAnimations = function() { |
| 31 | + var div = document.createElement('div'); |
| 32 | + |
| 33 | + // note we're only checking for Webkit vendor prefix as all other browsers |
| 34 | + // now support unprefixed CSS animations |
| 35 | + if (div.style.animationName !== undefined || div.style.WebkitAnimationName !== undefined) { |
| 36 | + return true; |
| 37 | + } |
| 38 | + |
| 39 | + return false; |
| 40 | +}; |
| 41 | + |
| 42 | +/** |
| 43 | + * Detect support for inline SVG elements. |
| 44 | + */ |
| 45 | +Mozilla.SVGAnimCheck.supportsInlineSVG = function() { |
| 46 | + var div = document.createElement('div'); |
| 47 | + div.innerHTML = '<svg/>'; |
| 48 | + return (div.firstChild && div.firstChild.namespaceURI) == 'http://www.w3.org/2000/svg'; |
| 49 | +}; |
| 50 | + |
| 51 | +/** |
| 52 | + * Try to animate stroke-dashoffset using CSS and then get the computed value to see if it has changed. |
| 53 | + */ |
| 54 | +Mozilla.SVGAnimCheck.supportsCSSAnimatedPaths = function() { |
| 55 | + var div = document.createElement('div'); |
| 56 | + var circle; |
| 57 | + var offset; |
| 58 | + |
| 59 | + div.id = 'svg-test'; |
| 60 | + div.innerHTML = '<svg role="presentation" xmlns="http://www.w3.org/2000/svg" version="1.1"><path id="svg-test-circle" fill-opacity="0" d="M33.665530413296274,58.589001490321166C43.94406883919239,58.59306994020939,52.274,66.92651000976564,52.274,77.205C52.274,87.486,43.939,95.821,33.658,95.821C23.377000000000002,95.821,15.042000000000002,87.48599999999999,15.042000000000002,77.205C15.041,66.923,23.376,58.589,33.658,58.589" stroke="#5d7489"></path></svg>'; |
| 61 | + document.documentElement.appendChild(div); |
| 62 | + |
| 63 | + // try/catch is maybe a little over cautious |
| 64 | + // but better to show the fallback than throw an exception |
| 65 | + try { |
| 66 | + circle = document.getElementById('svg-test-circle'); |
| 67 | + offset = window.getComputedStyle(circle).getPropertyValue('stroke-dashoffset'); |
| 68 | + |
| 69 | + div.parentNode.removeChild(div); |
| 70 | + |
| 71 | + // webkit & blink return a string with px value |
| 72 | + if (offset.replace(/\D/g, '') === '0') { |
| 73 | + return true; |
| 74 | + } |
| 75 | + return false; |
| 76 | + } catch(e) { |
| 77 | + div.parentNode.removeChild(div); |
| 78 | + return false; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
0 commit comments