Related
I have the following script that plays my SVGs when they scroll into view (i have several of these SVGs on the page). This works perfectly but only in Firefox! Is this the correct way to do it or is there a better code that will work on other browsers? Ideally, if it can't work on IE, and i am expecting this, then the SVG would just appear like an image and not animated. But i would expect it to work in all other browsers!
<div id="arrow-2id" class="arrow-2 leftarrows">
<!--START ARROW 2-->
<svg id="arrow2svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 265.009 303.044">
<script type="text/javascript">
var diva2 = document.getElementById("arrow-2id");
window.addEventListener("scroll", function() {
if (document.documentElement.scrollTop >= diva2.offsetTop - -500) {
diva2.classList.add("play");
}
}
);
</script>
<mask id="arrow2-mask1" maskUnits="userSpaceOnUse">
<path d="M272.304,11.13c-85-31.5-421.5,178.5-175,268.5"/>
</mask>
<mask id="arrow2-mask2" maskUnits="userSpaceOnUse">
<path d="M-3.696,293.63
c130.667-12.167,130.667,23.5,58.5-89.5"/>
</mask>
<path mask="url(#arrow2-mask1)" fill="#42A8FC" d="M74.443,277.599c-22.03-9.911-41.892-24.317-56.394-41.811c-32.086-38.702-16.961-81.441,14.377-119.45
c8.258-9.874,27.106-28.748,31.125-32.337C95.94,55.065,133.39,29.76,173.667,13.682c21.596-8.622,49.867-18.374,71.02-11.181
c10.227,3.479,20.322,12.379,20.322,14.696c-0.002,2.317-8.945-0.005-10.693-0.368c-5.762-1.199-13.512-0.332-19.262,0.301
c-22.932,2.51-47.227,13.269-68.383,23.897c-60.319,27.104-104.662,75.694-105.099,75.646
c-12.522,12.951-23.527,26.688-32.052,40.988c-32.137,53.904,3.738,93.021,50.723,109.687
C91.345,270.88,88.917,283.693,74.443,277.599z"/>
<path mask="url(#arrow2-mask2)" fill="#42A8FC" d="M74.443,277.599c-20.021,1.14-40.042,2.28-60.062,3.422c-16.744,0.952-18.679,22.91-2.629,21.996
c30.077-1.713,60.152-3.425,90.23-5.14c8.539-0.484,17.633-7.912,13.074-16.089c-13.158-23.591-26.316-47.181-39.471-70.771
c-6.476-11.604-30.8-2.861-23.525,10.185c8.394,15.048,19.787,31.097,28.182,46.146c5.811,8.531,5.436,9.656,5.436,9.656
L74.443,277.599z"/>
</svg>
<style>
.play #arrow2-mask1 path {
fill: none;
stroke: white;
stroke-width: 29;
stroke-dasharray: 478.362 478.362;
stroke-dashoffset: 0;
animation: brush2a 2s linear ;
animation-fill-mode: forwards;
}
.play #arrow2-mask2 path {
fill: none;
stroke: white;
stroke-width: 29;
stroke-dasharray: 203.128 203.128;
stroke-dashoffset: 0;
animation: brush2b 2s linear ;
animation-fill-mode: forwards;
}
#keyframes brush2a {
0% { stroke-dashoffset: 478.362; }
25% { stroke-dashoffset: 478.362; }
75% { stroke-dashoffset: 0; }
100% { stroke-dashoffset: 0; }
}
#keyframes brush2b {
0% { stroke-dashoffset: 203.128; }
80% { stroke-dashoffset: 203.128; }
100% { stroke-dashoffset: 0; }
}
</style>
<!--END ARROW 2-->
</div>
I have a working css animation. My graphic is SVG. I have been trying to add a button to play and pause the animation on click, but I just can't seem to get it work.
What I have managed so far is as below.
.animation1 {
--animation-delay: 0.1s;
animation: ani_keyframes 1.8s linear infinite var(--animation-delay) paused;
}
.playani {
animation-play-state: running;
}
.animation1:hover {
animation: none;
fill: #666;
}
#keyframes ani_keyframes {
...
...
}
<script type="text/javascript">
var button = document.getElementById('button-ani'),
test_ani = document.getElementByClassName('animation1');
button.onclick = function(){
test_ani.classList.toggle('playani');
}
</script>
<div id="animation1_wrapper">
<svg id="animation1" width="100%" height="100%" viewBox="0 0 418 255" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="ani_graphic">
<path class="animation1" d="M263.46 25.3801C257.63 29.2001 252.88 33.7801 249.33 37.8001H263.46V25.3801Z" fill="#000"></path>
.....
.....
.....
</g>
</svg>
</div>
<button id="button-ani">Toggle Animation Play State</button>
Since using document.getElementsByClassName('animation1') gives you HTMLCollection while ClassList applies to element so you can select the element by using indexer. and then your code works perfectly.
var button = document.getElementById('button-ani');
var test_ani = document.getElementsByClassName('animation1');
//this will give you HTML collection object so use [0] to access the element.
button.onclick = function(){
test_ani[0].classList.toggle('playani');
}
.animation1 {
--animation-delay: 0.1s;
animation: ani_keyframes 1.8s linear infinite var(--animation-delay) paused;
}
.playani {
animation-play-state: running;
}
.animation1:hover {
animation: none;
fill: #666;
}
#keyframes ani_keyframes {
from{
transform:scale(0);
}
to{
transform:scale(1.1);
}
}
<button id="button-ani">Toggle Animation Play State</button>
<div id="animation1_wrapper">
<svg id="animation1" width="100%" height="100%" viewBox="0 0 418 255" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="ani_graphic">
<path class="animation1" d="M263.46 25.3801C257.63 29.2001 252.88 33.7801 249.33 37.8001H263.46V25.3801Z" fill="#000"></path>
.....
.....
.....
</g>
</svg>
</div>
use
document.querySelector('.animation1');
instead of
document.getElementsByClassName('animation1');
I add a translate animation for the demo. Hope it works :).
var button = document.getElementById('button-ani');
var test_ani = document.querySelector('.animation1');
button.onclick = function(){
test_ani.classList.toggle('playani');
}
.animation1 {
--animation-delay: 0.1s;
animation: ani_keyframes 1.8s linear infinite var(--animation-delay) paused;
}
.playani {
animation-play-state: running;
}
.animation1:hover {
animation: none;
fill: #666;
}
#keyframes ani_keyframes {
from{
transform:translateX(0px);
}
to{
transform:translateX(-100px);
}
}
<button id="button-ani">Toggle Animation Play State</button>
<div id="animation1_wrapper">
<svg id="animation1" width="100%" height="100%" viewBox="0 0 418 255" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="ani_graphic">
<path class="animation1" d="M263.46 25.3801C257.63 29.2001 252.88 33.7801 249.33 37.8001H263.46V25.3801Z" fill="#000"></path>
.....
.....
.....
</g>
</svg>
</div>
I am looking to create an infinite (repeating) animation of a car going horizontally, with landscape (different layers, SVG) passing by.
I couldn't find how to repeat my SVG landscape layers along the X-axis so when I play the animation, it just keeps repeating.
My animation is done with CSS keyframes and translateX (not sure if it's the best solution though).
The idea here is that you wish to 'mimic' the background into repeating. I'm not sure this is the best solution, it's just one that I have used in the past and am very fond of.
First, we'll duplicate the background svg with the same properties and call it #back instead of #front.
keyframes frontScroll {
from {transform: translateX(0);}
to {transform: translateX(100%);}
}
keyframes backScroll {
from {transform: translateX(-100%);}
to {transform: translateX(0%);}
}
Next, we're setting another animation that has exactly the same properties, except it moves from -100% to 0% while the original one goes from 0% to100%`.
If we all wrap it together, the following happens:
body {
margin : 0;
overflow : hidden;
}
#front {
position : absolute;
left :0;
right:0;
bottom:0;
z-index : 9;
-webkit-animation: frontScroll 2.5s linear infinite;
animation: frontScroll 2.5s linear infinite;
}
#back {
position: absolute;
bottom: 0;
right: 0;
left: 0;
z-index: 9
-webkit-animation: backScroll 2.5s linear infinite;
animation: backScroll 2.5s linear infinite;
}
keyframes frontScroll {
from {transform: translateX(0);}
to {transform: translateX(100%);}
}
#-webkit-keyframes frontScroll {
from {transform: translateX(0);}
to {transform: translateX(100%);}
}
keyframes backScroll {
from {transform: translateX(-100%);}
to {transform: translateX(0%);}
}
#-webkit-keyframes backScroll {
from {transform: translateX(-100%);}
to {transform: translateX(0%);}
}
<div id="back">
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Calque_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1200 313.9" xml:space="preserve">
<style type="text/css">
.st0{fill:#602700;}
</style>
<g id="XMLID_171_">
<g id="XMLID_173_">
<g id="XMLID_193_">
<g id="XMLID_202_">
<path id="XMLID_203_" class="st0" d="M833.9,230.9l-25.9-3.7c0,0,0,0,0,0l-204.6,28c0,0,0,0,0,0l-160.5-3.8l-162.3,3.8
c0,0,0,0,0,0L200,240.8c0,0,0,0,0,0L0,241.5c0,0,0,0,0,0v72.4c0,0,0,0,0,0H1200c0,0,0,0,0,0l0-109.9c0,0,0,0,0,0l-200-25.2
c0,0,0,0,0,0L833.9,230.9C833.9,230.9,833.9,230.9,833.9,230.9z"/>
</g>
<path id="XMLID_200_" class="st0" d="M258.3,120.9V133c0,10.5,4.6,20,11.9,26.8c7.6,7,12.1,16.4,12.1,26.5v81c0,0,0,0,0,0h28.9
c0,0,0,0,0,0v-81c0-10.1,4.5-19.5,12.1-26.5c7.3-6.7,11.9-16.3,11.9-26.8v-32.2c0-5.8-5.3-10.4-11.5-9.5c-5,0.7-8.5,5-8.5,9.8
V133c0,6.3-3.4,11.8-8.5,14.9c0,0,0,0,0,0V79.6c0-4.8-3.6-9.2-8.5-9.8c-6.2-0.8-11.5,3.7-11.5,9.5v68.5c0,0,0,0,0,0
c-5.1-3.2-8.5-8.7-8.5-14.9V14.4c0-4.8-3.6-9.2-8.5-9.8c-6.2-0.8-11.5,3.7-11.5,9.5v83.6c0,0,0,0,0,0c-7.9-1-14-7.4-14-15.3V26.6
c0-3.2-2.5-6-5.8-6c-3.4-0.1-6.2,2.5-6.2,5.8v56.1c0,11,6.9,20.5,16.8,24.7C254.7,109.5,258.3,115,258.3,120.9z"/>
<path id="XMLID_199_" class="st0" d="M657.7,257.6h27.9c0,0,0,0,0,0v-69.3c0-8.9,4.6-17.2,12.1-22.4c10.9-7.7,18-20.2,18-34.2
l0-86.8c0-4.2-3.1-8-7.4-8.6c-5.4-0.7-10,3.3-10,8.3v87.1c0,11.2-7.5,20.8-18,24.3c0,0,0,0,0,0V8.7c0-4.2-3.1-8-7.4-8.6
c-5.4-0.7-10,3.3-10,8.3v147.5c0,0,0,0,0,0c-10.5-3.5-18-13.1-18-24.3V76.9c0-4.2-3.1-8-7.4-8.6c-5.4-0.7-10,3.3-10,8.3v55
c0,14,7.1,26.5,18,34.2c7.5,5.3,12.1,13.5,12.1,22.4L657.7,257.6C657.7,257.6,657.7,257.6,657.7,257.6z"/>
</g>
</g>
</g>
</svg>
</div>
<div id="front">
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Calque_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1200 313.9" xml:space="preserve">
<style type="text/css">
.st0{fill:#602700;}
</style>
<g id="XMLID_171_">
<g id="XMLID_173_">
<g id="XMLID_193_">
<g id="XMLID_202_">
<path id="XMLID_203_" class="st0" d="M833.9,230.9l-25.9-3.7c0,0,0,0,0,0l-204.6,28c0,0,0,0,0,0l-160.5-3.8l-162.3,3.8
c0,0,0,0,0,0L200,240.8c0,0,0,0,0,0L0,241.5c0,0,0,0,0,0v72.4c0,0,0,0,0,0H1200c0,0,0,0,0,0l0-109.9c0,0,0,0,0,0l-200-25.2
c0,0,0,0,0,0L833.9,230.9C833.9,230.9,833.9,230.9,833.9,230.9z"/>
</g>
<path id="XMLID_200_" class="st0" d="M258.3,120.9V133c0,10.5,4.6,20,11.9,26.8c7.6,7,12.1,16.4,12.1,26.5v81c0,0,0,0,0,0h28.9
c0,0,0,0,0,0v-81c0-10.1,4.5-19.5,12.1-26.5c7.3-6.7,11.9-16.3,11.9-26.8v-32.2c0-5.8-5.3-10.4-11.5-9.5c-5,0.7-8.5,5-8.5,9.8
V133c0,6.3-3.4,11.8-8.5,14.9c0,0,0,0,0,0V79.6c0-4.8-3.6-9.2-8.5-9.8c-6.2-0.8-11.5,3.7-11.5,9.5v68.5c0,0,0,0,0,0
c-5.1-3.2-8.5-8.7-8.5-14.9V14.4c0-4.8-3.6-9.2-8.5-9.8c-6.2-0.8-11.5,3.7-11.5,9.5v83.6c0,0,0,0,0,0c-7.9-1-14-7.4-14-15.3V26.6
c0-3.2-2.5-6-5.8-6c-3.4-0.1-6.2,2.5-6.2,5.8v56.1c0,11,6.9,20.5,16.8,24.7C254.7,109.5,258.3,115,258.3,120.9z"/>
<path id="XMLID_199_" class="st0" d="M657.7,257.6h27.9c0,0,0,0,0,0v-69.3c0-8.9,4.6-17.2,12.1-22.4c10.9-7.7,18-20.2,18-34.2
l0-86.8c0-4.2-3.1-8-7.4-8.6c-5.4-0.7-10,3.3-10,8.3v87.1c0,11.2-7.5,20.8-18,24.3c0,0,0,0,0,0V8.7c0-4.2-3.1-8-7.4-8.6
c-5.4-0.7-10,3.3-10,8.3v147.5c0,0,0,0,0,0c-10.5-3.5-18-13.1-18-24.3V76.9c0-4.2-3.1-8-7.4-8.6c-5.4-0.7-10,3.3-10,8.3v55
c0,14,7.1,26.5,18,34.2c7.5,5.3,12.1,13.5,12.1,22.4L657.7,257.6C657.7,257.6,657.7,257.6,657.7,257.6z"/>
</g>
</g>
</g>
</svg>
</div>
Now we can easily position a car or something on top of the land and voila, an never-ending driving car.
I am trying to animate several paths in an svg around two circles. My goal is to scale the paths at center, as if they were pulsating. I've looked at every answer on stackoverflow to figure out how to achieve this. The closest solution I have found did not wok.
Here is a jsfiddle of what I have so far. As you can see, the paths are scaling away/towards the origin. If there is no way to achieve this with CSS, is it possible to achieve this with a javascript framework like Velocity.js?
This is the SVG:
<div class="wrapper">
<svg version="1.1" viewBox="20 20 60 60" >
<g class="icon-sun">
<path class="icon-sun-beam"
d="M64.175,38.688c-0.781,0.781-2.049,0.781-2.828,0c-0.781-0.781-0.781-2.047,0-2.828l2.828-2.828c0.779-0.781,2.047-0.781,2.828,0c0.779,0.781,0.779,2.047,0,2.828L64.175,38.688z"/>
<path class="icon-sun-beam"
d="M64.175,38.688c-0.781,0.781-2.049,0.781-2.828,0c-0.781-0.781-0.781-2.047,0-2.828l2.828-2.828c0.779-0.781,2.047-0.781,2.828,0c0.779,0.781,0.779,2.047,0,2.828L64.175,38.688z"/>
<path class="icon-sun-beam"
d="M50.034,34.002c-1.105,0-2-0.896-2-2v-3.999c0-1.104,0.895-2,2-2c1.104,0,2,0.896,2,2v3.999C52.034,33.106,51.136,34.002,50.034,34.002z"/>
<path class="icon-sun-beam"
d="M35.893,38.688l-2.827-2.828c-0.781-0.781-0.781-2.047,0-2.828c0.78-0.781,2.047-0.781,2.827,0l2.827,2.828c0.781,0.781,0.781,2.047,0,2.828C37.94,39.469,36.674,39.469,35.893,38.688z"/>
<path class="icon-sun-beam"
d="M34.034,50c0,1.104-0.896,1.999-2,1.999h-4c-1.104,0-1.998-0.896-1.998-1.999s0.896-2,1.998-2h4C33.14,48,34.034,48.896,34.034,50z"/>
<path class="icon-sun-beam"
d="M35.893,61.312c0.781-0.78,2.048-0.78,2.827,0c0.781,0.78,0.781,2.047,0,2.828l-2.827,2.827c-0.78,0.781-2.047,0.781-2.827,0c-0.781-0.78-0.781-2.047,0-2.827L35.893,61.312z"/>
<path class="icon-sun-beam"
d="M50.034,65.998c1.104,0,2,0.895,2,1.999v4c0,1.104-0.896,2-2,2c-1.105,0-2-0.896-2-2v-4C48.034,66.893,48.929,65.998,50.034,65.998z"/>
<path class="icon-sun-beam"
d="M64.175,61.312l2.828,2.828c0.779,0.78,0.779,2.047,0,2.827c-0.781,0.781-2.049,0.781-2.828,0l-2.828-2.827c-0.781-0.781-0.781-2.048,0-2.828C62.126,60.531,63.392,60.531,64.175,61.312z"/>
<circle class="icon-sun-outline"
cx="50.034"
cy="50"
r="11.999"/>
<circle class="icon-sun-fill"
fill="#FFFFFF"
cx="50.034"
cy="50"
r="7.999"/>
</g>
</svg>
</div>
And here is the CSS:
.wrapper {
width: 100px;
text-align: center;
}
.icon-sun-beam {
animation-name: scale;
animation-duration: 3s;
animation-iteration-count: infinite;
transform-origin: 50px 50px;
animation-timing-function: linear;
animation-fill-mode: both;
animation-direction: alternate;
}
.icon-sun-beam:nth-child(even) {
animation-delay: 4.5s, 4.5s;
}
.icon-sun {
animation-name: rotate;
animation-iteration-count: infinite;
animation-duration: 18s;
animation-timing-function: linear;
transform-origin: 50px 50px;
}
svg {
shape-rendering: geometricPrecision;
}
#keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
#keyframes scale {
0% {
transform: scale(0.85, 0.85);
}
100% {
transform: scale(1.35, 1.35);
}
}
There is a simple solution, that is cross-browser.
Just give each of your sunbeams its own absolute transform-origin.
.wrapper {
width: 100px;
text-align: center;
}
.icon-sun-beam {
animation-name: scale;
animation-duration: 3s;
animation-iteration-count: infinite;
animation-timing-function: linear;
animation-fill-mode: both;
animation-direction: alternate;
}
.icon-sun-beam:nth-child(even) {
animation-delay: 4.5s, 4.5s;
}
.icon-sun {
animation-name: rotate;
animation-iteration-count: infinite;
animation-duration: 18s;
animation-timing-function: linear;
transform-origin: 50px 50px;
}
svg {
shape-rendering: geometricPrecision;
}
#keyframes rotate {
0% {
transform: rotate(0);
}
100% {
transform: rotate(360deg);
}
}
#keyframes scale {
0% {
transform: scale(0.85, 0.85);
}
100% {
transform: scale(1.35, 1.35);
}
}
<div class="wrapper">
<svg version="1.1" viewBox="20 20 60 60" >
<g class="icon-sun">
<path class="icon-sun-beam" style="transform-origin: 70.0px 50.0px;"
d="M72.03,51.999
h-3.998
c-1.105,0-2-0.896-2-1.999
s0.895-2,2-2h3.998
c1.104,0,2,0.896,2,2
S73.136,51.999,72.03,51.999z"/>
<path class="icon-sun-beam" style="transform-origin: 64.2px 35.9px;"
d="M64.175,38.688c-0.781,0.781-2.049,0.781-2.828,0c-0.781-0.781-0.781-2.047,0-2.828l2.828-2.828c0.779-0.781,2.047-0.781,2.828,0c0.779,0.781,0.779,2.047,0,2.828L64.175,38.688z"/>
<path class="icon-sun-beam" style="transform-origin: 50.0px 30.0px;"
d="M50.034,34.002c-1.105,0-2-0.896-2-2v-3.999c0-1.104,0.895-2,2-2c1.104,0,2,0.896,2,2v3.999C52.034,33.106,51.136,34.002,50.034,34.002z"/>
<path class="icon-sun-beam" style="transform-origin: 35.9px 35.9px;"
d="M35.893,38.688l-2.827-2.828c-0.781-0.781-0.781-2.047,0-2.828c0.78-0.781,2.047-0.781,2.827,0l2.827,2.828c0.781,0.781,0.781,2.047,0,2.828C37.94,39.469,36.674,39.469,35.893,38.688z"/>
<path class="icon-sun-beam" style="transform-origin: 30.0px 50.0px;"
d="M34.034,50c0,1.104-0.896,1.999-2,1.999h-4c-1.104,0-1.998-0.896-1.998-1.999s0.896-2,1.998-2h4C33.14,48,34.034,48.896,34.034,50z"/>
<path class="icon-sun-beam" style="transform-origin: 35.9px 64.1px;"
d="M35.893,61.312c0.781-0.78,2.048-0.78,2.827,0c0.781,0.78,0.781,2.047,0,2.828l-2.827,2.827c-0.78,0.781-2.047,0.781-2.827,0c-0.781-0.78-0.781-2.047,0-2.827L35.893,61.312z"/>
<path class="icon-sun-beam" style="transform-origin: 50.0px 70.0px;"
d="M50.034,65.998c1.104,0,2,0.895,2,1.999v4c0,1.104-0.896,2-2,2c-1.105,0-2-0.896-2-2v-4C48.034,66.893,48.929,65.998,50.034,65.998z"/>
<path class="icon-sun-beam" style="transform-origin: 64.2px 64.1px;"
d="M64.175,61.312l2.828,2.828c0.779,0.78,0.779,2.047,0,2.827c-0.781,0.781-2.049,0.781-2.828,0l-2.828-2.827c-0.781-0.781-0.781-2.048,0-2.828C62.126,60.531,63.392,60.531,64.175,61.312z"/>
<circle class="icon-sun-outline"
cx="50.034"
cy="50"
r="11.999"/>
<circle class="icon-sun-fill"
fill="#FFFFFF"
cx="50.034"
cy="50"
r="7.999"/>
</g>
</svg>
</div>
I ended up using GSAP library to animate the SVG. I chose to do this because there was no solution that would have worked without breaking the animation on either Chrome or Firefox. This is the code that I have tested which works on IE 11, Firefox and Chrome. I used Jquery and TweenMax libraries.
$(document).ready(function() {
var sun = $(".icon-sun");
var sunBeamEven = $(".icon-sun-beam:even");
var sunBeamOdd = $(".icon-sun-beam:odd");
function animateSun(){
TweenMax.to(sun, 12, {rotation:"+=360", svgOrigin:"50 50", smoothOrigin:true, repeat:-1, ease:Linear.easeNone});
TweenMax.to(sunBeamEven, 4, {scale:0.5, transformOrigin:"50% 50%", repeat:-1, yoyo:true, ease:Linear.easeNone});
TweenMax.to(sunBeamOdd, 4, {scale:0.5, transformOrigin:"50% 50%", repeat:-1, yoyo:true, ease:Linear.easeNone, delay:3});
}
animateSun();
So I am adding an SVG to my website. I am also resizing this svg to fit most of my screen, by changing a class called "background-svg" that you'll see in my code below.
Basically, If you make the SVG bigger, it becomes super laggy (like its animations are super laggy) and even other animations on the page become super laggy. Try On JSFIDDLE
If you make the SVG small, the animations are Smooth. On the JSFIDDLE link above, try making your window smaller (you'll see the animations are better), then make your window bigger (laggy animations again).
Problem Only Occurs on Chrome and Safari...No Lags on FireFox
My SVG:
<svg class="background-svg" viewBox="0 0 400 300" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<filter x="" y="-50%" width="200%" height="200%" filterUnits="objectBoundingBox" id="filter-1">
<feOffset dx="0" dy="10" in="SourceAlpha" result="shadowOffsetOuter1"></feOffset>
<feGaussianBlur stdDeviation="2" in="shadowOffsetOuter1" result="shadowBlurOuter1"></feGaussianBlur>
<feColorMatrix values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.095 0" in="shadowBlurOuter1" type="matrix" result="shadowMatrixOuter1"></feColorMatrix>
<feOffset dx="0" dy="1" in="SourceAlpha" result="shadowOffsetInner1"></feOffset>
<feGaussianBlur stdDeviation="1.5" in="shadowOffsetInner1" result="shadowBlurInner1"></feGaussianBlur>
<feComposite in="shadowBlurInner1" in2="SourceAlpha" operator="arithmetic" k2="-1" k3="1" result="shadowInnerInner1"></feComposite>
<feColorMatrix values="0 0 0 0 0.647959184 0 0 0 0 0.549016553 0 0 0 0 0.549016553 0 0 0 0.35 0" in="shadowInnerInner1" type="matrix" result="shadowMatrixInner1"></feColorMatrix>
<feMerge>
<feMergeNode in="shadowMatrixOuter1"></feMergeNode>
<feMergeNode in="SourceGraphic"></feMergeNode>
<feMergeNode in="shadowMatrixInner1"></feMergeNode>
</feMerge>
</filter>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard-1" fill="#8B65E4">
<path d="M187.785156,200 L180,232 L66,232 L58.2148437,200 L187.785156,200 Z" id="Rectangle-1" filter="url(#filter-1)"></path>
<path d="M349.760339,49.1234675 L375.905579,277.733833 L199.999999,277.733834 L43.9648432,143.710938 L349.760339,49.1234675 Z" id="Triangle-1" filter="url(#filter-1)"></path>
<path d="M399.8936,96.1889997 L29.4623426,250.140552 L0,36.4302476 L399.8936,96.1889997 Z" id="Triangle-2" filter="url(#filter-1)"></path>
</g>
<foreignObject x="8%" y="20%" width="80%" height="100%"
>
<body xmlns="http://www.w3.org/1999/xhtml">
<div xmlns="http://www.w3.org/1999/xhtml">
<h1>
Hey! <br />I'm <span>someperson</span> <span class="info">I like</span>
</h1>
</div>
</body>
</foreignObject>
</g>
</svg>
My CSS:
#import url(https://fonts.googleapis.com/css?family=Oswald|Roboto);
body {
height: 100vh;
overflow: hidden;
text-align: center;
font-family: "Roboto", sans-serif;
}
.background-svg{
position: absolute;
top: 0;
left: 20%;
width: 80%;
height: 80%;
}
h1 {
font-weight: 300;
font-size: 24px;
letter-spacing: 2px;
color: #fff;
text-align: left;
}
h1 .info {
display: block;
color: #CFBDF9;
font-size: 16px;
letter-spacing: 0px;
}
.box {
text-align: right;
padding: 0px 40px;
}
.box-item {
display: inline-block;
color: #fff;
font-size: 30px;
padding-right: 20px;
-moz-transition: all 0.3s;
-o-transition: all 0.3s;
-webkit-transition: all 0.3s;
transition: all 0.3s;
}
#Triangle-1 {
-webkit-animation: box 2.5s infinite; /* Main Anim is super laggy on chrome and safary*/
-moz-animation: box 2.5s infinite; /* Main Anim seems good on Firefox*/
}
#Triangle-2 {
-webkit-animation: box2 1s infinite; /* same as above */
-moz-animation: box2 1s infinite; /* same as above */
}
#keyframes box2 {
10% {
-moz-transform: rotate(1deg);
-ms-transform: rotate(1deg);
-webkit-transform: rotate(1deg);
transform: rotate(1deg);
}
90% {
-moz-transform: rotate(-2deg);
-ms-transform: rotate(-2deg);
-webkit-transform: rotate(-2deg);
transform: rotate(-2deg);
}
}
#keyframes box {
10% {
-moz-transform: rotate(-2deg);
-ms-transform: rotate(-2deg);
-webkit-transform: rotate(-2deg);
transform: rotate(-2deg);
}
90% {
-moz-transform: rotate(2deg);
-ms-transform: rotate(2deg);
-webkit-transform: rotate(2deg);
transform: rotate(2deg);
}
}
TL;DR: Super Laggy SVG Animations if SVG is large, but smooth animations if SVG is small.
This has to do with GPU usage when animating with pure CSS. Although most properties like transform are 'GPU injected' this isn't a guarantee. If you were to do this with JavaScript; you may see significant performance enhancements.
I do not get much 'lag' at all while running these animations.
This article is specific to a JS animation library, but clearly explains this concept as well.
How CSS animations are rendered
As far as performance changing with window scaling, it has to do with the number of pixels being altered/rendered with each animation cycle.
For example, if you're vector SVG draws a shape on a 500x500px canvas that takes up 75% of the area, you're going to be drawing alot less pixels in then if you have a 2000x2000px canvas with a shape that takes up 75% of the area.
Since this is calculated over and over again then redrawn when you're using an SVG, there can be a significant performance difference when scaling up.
Since browsers are built and render differently, FireFox is simply able to handle the redrawing of these shapes over and over better than the other browsers in this circumstance.
I've noticed two main issues:
the order of the css properties: transform and transition properties are after the vendor-specific ones, so I put it before them
the use of the 2D transformations, which don't exploit hardware acceleration, so I changed them with the equivalent 3D ones
The following snippet contains part of what I've modified, you can try it in this fiddle and give me a feedback
#Triangle-2 {
animation: box2 1s infinite;
-o-animation: box2 1s infinite;
-moz-animation: box2 1s infinite;
-webkit-animation: box2 1s infinite;
}
#keyframes box2 {
10% {
transform: rotate(1deg);
-o-animation: rotateZ(1deg);
-moz-animation: rotateZ(1deg);
-webkit-transform: rotateZ(1deg);
}
90% {
transform: rotate(-2deg);
-o-animation: rotateZ(-2deg);
-moz-animation: rotateZ(-2deg);
-webkit-transform: rotateZ(-2deg);
}
}
I've worked with html elements translations in the past, and in my case the 3D css solution outperformed the 2D and the javascript ones, mostly because of the hardware speed up without passing through the javascript interpreter.
If you want take a look here to have more details about hardware accelerations and comparison with javascript solutions.