SVG 動畫⼆二三事patw / Front-End EngineerGogolook Developers meet up╳
View Slide
SVG• Scalable Vector Graphics• Proposed by W3C in 1999. In 2001 the SVGgained its first official release.• XML• Vector vs Bitmap
SVG• Scalable• Smaller sizes (can be gzipped)• Interactive & Styleable (CSS & JavaScript)
TOOLS• Inkscape (http://inkscape.org/)• SVG-edit (http://svg-edit.googlecode.com/svn/branches/stable/editor/svg-editor.html)• Adobe Illustrator• Sketch
SVG ANIMATIONS
ANIMATED SVG VS. GIFS“By using animated SVGs instead of GIFswe were able to reduce our page size from1.6 mb to 389 kb, and reduce our pageload time from 8.75 s to 412 ms. That’s ahuge difference.”http://oak.is/thinking/animated-svgs/
SVG ANIMATIONS• 更多變化:⽐比起 CSS 3 transition,除了位移、顏⾊色變化之外,還⽀支援路徑描繪、導引線、形狀漸變等動畫...• 互動效果:點擊後播放、等待 A 結束若干秒後播放,或是針對單⼀一元件互動...
SVG ANIMATIONS• 三種實現⽅方法• CSS• SMIL(Synchronized Multimedia IntegrationLanguage)• JavaScripthttp://css-tricks.com/video-screencasts/135-three-ways-animate-svg/
SVG ANIMATIONS• CSS 使⽤用時機• 只要實現簡單的動畫效果• 只需 CSS 動畫可⽀支援的屬性就可完成的情況• 了解 CSS 動畫怎麼做
SVG ANIMATIONS• SMIL 使⽤用時機• 那些 CSS 辦不到的動畫屬性(例如形變)• 要⽤用上 SMIL 的特殊效果,例如在 A 動畫結束後播放(不需像 CSS 動畫要⾃自⾏行調整延遲時間),或是點擊後播放之類的
SMIL ANIMATIONS• 不⽤用 CSS、JavaScript 就可實現動畫效果!• 瀏覽器⽀支援度 (http://caniuse.com/#feat=svg-smil)
SMIL ANIMATIONS• • • • • http://tutorials.jenkov.com/svg/svg-animation.html
SVG ANIMATIONS• JavaScript 使⽤用時機• 就是要⽤用 JavaScript 的情況,像是動畫可能是依據接收到的 JSON 來動作的• 或是需要⼀一些數學邏輯時• 解決跨瀏覽器的問題• 需要實現龐⼤大且複雜的動畫• 太複雜也許就⽤用 Canvas 了
SVG ANIMATIONS• JavaScript Solutions• Snap.svg (http://snapsvg.io/)• GreenSock (http://codepen.io/GreenSock/pen/gpDrC)• Velocity.js (http://julian.com/research/velocity/)• SVG Morpheus (http://alexk111.github.io/SVG-Morpheus/)
DEMO
REFERENCES• http://slides.com/sarasoueidan/animating-svg-with-css-and-smil-full-version• http://css-tricks.com/video-screencasts/135-three-ways-animate-svg/• http://www.oxxostudio.tw/articles/201409/svg-21-smil-animation.html• http://tech.mozilla.com.tw/posts/5660/• http://www.zhangxinxu.com/wordpress/2014/08/so-powerful-svg-smil-animation/