Reputation: 107
I've got an SVG that I've animated. It works fine (or at least as well as it needs to for now), however when I look at it in Firefox, the 2nd and 3rd rectangles of the SVG are squashed together.
No idea why, as it's fine in Chrome/Safari. I think it may be something to do with the transform: scaleY
, but not 100% sure.
If anyone can tell me why/how to fix, that'd be great.
Sidenote: If anyone knows how to sort the smoothness when it stops/starts, awesome, but that may be another question.
Codepen: https://codepen.io/Will5592/pen/WPQQKB
const svg = document.querySelectorAll('rect');
svg.forEach(item => {
item.addEventListener('click', (e) => {
svg.forEach(i => i.classList.toggle('animation-on'))
})
})
body {
height: 80vh;
display: flex;
justify-content: center;
align-items: center;
}
svg {
width: 25vw;
height: 25vh;
cursor: pointer;
}
.animation-on {
animation: updown 0.75s infinite linear;
}
rect {
transform-origin: 0 50%;
transform: scaleY(0.75);
}
rect:nth-child(1) {
animation-delay: 0.05s;
}
rect:nth-child(2) {
animation-delay: 0.075s;
animation-duration: 0.65s;
}
rect:nth-child(3) {
animation-delay: 0.10s;
animation-duration: 0.75s;
}
rect:nth-child(4) {
animation-delay: 0.125s;
animation-duration: 0.75s;
}
rect:nth-child(5) {
animation-delay: 0.15s;
animation-duration: 0.85s;
}
@keyframes updown {
0% {
transform: scaleY(0.5);
}
50% {
transform: scaleY(1);
}
100% {
transform: scaleY(0.5);
}
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 14 17.5">
<g id="Layer_2" data-name="Layer 2">
<g id="Layer_1-2" data-name="Layer 1">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:purple;stop-opacity:1" />
<stop offset="100%" style="stop-color:indigo;stop-opacity:1" />
</linearGradient>
</defs>
<rect fill="url(#grad1)" rx="2px" ry="1px" x="2.86" y="4" width="2.57" height="9.5"/>
<rect fill="url(#grad1)" rx="2px" ry="1px"x="8.57" y="1.85" width="2.57" height="13.81"/>
<rect fill="url(#grad1)" rx="2px" ry="1px"x="11.43" y="5.18" width="2.57" height="7.14"/>
<rect fill="url(#grad1)" rx="2px" ry="1px"y="6.13" width="2.57" height="5.24"/>
<rect fill="url(#grad1)" rx="2px" ry="1px"x="5.71" width="2.57" height="17.5"/>
</g>
</g>
</svg>
Upvotes: 0
Views: 42
Reputation: 101820
As mentioned in my comment above. Using whole numbers for the x
coordinate seems to be a workaround for this issue.
In the example below, I've multiplied all coordinate values by 10 and rounded off the x
coordinate.
const svg = document.querySelectorAll('rect');
svg.forEach(item => {
item.addEventListener('click', (e) => {
svg.forEach(i => i.classList.toggle('animation-on'))
})
})
body {
height: 80vh;
display: flex;
justify-content: center;
align-items: center;
}
svg {
width: 25vw;
height: 25vh;
cursor: pointer;
}
.animation-on {
animation: updown 0.75s infinite linear;
}
rect {
transform-origin: 0 50%;
transform: scaleY(0.75);
}
rect:nth-child(1) {
animation-delay: 0.05s;
}
rect:nth-child(2) {
animation-delay: 0.075s;
animation-duration: 0.65s;
}
rect:nth-child(3) {
animation-delay: 0.10s;
animation-duration: 0.75s;
}
rect:nth-child(4) {
animation-delay: 0.125s;
animation-duration: 0.75s;
}
rect:nth-child(5) {
animation-delay: 0.15s;
animation-duration: 0.85s;
}
@keyframes updown {
0% {
transform: scaleY(0.5);
}
50% {
transform: scaleY(1);
}
100% {
transform: scaleY(0.5);
}
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 140 175">
<g id="Layer_2" data-name="Layer 2">
<g id="Layer_1-2" data-name="Layer 1">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="100%">
<stop offset="0%" style="stop-color:purple;stop-opacity:1" />
<stop offset="100%" style="stop-color:indigo;stop-opacity:1" />
</linearGradient>
</defs>
<rect fill="url(#grad1)" rx="20px" ry="1px" x="29" y="40" width="25.7" height="95"/>
<rect fill="url(#grad1)" rx="20px" ry="1px" x="86" y="18.5" width="25.7" height="138.1"/>
<rect fill="url(#grad1)" rx="20px" ry="1px" x="114" y="51.8" width="25.7" height="71.4"/>
<rect fill="url(#grad1)" rx="20px" ry="1px" y="61.3" width="25.7" height="52.4"/>
<rect fill="url(#grad1)" rx="20px" ry="1px" x="57" width="25.7" height="175"/>
</g>
</g>
</svg>
Upvotes: 1