Hello
I am developing a react app and I want to set a SVG for my header background.
But when I export it from adobe XD it's size will change.
This is the SVG code :
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1970.909" height="211.264" viewBox="0 0 1970.909 211.264">
<defs>
<filter id="Path_7" x="0" y="0" width="1970.909" height="211.264" filterUnits="userSpaceOnUse">
<feOffset dy="6" input="SourceAlpha"/>
<feGaussianBlur stdDeviation="8.5" result="blur"/>
<feFlood flood-color="#396eb0" flood-opacity="0.278"/>
<feComposite operator="in" in2="blur"/>
<feComposite in="SourceGraphic"/>
</filter>
</defs>
<g transform="matrix(1, 0, 0, 1, 0, 0)" filter="url(#Path_7)">
<path id="Path_7-2" data-name="Path 7" d="M0,0,1919.909.341V159.89s-164.931-36.264-428.15-36.264-349.451,32.555-624.725,36.264-259.615-21.429-476.374-21.429S0,159.89,0,159.89Z" transform="translate(25.5 19.5)" fill="#f05454"/>
</g>
</svg>
This is a link to image
Edit: I can resize my SVG in adobe illustrator and every thing is fine , but I want to export and use directly from adobe XD
Edit 2: If I remove width a and height from svg it will not fit the screen again it's like it has margin.
I feel that this SVG could be optimized a bit. The path itself is fairly simple. So, in this example I scaled down the path. Now the numbers in the path are easier to overview. Then the viewBox can be simple as well. And now the filter is also more controllable.
PS: in your code you have two elements with the same id. That should be avoided.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 36 5">
<defs>
<filter id="Path_7" filterUnits="userSpaceOnUse">
<feOffset dy=".5" input="SourceAlpha"/>
<feGaussianBlur stdDeviation=".2" result="blur"/>
<feFlood flood-color="#396eb0" flood-opacity=".6"/>
<feComposite operator="in" in2="blur"/>
<feComposite in="SourceGraphic"/>
</filter>
</defs>
<path filter="url(#Path_7)" data-name="Path 7" d="M 0 0 L 36 0 V 3 s -3 -0.5 -7 -0.5 s -7 0.5 -11 0.5 s -7 -0.5 -11 -0.5 S 0 3 0 3 Z" fill="#f05454"/>
</svg>
If you remove the width and height attributes from both the svg element and the filter element, then it will scale normally:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1970.909 211.264">
<defs>
<filter id="Path_7" x="0" y="0" filterUnits="userSpaceOnUse">
<feOffset dy="6" input="SourceAlpha"/>
<feGaussianBlur stdDeviation="8.5" result="blur"/>
<feFlood flood-color="#396eb0" flood-opacity="0.278"/>
<feComposite operator="in" in2="blur"/>
<feComposite in="SourceGraphic"/>
</filter>
</defs>
<g transform="matrix(1, 0, 0, 1, 0, 0)" filter="url(#Path_7)">
<path id="Path_7-2" data-name="Path 7" d="M0,0,1919.909.341V159.89s-164.931-36.264-428.15-36.264-349.451,32.555-624.725,36.264-259.615-21.429-476.374-21.429S0,159.89,0,159.89Z" transform="translate(25.5 19.5)" fill="#f05454"/>
</g>
</svg>
So, if you check the fiddle, (https://jsfiddle.net/xXnikosXx/6jx2rpsf/3/)
I have an SVG image, with a couple of objects, and a mask. It's masking the text (colorful lines) which are hidden below it, and the plan was to animate their top position to make the text from below appear and the text from the top to disappear, to make a text scrolling effect. The text without the mask is shown in the image below, in case it helps.
here
I thought the text would appear when the animation occurs, but it doesnt (if you remove the mask, the text is shown and animated properly but they layers are wrong so the text appears above everything else)
I cant figure out a way to make the text animate properly using the mask, is there something i can use instead of the mask to get the same result, but without the text issue?
relevant code:
<mask id="mask0" mask-type="alpha" maskUnits="userSpaceOnUse" x="354" y="174" width="733" height="344">
<rect id="laptop-screen-front" x="354" y="174" width="733" height="344" fill="white" />
</mask>
<g mask="url(#mask0)">
<!-- some of the lines that represent text: -->
<g id="text-lines">
<line id="Line 3" x1="408" y1="194.5" x2="433" y2="194.5" stroke="#E06C60" stroke-width="5" />
<line id="Line 23" x1="438" y1="194.5" x2="463" y2="194.5" stroke="#D18C4C" stroke-width="5" />
<line id="Line 35" x1="469" y1="194.5" x2="542" y2="194.5" stroke="#7BC379" stroke-width="5" />
<line id="Line 36" x1="469" y1="209.5" x2="525" y2="209.5" stroke="#7BC379" stroke-width="5" />
</g>
Your code doesn't animate the mask. It attempts to animate the position of the text-lines g element. But SVG is not HTML. It is its own thing and doesn't support many common CSS properties: e.g. most relevant for you - there is no "top" property in SVG. The easiest way to do what you want is to get rid of all the CSS animations, and add a transform and a SMIL animation directly into the following part of the SVG.
<g mask="url(#mask0)">
<g id="text-lines" transform="translate(0 0)">
<animateTransform attributeName="transform" type="translate" values="0 0; 0 -150; 0 0" dur="11s" repeatCount="indefinite"/>
<line id="Line 3" x1="408" .... etc.
BTW - one of the reasons to use transforms for animation is that browsers can often optimize things by doing the animation on the GPU - so it's better than viewBox or top animations.
2 methods:
with SMIL
svg#parent {
background-color: rgba(100, 148, 237, 0.3);
}
<svg id="parent" width="200" height="100" viewBox="0 0 200 100">
<rect x="10" y="10" width="180" height="20" fill="white" />
<svg x="10" y="10" width="180" height="20" viewBox="0 0 180 20" >
<text x="20" y="15" > hello </text>
<text x="20" y="35" > world </text>
<animate attributeName="viewBox"
values="0 0 180 20; 0 20 180 20; 0 0 180 20"
begin="1s" dur="2s" repeatCount="indefinite" />
</svg>
</svg>
with javascript - requestAnimationFrame
(function()
{
const
svgInside = document.querySelector('#insideSVG')
, moving = .7 // change the value to slow down or speed up
;
let
stepVal = moving
, stepPos = 0
;
requestAnimationFrame(step)
;
function step()
{
stepPos += stepVal
if (stepPos<=0) stepVal = moving
if (stepPos>=20) stepVal = -moving
svgInside.setAttribute('viewBox',`0 ${stepPos} 180 20`)
requestAnimationFrame(step)
}
}
)()
svg#parent {
background-color: rgba(100, 148, 237, 0.3);
}
<svg id="parent" width="200" height="100" viewBox="0 0 200 100">
<rect x="10" y="10" width="180" height="20" fill="white" />
<svg x="10" y="10" width="180" height="20" viewBox="0 0 180 20"
id="insideSVG" >
<text x="20" y="15" > hello </text>
<text x="20" y="35" > world </text>
</svg>
</svg>
For your SVG the values should be around:
<svg x="354" y="174" width="733" height="344" viewBox="354 174 733 344" >
<line x1="419" y1="187.5" ..... />
/.../
<animate attributeName="viewBox"
values="354 174 733 344; 354 520 733 344; 354 174 733 344"
begin="1s" dur="8s" repeatCount="indefinite" />
</svg>
how does <animate> work ?
– Paul LeBeau
the mdn documentation is quite complete: :
SVG animation with SMIL
<animate>
do not hesitate to consult the links to additional information pages
but if you want a general vision of the animation possibilities of your SVGs, the Wikipedia page is ideal
There is even a part showing the use of the requestAnimationFrame(), which I am using here.
PS:The Whitesmith style has been around since 1978, and no, I don't use it for aesthetic reasons but because it checks that the right pairs of braces frame the right pieces of code. For the same reasons, I put the commas in front of data lines (they are invisible [and often forgotten] at the end of the line), as well as the column alignment (both in a similar way used in Haskell)
No, the K&R is not a foolproof style, nor is it more legitimate than any other.
I have a few SVG objects in a group. It is a curve with an "X" in the middle of it. I would like to make it look a little nicer and have a drop shadow behind it. I know I can't just CSS and will have to use a filter.
The problem I'm having is getting this drop shadow to be a circle since I believe the path was made as a square. I thought about overlaying an ellipse and just making the fill none, but I wanted to know if there is a better way.
Picture of what it looks like
Code
<g class="link" data-link_id="1">
<path stroke-width="5" fill="none" d="M1732,610.0000305175781 C1832,610.0000305175781 1760.0000610351562,710.0000305175781 1861.0000610351562,710.0000305175781" stroke="#000000"></path>
<rect stroke="none" mask="url(#fc_mask_0_1)" x="1860.0000610351562" y="707.5000305175781" width="21" height="5" fill="#000000"></rect>
<defs>
<filter id="f1" x="0" y="0">
<feOffset result="offOut" in="SourceAlpha" dx="-5" dy="-5"></feOffset>
<feGaussianBlur result="blurOut" in="offOut" stdDeviation="1"></feGaussianBlur>
<feBlend in="SourceGraphic" in2="blurOut" mode="normal"></feBlend>
</filter>
</defs>
<path stroke-width="5" fill="red" class="delete_link" filter="url(#f1)" transform="translate(1796.209228515625,660.2947998046875)" width="5" height="5" d="m10.84806,-5.81639c-1.08944,-1.8664 -2.56719,-3.34415 -4.43364,-4.43353c-1.86679,-1.08933 -3.90465,-1.63388 -6.11514,-1.63388c-2.21027,0 -4.24874,0.54455 -6.11519,1.63388c-1.86657,1.08921 -3.34432,2.56697 -4.43375,4.43353c-1.08949,1.86668 -1.63404,3.90515 -1.63404,6.11525c0,2.21021 0.54472,4.24835 1.63388,6.11514c1.08938,1.8664 2.56713,3.34415 4.4337,4.43364c1.86668,1.08927 3.90498,1.63388 6.11519,1.63388s4.24874,-0.54461 6.1153,-1.63388c1.86645,-1.0891 3.34415,-2.56724 4.43348,-4.43364c1.08916,-1.86662 1.63365,-3.90504 1.63365,-6.11514c0.00006,-2.21032 -0.5445,-4.24885 -1.63343,-6.11525zm-4.80668,8.98619c0.20096,0.20074 0.30158,0.43921 0.30158,0.71418c0,0.28519 -0.10062,0.52861 -0.30158,0.72935l-1.42752,1.42797c-0.2008,0.20074 -0.44415,0.30114 -0.72979,0.30114c-0.27491,0 -0.51316,-0.1004 -0.71379,-0.30114l-2.87116,-2.87149l-2.87127,2.87149c-0.20091,0.20074 -0.43882,0.30114 -0.71379,0.30114c-0.28558,0 -0.52877,-0.1004 -0.72974,-0.30114l-1.42775,-1.42797c-0.20091,-0.20074 -0.30136,-0.44415 -0.30136,-0.72935c0,-0.27491 0.10051,-0.51338 0.30136,-0.71418l2.87127,-2.87099l-2.87127,-2.87127c-0.20091,-0.20069 -0.30136,-0.43882 -0.30136,-0.71379c0,-0.28558 0.10051,-0.52877 0.30136,-0.72974l1.42775,-1.42752c0.20096,-0.20091 0.44415,-0.30136 0.72974,-0.30136c0.27497,0 0.51288,0.10051 0.71379,0.30136l2.87127,2.87105l2.87116,-2.87105c0.20057,-0.20091 0.43882,-0.30136 0.71379,-0.30136c0.28564,0 0.52899,0.10051 0.72979,0.30136l1.42752,1.42752c0.20096,0.20096 0.30158,0.44415 0.30158,0.72974c0,0.27491 -0.10062,0.5131 -0.30158,0.71379l-2.87111,2.87127l2.87111,2.87099z"></path>
</g>
The problem is that your filter begins at 0,0: <filter id="f1" x="0" y="0">and your crossed circle at -11.88/-11.88, and your shadow gets clipped.
<svg viewBox="1730 605 160 110">
<g class="link" data-link_id="1">
<path stroke-width="5" fill="none" d="M1732,610.0000305175781 C1832,610.0000305175781 1760.0000610351562,710.0000305175781 1861.0000610351562,710.0000305175781" stroke="#000000"></path>
<rect stroke="none" mask="url(#fc_mask_0_1)" x="1860.0000610351562" y="707.5000305175781" width="21" height="5" fill="#000000"></rect>
<defs>
<filter id="f1" x="-10" y="-10" height="15" width="15">
<feOffset result="offOut" in="SourceAlpha" dx="-5" dy="-5"></feOffset>
<feGaussianBlur result="blurOut" in="offOut" stdDeviation="1"></feGaussianBlur>
<feBlend in="SourceGraphic" in2="blurOut" mode="normal"></feBlend>
</filter>
</defs>
<path id="test" stroke-width="5" fill="red" class="delete_link" filter="url(#f1)" transform="translate(1796.209228515625,660.2947998046875)" width="5" height="5" d="m10.84806,-5.81639c-1.08944,-1.8664 -2.56719,-3.34415 -4.43364,-4.43353c-1.86679,-1.08933 -3.90465,-1.63388 -6.11514,-1.63388c-2.21027,0 -4.24874,0.54455 -6.11519,1.63388c-1.86657,1.08921 -3.34432,2.56697 -4.43375,4.43353c-1.08949,1.86668 -1.63404,3.90515 -1.63404,6.11525c0,2.21021 0.54472,4.24835 1.63388,6.11514c1.08938,1.8664 2.56713,3.34415 4.4337,4.43364c1.86668,1.08927 3.90498,1.63388 6.11519,1.63388s4.24874,-0.54461 6.1153,-1.63388c1.86645,-1.0891 3.34415,-2.56724 4.43348,-4.43364c1.08916,-1.86662 1.63365,-3.90504 1.63365,-6.11514c0.00006,-2.21032 -0.5445,-4.24885 -1.63343,-6.11525zm-4.80668,8.98619c0.20096,0.20074 0.30158,0.43921 0.30158,0.71418c0,0.28519 -0.10062,0.52861 -0.30158,0.72935l-1.42752,1.42797c-0.2008,0.20074 -0.44415,0.30114 -0.72979,0.30114c-0.27491,0 -0.51316,-0.1004 -0.71379,-0.30114l-2.87116,-2.87149l-2.87127,2.87149c-0.20091,0.20074 -0.43882,0.30114 -0.71379,0.30114c-0.28558,0 -0.52877,-0.1004 -0.72974,-0.30114l-1.42775,-1.42797c-0.20091,-0.20074 -0.30136,-0.44415 -0.30136,-0.72935c0,-0.27491 0.10051,-0.51338 0.30136,-0.71418l2.87127,-2.87099l-2.87127,-2.87127c-0.20091,-0.20069 -0.30136,-0.43882 -0.30136,-0.71379c0,-0.28558 0.10051,-0.52877 0.30136,-0.72974l1.42775,-1.42752c0.20096,-0.20091 0.44415,-0.30136 0.72974,-0.30136c0.27497,0 0.51288,0.10051 0.71379,0.30136l2.87127,2.87105l2.87116,-2.87105c0.20057,-0.20091 0.43882,-0.30136 0.71379,-0.30136c0.28564,0 0.52899,0.10051 0.72979,0.30136l1.42752,1.42752c0.20096,0.20096 0.30158,0.44415 0.30158,0.72974c0,0.27491 -0.10062,0.5131 -0.30158,0.71379l-2.87111,2.87127l2.87111,2.87099z"></path>
</g>
</svg>
I'd like to add a border to a number of elements which are grouped by <g>. As an example:
<g id="group">
<circle cx="125" cy="125" r="65" fill="orange" />
<line x1="50" y1="50" x2="200" y2="200" stroke="blue" stroke-width="4" />
</g>
Best case, the border should look like in the following picture. The distance between the elements and the border is not required (but nice to have). The main goal should be a single border (stroke) around the group elements.
I found the picture in a tutorial, but there it was just to demonstrate what a group of elements may look like. So this doesn't help.
I already tried different solutions, but none of them worked as expected, e.g.
A SVG filter using feColorMatrix and feMorphology (see this post). But in this case, the color of the elements changes when applying the filter.
Styling the <g> with stroke and stroke-width results in a rectangular border around the group, which is also not the thing I want.
Any ideas, how to get a border around the group as shown in the picture?
It would be difficult to get the dashed stroke shown in the image you provided. But a solid outline should be possible. Here's an example:
<svg width="250" height="250" viewBox="0 0 250 250">
<defs>
<filter id="groupborder" filterUnits="userSpaceOnUse"
x="0" y="0" width="250" height="250">
<feMorphology operator="dilate" in="SourceAlpha"
radius="8" result="e1" />
<feMorphology operator="dilate" in="SourceAlpha"
radius="10" result="e2" />
<feComposite in="e1" in2="e2" operator="xor"
result="outline"/>
<feColorMatrix type="matrix" in="outline"
values="1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 .3 0" result="outline2"/>
<feComposite in="outline2" in2="SourceGraphic"
operator="over" result="output"/>
</filter>
</defs>
<g id="group" filter="url(#groupborder)">
<circle cx="125" cy="125" r="65" fill="orange" />
<line x1="50" y1="50" x2="200" y2="200" stroke="blue" stroke-width="4" />
</g>
</svg>
Here's how it works:
<feMorphology operator="dilate" in="SourceAlpha" radius="8" result="e1" />
Uses a dilate operation to fatten up the graphic elements. By using the source alpha as the input image, this results in black regions corresponding to the graphic elements in the image, and white everywhere else.
<feMorphology operator="dilate" in="SourceAlpha" radius="10" result="e2" />
The same filter again, but with a larger amount of dilation, resulting in a slightly fatter image
<feComposite in="e1" in2="e2" operator="xor" result="outline"/>
These dilated results are combined using an XOR operation that leaves behind a black outline.
<feColorMatrix type="matrix" in="outline"
values="1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 .3 0" result="outline2"/>
This filter multiplies the alpha component of the outline by 0.3 so it appears grey instead of solid black.
<feComposite in="outline2" in2="SourceGraphic" operator="over" result="output"/>
Finally, add this outline to the original image.
I'm having some problems when I imported my svg image as a background in css, it seems like one of the mountains in my background have this small transparent lines. But for some reason it only appears in Chrome but not FireFox.
Thanks.
here's the fiddleJS
.
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1440" height="328.656" viewBox="0 0 1440 328.656" style="shape-rendering: geometricPrecision">
<defs>
<style>
.cls-1 {
filter: url(#filter);
}
.cls-2 {
filter: url(#filter-2);
}
.cls-3 {
fill: #47c9af;
fill-rule: evenodd;
}
</style>
<filter id="filter" filterUnits="userSpaceOnUse">
<feOffset result="offset" dx="81.915" dy="57.358" in="SourceAlpha"/>
<feGaussianBlur result="blur"/>
<feFlood result="flood" flood-color="#34495e"/>
<feComposite result="composite" operator="in" in2="blur"/>
<feBlend result="blend" in="SourceGraphic"/>
</filter>
<filter id="filter-2" filterUnits="userSpaceOnUse">
<feOffset result="offset" dx="42.596" dy="29.826" in="SourceAlpha"/>
<feGaussianBlur result="blur"/>
<feFlood result="flood" flood-color="#16a085"/>
<feComposite result="composite" operator="in" in2="blur"/>
<feBlend result="blend" in="SourceGraphic"/>
</filter>
</defs>
<g id="mountain.svg" class="cls-1">
<g id="_" data-name=">" class="cls-2">
<path id="Polygon_1" data-name="Polygon 1" class="cls-3" d="M-1.8,845.344L135.187,1083h-273.97Z" transform="translate(0 -812.344)"/>
<path id="Polygon_1_copy" data-name="Polygon 1 copy" class="cls-3" d="M272.186,903.344L409.171,1141H135.2Z" transform="translate(0 -812.344)"/>
<path id="Polygon_1_copy_2" data-name="Polygon 1 copy 2" class="cls-3" d="M546.171,845.344L683.156,1083H409.186Z" transform="translate(0 -812.344)"/>
<path id="Polygon_1_copy_3" data-name="Polygon 1 copy 3" class="cls-3" d="M820.155,903.344L957.14,1141H683.17Z" transform="translate(0 -812.344)"/>
<path id="Polygon_1_copy_4" data-name="Polygon 1 copy 4" class="cls-3" d="M1094.14,845.344L1231.12,1083H957.155Z" transform="translate(0 -812.344)"/>
<path id="Polygon_1_copy_5" data-name="Polygon 1 copy 5" class="cls-3" d="M1308.12,812.344L1445.11,1050H1171.14Z" transform="translate(0 -812.344)"/>
</g>
</g>
</svg>
This is a drawing bug in webkit/Chrome that seems to be triggered by the repeated use of feOffset to layer content more than 3 layers deep. If you resize the iframe in the jsfiddle, you'll see that the thin lines stay fixed relative to the parent window, not the content - so it seems to be a low level bug. Based on my experiments, any shape that is 4 or more layers deep gets the drawing artifact.
Easiest way to fix is to define your shape once in defs and utilize multiple use elements to place and color them.
I've filed bug#660745 with Chrome.
(That said - your filters have badly specified primitives (no stdDeviation for the (unused) GaussianBlur, no filter dimensions, no mode for the feBlend) that you should fix. These are better specified versions of your filters.)
<filter id="filter" >
<feOffset result="offset" dx="81.915" dy="57.358" in="SourceAlpha"/>
<feFlood result="flood" flood-color="#34495e"/>
<feComposite result="composite" operator="in" in2="offset"/>
<feBlend mode="normal" result="blend" in="SourceGraphic"/>
</filter>
<filter id="filter-2">
<feOffset result="offset" dx="42.596" dy="29.826" in="SourceAlpha"/>
<feFlood result="flood" flood-color="#16a085"/>
<feComposite result="composite" operator="in" in2="offset"/>
<feBlend mode="normal" result="blend" in="SourceGraphic"/>
</filter>