I have a non-convex region (made from several circle arcs) and want to fill the interior region with a color. However, the fill-property will instead fill the outside, as shown in the picture. How can I achieve the interiour being filled? This is an example code:
<path
id="curved_path"
style="stroke:#000000; stroke-width:0.2px; fill:none"
d="M 79, 66 c 0,0 -17,0 -17, 15
M 45, 66 c 0,0 17,0 17, 15
M 79, 66 c 0,0 -17,0 -17,-15
M 45, 66 c 0,0 17,0 17,-15"
/>
Eventually, I want to write a script which draws a large number (possibly millions) of these curved polygons. If this turns out complicated in SVG, are there other languages or scriptable tools that can handle these problems better?
All those M commands create separate subpaths which are filled independently. You need to draw the star as one continuous path:
<svg width="350" height="300" viewBox="45,51 35,30">
<path id="curved_path"
style="stroke:#000000; stroke-width:0.2px; fill:dodgerblue"
d="M 79,66 c 0, 0 -17, 0 -17, 15
c 0,-15 -17,-15 -17,-15
c 0, 0 17, 0 17,-15
c 0, 15 17, 15 17, 15"
/>
</svg>
Related
I'm trying to draw a partial circle with svg's path. I have the circle center coordinates, radius and the start/end coordinates (where the partial circle will end), but I can't wrap my head around on how to draw the circle.
I hope that this small example can help you. The path starts in 50%,50% (center), moves to 50%,0 (that is 12 o'clock), creates an arc where radius x and y are 50 (the fist two numbers after a) and ends in the position that is a calculation with sin() and cos() on 45 degrees (the angle should be in radians). The three numbers in between (0 0 1) are flags.
See more here: d - Elliptical Arc Curve
// 45 deg:
console.log(Math.cos(Math.PI/4)*50, 50-Math.sin(Math.PI/4)*50);
document.querySelector('path')
.setAttribute('d', `M50,50 L50,0 a 50 50 0 0 1 ${Math.cos(Math.PI/4)*50} ${50-Math.sin(Math.PI/4)*50} z`);
<svg width="200" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="50" fill="lightblue" />
<path fill="navy" />
</svg>
This is my svg path. I am trying to achieve trapezium. Left of the rectangle has come out fine. I wanted the same way on the right side. How do I bend it?
<path d="M20 20 H 300 V 70 H 10 Z" fill="transparent" style="stroke:black; stroke-width:2"/>
This is the image reference of the shape I wanted:
http://amsi.org.au/teacher_modules/C3/C3g37.png
If you don't want a vertical line best not to use V as that command draws them. I've replaced the V with an L below.
<svg width="100%" height="100%">
<path d="M20 20 H 300 L 310 70 H 10 Z" fill="transparent" style="stroke:black; stroke-width:2"/>
</svg>
I need to create a block with specific borders around it:
I want to have this block scalable, that is why I try doing it with svg.
Here is what I've done:
<svg xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none slice" viewBox="0 0 300 400">
<path stroke-width="1" stroke="#7DD37D" fill="red" d="
M20 0h260
m20 20v360
m-20 20H20
m-20 -20V20
M0 20a20 20 0 0 0 20 -20
M300 20a20 20 0 0 1 -20 -20
M300 380a20 20 0 0 0 -20 20
M0 380a20 20 0 0 1 20 20" id="path"/>
</svg>
Demo on codepen
The border was created as path from lines and arcs.
The problem is, that I can't make svg to fill the area inside this path. It fills some space inside arcs instead of rectangle. What I'm doing wrong?
When I do something similar with Inkscape, the resulting path is combined from lines and Cubic Bezier curves. Can it be done with simple arcs instead of Bezier curves?
Each time you use a move ('m' or 'M') path command, it creates a new subpath. Each subpath gets filled, not the whole thing.
If you want the whole thing filled, it has to be a continuous path. In other words, in this case, it should be a move followed by a line, then an arc, then another line, and arc, and so on until you have completed all four sides of the shape.
The SVG I'm working with has a drop shadow via feGaussianBlur filter.
The shadow itself is displayed properly, but gets cut off on top and bottom edges.
Like so:
The SVG in question is:
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE svg
PUBLIC '-//W3C//DTD SVG 1.1//EN'
'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'>
<svg height="600" version="1.1" width="700" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs/>
<filter id="SVGID_0">
<feGaussianBlur in="SourceGraphic" stdDeviation="6.6"/>
<feOffset dx="0" dy="0"/>
<feMerge>
<feMergeNode/>
<feMergeNode in="SourceGraphic"/>
</feMerge>
</filter>
<path d="M 0 83 Q 0 83 0 83 Q 0 83 6 79.5 Q 12 76 17 71 Q 22 66 30.5 57.5 Q 39 49 54 36 Q 69 23 82.5 16.5 Q 96 10 120 4.5 Q 144 -1 170.5 0 Q 197 1 218 16.5 Q 239 32 253.5 51 Q 268 70 278 83.5 Q 288 97 299 110 Q 310 123 320 129.5 Q 330 136 338 136.5 Q 346 137 355 129.5 L 364 122" stroke-linecap="round" style="stroke: #005e7a; stroke-width: 30; fill: none; filter: url(#SVGID_0);" transform="translate(50 50)" />
</svg>
The cropping seems to happen consistently in Chrome (30), Firefox (25), and Opera (12).
I can see that it's not a viewbox limitation, as it's set to 600x700.
I can also see in devtools inspector the bounding box of <path> element, and it's almost as if that's what cuts off the shadow:
If that's the case:
Why is the shadow only cut off vertically and not horizontally?
How to work around it, so that it's not clipped like this?
If it's not the bounding box, what causes this and how to avoid this clipping?
You need to increase the size of the filter region.
<filter id="SVGID_0" y="-40%" height="180%">
works just fine. The silent defaults for the filter region are: x="-10%" y="-10%" width="120%" height="120%" - large blurs usually get clipped. (Your shadow isn't getting clipped horizontally because your width is about 2.5x your height - so that 10% results in a wider horizontal filter region). Also, the y filter region seems to be calculated as if the path had a zero pixel stroke, so it's ignoring the stroke width. (Different browsers have different behavior wrt whether they consider the stroke to be part of the bounding box for purposes of filter region calculation.)
(Update: Moving up observations from comments)
Please note that if your particular shape is either zero width or zero height (e.g. a horizontal or vertical line), then you must specify filterUnits="userSpaceOnUse" as part of the filter declaration and explicitly specify a filter region (x,y,width height) in userSpaceUnits (usually pixels) that creates enough room to display a shadow.
Add an attribute to the filter shadow svg tag:
filterUnits="userSpaceOnUse"
Final Output:
<filter id="dropshadow" filterUnits="userSpaceOnUse" height="800" width="1600">
Which makes the shadow absolutely positioned and visible outside of its container.
If you're using it inside an HTML, you can simply use CSS properties to fix this issue.
svg {
overflow: visible !important;
}
I haven't checked other browsers, but chrome has the overflow: hidden by default on svg tags.
A bit late, but I hope it's helpful.
I want to draw a shape in SVG that has the centre hollowed out.
I asked this question for drawing a circle with a circle in the middle here.
I would like to draw any shape in SVG with the middle (of another shape) hollowed out.
Any ideas?
I'm thinking it could be possible using Masks or ClipPaths but I'm not sure if I understand them fully
Create your shape as a path, which consists of two sub-paths. The first subpath describes your outer shape; the second subpath describes the inside shape. Set the fill-rule of the pathto 'evenodd'.
e.g. for a hollowed out rectangle, we make a path consisting of two sub-paths. One for the outside rectangle; one for the inner rectangle:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<path d="M 100 100 L 200 100 L 200 200 L 100 200 z
M 110 110 L 190 110 L 190 190 L 110 190 z"
fill="red"
stroke="black" stroke-width="1"
fill-rule="evenodd"/>
</svg>