SVG Animations pattern edge - svg

I want to create stripe animated background but for some reason can't remove vertical lines that appear on the edge of patterns.
Here is my svg:
<svg width="500" height="500">
<defs>
<linearGradient id='stripe-gradient' x1='0%' y1='0%' x2='100%' y2='100%'>
<stop offset='0%' stop-color='#AAA'></stop>
<stop offset='24.9%' stop-color='#AAA'></stop>
<stop offset='25%' stop-color='#666'></stop>
<stop offset='49.9%' stop-color='#666'></stop>
<stop offset='50%' stop-color='#AAA'></stop>
<stop offset='74.9%' stop-color='#AAA'></stop>
<stop offset='75%' stop-color='#666'></stop>
<stop offset='99.9%' stop-color='#666'></stop>
<stop offset='100%' stop-color='#AAA'></stop>
</linearGradient>
<pattern id='stripe-pattern' width='20' height='20' patternUnits='userSpaceOnUse' >
<rect x='-20' y='0' width='20' height='20' fill='url(#stripe-gradient)' stroke-width='0' stroke='none'>
<animate attributeName='x' from='-20' to='0' dur='0.5s' repeatCount='indefinite'></animate>
</rect>
<rect x='0' y='0' width='20' height='20' fill='url(#stripe-gradient)' stroke-width='0' stroke='none'>
<animate attributeName='x' from='0' to='20' dur='0.5s' repeatCount='indefinite'></animate>
</rect>
</pattern>
<mask id='stripe-mask'>
<rect x='0' y='0' width='100%' height='100%' fill='url(#stripe-pattern)' ></rect>
</mask>
</defs>
<rect class="hbar thing" x="0" y="0" width="200" fill="green" height="200" mask="url(#stripe-mask)"></rect>
</svg>

I know this post is a bit old but in case you still need to solve this issue I have made it work by doubling the stripes, removing one of the rects inside the pattern and then doubling the size of the last rect, c.f., the following code:
<svg width="500" height="500">
<defs>
<linearGradient id='stripe-gradient' x1='0%' y1='0%' x2='100%' y2='100%'>
<stop offset='0%' stop-color='#AAA'></stop>
<stop offset='12.45%' stop-color='#AAA'></stop>
<stop offset='12.5%' stop-color='#666'></stop>
<stop offset='24.45%' stop-color='#666'></stop>
<stop offset='25.5%' stop-color='#AAA'></stop>
<stop offset='37.45%' stop-color='#AAA'></stop>
<stop offset='37.5%' stop-color='#666'></stop>
<stop offset='49.9%' stop-color='#666'></stop>
<stop offset='50%' stop-color='#AAA'></stop>
<stop offset='62.45%' stop-color='#AAA'></stop>
<stop offset='62.5%' stop-color='#666'></stop>
<stop offset='74.95%' stop-color='#666'></stop>
<stop offset='75%' stop-color='#AAA'></stop>
<stop offset='87.45%' stop-color='#AAA'></stop>
<stop offset='87.5%' stop-color='#666'></stop>
<stop offset='100%' stop-color='#666'></stop>
</linearGradient>
<pattern id='stripe-pattern' width='20' height='20' patternUnits='userSpaceOnUse' >
<rect x='-20' y='0' width='40' height='40' fill='url(#stripe-gradient)' stroke-width='0' stroke='none'>
<animate attributeName='x' from='-20' to='0' dur='0.5s' repeatCount='indefinite'></animate>
</rect>
</pattern>
<mask id='stripe-mask'>
<rect x='0' y='0' width='100%' height='100%' fill='url(#stripe-pattern)'></rect>
</mask>
</defs>
<rect x="0" y="0" width="200" fill="green" height="200" mask="url(#stripe-mask)"></rect>
</svg>
I think the vertical stripes appeared as a result of some small numerical issues whenever the browser calculates the position of the two rects. Therefore, the solution is to use only one rect.

Related

How to make the animation smooth in the below SVG?

Im trying to make the below svg animation smooth. I would want to make the white gradient to move through the path indefinitely. Any suggestions would be very helpful.
<svg width="8" height="243" viewBox="0 0 8 243" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3 3L3 240" stroke="url(#paint0_linear)" strokeWidth="5" strokeLinecap="round" strokeLinejoin="round"/>
<defs>
<linearGradient id="paint0_linear" x1="3" y1="3" x2="3" y2="240" gradientUnits="userSpaceOnUse">
<stop offset="0" stopColor="#E8FF00" stopOpacity="1">
<animate attributeName="stop-opacity" values="1;1;1;0;0;0.5;0.8;1;1;1;1;1" dur="1s" repeatCount="indefinite" />
</stop>
<stop offset="0.3" stopColor="#E8FF00" stopOpacity="0">
<animate attributeName="offset" values="0.3;0.2;0.1;0;1;0.9;0.8;0.7;0.6;0.5;0.4;0.3" dur="1s" repeatCount="indefinite" />
</stop>
<stop offset="0.4" stopColor="#E8FF00" stopOpacity="0">
<animate attributeName="offset" values="0.4;0.3;0.2;0.1;0;1;0.9;0.8;0.7;0.6;0.5;0.4" dur="1s" repeatCount="indefinite" />
</stop>
<stop offset="0.5" stopColor="#E8FF00" stopOpacity="0">
<animate attributeName="offset" values="0.5;0.4;0.3;0.2;0.1;0;1;0.9;0.8;0.7;0.6;0.5" dur="1s" repeatCount="indefinite" />
</stop>
<stop offset="1" stopColor="#E8FF00" stopOpacity="1">
<animate attributeName="stop-opacity" values="1;1;1;1;0.8;0.5;0;0;0;1;1;1" dur="1s" repeatCount="indefinite" />
</stop>
</linearGradient>
</defs>
</svg>
Consider solving a gradient animation using gradientTransform
Below is the code showing the position of the white bar in the static version
<svg width="108" height="243" viewBox="0 0 108 243" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="3" y="3" width="5" rx="5" height="240" fill="url(#paint0_linear)" />
<defs>
<linearGradient id="paint0_linear" x1="0%" y1="0%" x2="0%" y2="100%"" >
<stop offset="0%" stop-color="grey" />
<stop offset="43%" stop-color="grey" />
<stop offset="50%" stop-color="white" />
<stop offset="57%" stop-color="grey" />
<stop offset="100%" stop-color="grey" />
</linearGradient>
</defs>
</svg>
Add an animation command to move the entire gradient from top to bottom
<animateTransform attributeName="gradientTransform" ... />
<svg width="108" height="243" viewBox="0 0 108 243" fill="none" xmlns="http://www.w3.org/2000/svg">
<rect x="3" y="3" width="5" rx="5" height="240" fill="url(#paint0_linear)" />
<defs>
<linearGradient id="paint0_linear" x1="0%" y1="0%" x2="0%" y2="100%" >
<stop offset="0%" stop-color="grey" />
<stop offset="43%" stop-color="grey" />
<stop offset="50%" stop-color="white" />
<stop offset="57%" stop-color="grey" />
<stop offset="100%" stop-color="grey" />
<animateTransform attributeName="gradientTransform"
type="translate"
from="0 -1"
to="0 1"
begin="0s"
dur="1.5s"
repeatCount="indefinite"/>
</linearGradient>
</defs>
</svg>
Update
#tejash jl comments:
animationTransform is not working on line path
I changed your path a little
<svg width="108" height="243" viewBox="0 0 108 243" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M3 3L3 240 L6 240 L6 3z" fill="url(#paint0_linear)" stroke-linecap="round" stroke-linejoin="round"/>
<defs>
<linearGradient id="paint0_linear" x1="0%" y1="0%" x2="0%" y2="100%" >
<stop offset="0%" stop-color="grey" />
<stop offset="43%" stop-color="grey" />
<stop offset="50%" stop-color="white" />
<stop offset="57%" stop-color="grey" />
<stop offset="100%" stop-color="grey" />
<animateTransform attributeName="gradientTransform"
type="translate"
from="0 -1"
to="0 1"
begin="0s"
dur="1.5s"
repeatCount="indefinite"/>
</linearGradient>
</defs>
</svg>
The path must be closed for the gradient animation to work. But you can also select a polyline and apply a gradient to the line
<svg width="108" height="243" viewBox="0 0 108 243" fill="none" xmlns="http://www.w3.org/2000/svg">
<polyline points="3,3 3,240" stroke="url(#paint0_linear)" stroke-width="5" stroke-linecap="round" stroke-linejoin="round"/>
<defs>
<linearGradient id="paint0_linear" x1="3" y1="3" x2="3" y2="240" gradientUnits="userSpaceOnUse" >
<stop offset="0" stop-color="grey" />
<stop offset=".43" stop-color="grey" />
<stop offset=".50" stop-color="white" />
<stop offset=".57" stop-color="grey" />
<stop offset="1" stop-color="grey" />
<animateTransform attributeName="gradientTransform"
type="translate"
from="0 -240"
to="0 240"
begin="0s"
dur="1.5s"
repeatCount="indefinite"/>
</linearGradient>
</defs>
</svg>

How can I rewrite a gradient with userSpaceOnUse to objectBoundingBox?

I have this gradient that uses userSpaceOnUse.
<svg height="400px" width="800px" viewBox="0 0 800 400" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" x1="0" y1="0" x2="800" y2="400" gradientUnits="userSpaceOnUse">
<stop offset="0%" style="stop-color:green;stop-opacity:1" />
<stop offset="50%" style="stop-color:black;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<rect x="0" y="0" width="800" height="400" fill="url(#grad1)"/>
</svg>
I want to rewrite it to use objectBoundingBox but be rendered the same. How can I do this?
I imagine the rewritten SVG would be close to:
<svg height="400px" width="800px" viewBox="0 0 800 400" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" x1="0" y1="0" x2="1" y2="1" gradientUnits="objectBoundingBox">
<stop offset="0%" style="stop-color:green;stop-opacity:1" />
<stop offset="50%" style="stop-color:black;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<rect x="0" y="0" width="800" height="400" fill="url(#grad1)"/>
</svg>
This answer (https://stackoverflow.com/a/50624704/1283776) indicates that I need to use a transform, but I haven't been successful in found a solution.
<!-- https://stackoverflow.com/questions/22214999/svg-linear-gradient-independent-of-the-shape-coordinates -->
<svg height="400px" width="800px" viewBox="0 0 800 400" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" x1="0" y1="0" x2="1" y2="0.5" gradientUnits="objectBoundingBox" gradientTransform="scale(1, 2)">
<stop offset="0%" style="stop-color:green;stop-opacity:1" />
<stop offset="50%" style="stop-color:black;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<rect x="0" y="0" width="800" height="400" fill="url(#grad1)"/>
</svg>

SVG linear gradients objectBoundingBox vs userSpaceOnUse

I am making two gradients: one in objectBoundingBox units and another in userSpaceOnUse. The idea is to make them look the same. But somehow they are different. Here is the svg file.
<svg width="500" height="500" viewBox="0 0 500 500" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="user-grad" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="200" y2="100">
<stop stop-color="orange" offset="0"/>
<stop stop-color="blue" offset="1"/>
</linearGradient>
<linearGradient id="box-grad" x1="0" y1="0" x2="1" y2="1">
<stop stop-color="orange" offset="0"/>
<stop stop-color="blue" offset="1"/>
</linearGradient>
</defs>
<rect x="0" y="0" width="200" height="100" fill="url(#user-grad)"/>
<rect x="250" y="0" width="200" height="100" fill="url(#box-grad)"/>
</svg>
Here is what it looks like
Shouldn't they look the same?
Shouldn't they look the same?
No. When using object bounding box coordinates, you are basically transforming a 1x1 square onto your rectangle. So the 0 to 1 coordinates are stretched to fit the rectangle. Thus causing the gradient to stretch also.
If you want them to look the same, you would need to apply a gradientTransform, to your userSpaceOnUse one, that applies the equivalent stretch.
<svg width="500" height="500" viewBox="0 0 500 500" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<linearGradient id="user-grad" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="100" y2="100" gradientTransform="scale(2, 1)">
<stop stop-color="orange" offset="0"/>
<stop stop-color="blue" offset="1"/>
</linearGradient>
<linearGradient id="box-grad" x1="0" y1="0" x2="1" y2="1">
<stop stop-color="orange" offset="0"/>
<stop stop-color="blue" offset="1"/>
</linearGradient>
</defs>
<rect x="0" y="0" width="200" height="100" fill="url(#user-grad)"/>
<rect x="250" y="0" width="200" height="100" fill="url(#box-grad)"/>
</svg>

What’s causing the linear-gradient to degrade in image quality when placed on Blogger?

After it is placed on Blogger, the hue of the linear gradient drops/lessens dramatically.
How do I prevent this from happening?
Is there any possible way I can fix this so it stays the same?
https://jsfiddle.net/cs311dLn/1/
https://testpage34567.blogspot.com/
The linear-gradient in the images are the 2 lines in the middle going down.
.html
Screenshot
Blogger:
Screenshot
<svg width="260" height="194">
<defs>
<clippath id="circleView">
<circle cx="130" cy="97" r="85" fill="orange"></circle>
</clippath>
</defs>
<image x="40" y="7" width="180" height="180" xlink:href="https://i.imgur.com/BO6KOvw.jpg" clip-path="url(#circleView)"> </image>
<image x="40" y="7" width="180" height="180" xlink:href="http://i.imgur.com/4HJbzEq.png"></image>
<svg width="260" height="194">
<defs>
<lineargradient id="MyGradient">
<stop offset="0%" stop-color="transparent"></stop>
<stop offset="31.9%" stop-color="transparent"></stop>
<stop offset="31.9%" stop-color="#0059dd"></stop>
<stop offset="33.2%" stop-color="#0059dd"></stop>
<stop offset="33.2%" stop-color="transparent"></stop>
<stop offset="66.8%" stop-color="transparent"></stop>
<stop offset="66.8%" stop-color="#0059dd"></stop>
<stop offset="68.1%" stop-color="#0059dd"></stop>
<stop offset="68.1%" stop-color="transparent"></stop>
<stop offset="100%" stop-color="transparent"></stop>
</lineargradient>
</defs>
<rect fill="url(#MyGradient)" x="0" y="0" width="260" height="194"></rect>
</svg>
</svg>

SVG background not applied

I've made a simple SVG image but the background isn't applied whereas it should.
I got the following code so far:
<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" id="content" width="100%" height="100%" viewbox="0 0 800 1000" preserveaspectratio="xMidYMid meet" version="1.1">
<defs>
<lineargradient id="gradient" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" stop-color="#ffffff" stop-opacity="0"/>
<stop offset="100%" stop-color="orange" stop-opacity="0.8"/>
</lineargradient>
<lineargradient id="gradientLine" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" stop-color="orange" stop-opacity="0"/>
<stop offset="100%" stop-color="orange" stop-opacity="0.6"/>
</lineargradient>
</defs>
<g id="vis" transform="translate(330,670)">
<path fill="url(#gradient)" d="M-112.87131548350527,-640.1250394579351A650,650 0 0,1 222.31309316168483,-610.8002035108404L0,0Z"/>
<circle r="35" stroke="orange" fill="orange" opacity="1"/>
<circle r="3" stroke="black" fill="black" opacity="1"/>
<line x1="0" x2="-85" y1="0" y2="0" fill="black" stroke="black"/>
</g>
</svg>
The path is black but it should be a gradient going from white to orange.
Apparently, when I run my code in jsfiddle, it displays the good result. But when I try to display the SVG in an img tag or directly in my browser, I got a black background. Any idea ?
Did I make something wrong or is this a common issue ?
Thanks for your help.
I think the problem is here:
<lineargradient id="gradient" x1="0%" y1="0%" x2="0%" y2="100%">
Fixed:
<linearGradient id="gradient" x1="0%" y1="0%" x2="0%" y2="100%">

Resources