SVG half of a circle - css

How can I rotate the circle 180 degrees in the SVG, not with CSS? I mean only having the top of the SVG not the bottom.
<svg width="135" height="135">
<path d="M10,80 a60,60 0 0,0 115,0" fill="#f7ad1a" />
</svg>

The third 0 (fifth parameter) to the a (arc) command is the "sweep flag". Change it from a 0 (anti-clockwise) to a 1 (clockwise). It will then go the other direction, around the circle, from the start to the end point.
<svg width="135" height="135">
<path d="M10,80 a60,60 0 0,1 115,0" fill="#f7ad1a" />
</svg>

Related

How to fit svg width/height to be the same as path width/height

I have this svg that renders an arrow
<div style="position: absolute; z-index: 0;">
<svg
width="373"
height="280"
overflow="auto"
style="position: absolute; left: 630.922px; top: -305px; pointer-events: none;"
>
<path
d="M 20 260 C 20 260, 334.74671750091653 33.15551891825835, 334.74671750091653 33.15551891825835"
stroke="CornflowerBlue"
stroke-dasharray="0 0"
stroke-width="5"
fill="transparent"
pointer-events="visibleStroke"
></path>
<g
fill="CornflowerBlue"
pointer-events="auto"
transform="translate(319.89194405571646,25.37183689162216) rotate(-35.78107386189255) scale(30)"
opacity="1"
>
<animate
dur="0.4"
attributeName="opacity"
from="0"
to="1"
begin="indefinite"
repeatCount="0"
fill="freeze"
></animate>
<path d="M 0 0 L 1 0.5 L 0 1 L 0.25 0.5 z"></path>
</g>
</svg>
</div>
This results in
As you can clearly see from the photo (it's an inspect element of the svg element though), the diagonal arrow has blue background even after the arrow's head ends. The same thing counts for the width as well, the blue background is wider than the actual arrow width. Is it possible to fit the svg width/height to be the same on as the path's? The paths looks like this:
If possible, how can I achieve this?
Draw your arrows outside the SVG by either changing the path data or by adding a viewBox to the SVG element with a x/y/width/height that shifts the arrows outside the SVG's boundaries. And then add overflow: visible to your SVG element.
In this example I have a viewBox 500 in width and 300 in height. I made a line from the left bottom corner to the top right corner. I turned the arrow into a <marker>. To hide the start and the end of the line I added a stroke-dasharray there the first segment in 0, a space of 1, a segment of 98 and a space of 1 (0 1 98 1). The path length is set to 100.
<svg viewBox="0 0 500 300" width="500">
<defs>
<marker id="arrow" viewBox="0 0 1 1" refX="1" refY=".5"
markerWidth="6" markerHeight="6"
orient="auto-start-reverse">
<path d="M 0 0 L 1 0.5 L 0 1 L 0.25 0.5 z" fill="CornflowerBlue" />
</marker>
</defs>
<rect width="500" height="300" fill="#eee"/>
<line x1="0" y1="300" x2="500" y2="0" stroke-dasharray="0 1 98 1"
pathLength="100" stroke="CornflowerBlue" stroke-width="5" marker-end="url(#arrow)" />
</svg>

How to do transparent circle on SVG image?

I'm working on SVG game like Worms, but i don´t know how to do SVG terrain. I have created bitmap with logic ( when a bullet hits terrain, it makes crater). And now i got idea i give SVG terrain (img) on bitmap and everytime the bullet hits terrain, it draws svg circle. I know how to do svg circle, but i can´t see background, because the circle hides my background.
So my question is: How can i make transparent circle on SVG image ?
This is what i did https://imgur.com/a/bPHjthi
This is what i want
https://imgur.com/a/ZT54RxI but SVG.
Thanks for help
You can control the background of an svg circle with the fill property, see here. But I'm not sure setting fill: none or fill: transparent is going to achieve what you want, you would simply see the green background trough the circle. So to "cut" out a circle from the green part you would need to use a clipPath, see here;
For this I would use clipPath. In order to clip a circle out of the polygon you will need a path: a rectangle as big as the svg canvas with a circular hole in it:
<svg viewBox="0 0 100 100" width="300">
<path d="M0,0h100v100h-100v-100M60,50a10,10 0 0 0 -20,0a10,10 0 0 0 20,0" />
</svg>
The path is builded out of a rect: M0,0h100v100h-100v-100M60,50 drawn clockwise and a circle drawn counterclockwise. For a circle with a radius of 10 and the center in the {x:50,y:50} you would do something like this: M60,50a10,10 0 0 0 -20,0a10,10 0 0 0 20,0.
Next you use this path as a clipping path to perforate the polygon:
svg{border:1px solid;background:gold}
<svg viewBox="0 0 100 100" width="300">
<clipPath id="clip">
<path d="M0,0h100v100h-100v-100
M60,50a10,10 0 0 0 -20,0a10,10 0 0 0 20,0" />
</clipPath>
<polygon points="0,90 70,30 100,80 100,100 0,100" clip-path="url(#clip)" />
</svg>
If you need to cut several circles out of the polygon you need to change the d attribute for the clipping path by adding a new circle like so:
d="M0,0h100v100h-100v-100
M60,50a10,10 0 0 0 -20,0a10,10 0 0 0 20,0
M40,60a10,10 0 0 0 -20,0a10,10 0 0 0 20,0"
And this is a demo:
svg{border:1px solid;background:gold}
<svg viewBox="0 0 100 100" width="300">
<clipPath id="clip">
<path d="M0,0h100v100h-100v-100
M60,50a10,10 0 0 0 -20,0a10,10 0 0 0 20,0
M40,60a10,10 0 0 0 -20,0a10,10 0 0 0 20,0" />
</clipPath>
<polygon points="0,90 70,30 100,80 100,100 0,100" clip-path="url(#clip)" />
</svg>
To draw a transparent circle in svg you should use the command circle attribute fill=none like this:
<svg viewbox "0 0 200 200" width="200" height="200" xmlns="http://www.w3.org/2000/svg">
<view id="one" viewBox="0 0 200 200" />
<circle cx="100" cy="100" r="40" stroke="red" stroke-width="5" fill=none />
<circle cx="100" cy="100" r="60" stroke="blue" stroke-width="5" fill=none />
</svg>

svg keep element size when adding text

I'm quite new and trying to combine some drawing with texts or draw more. I draw a circle with sections, I want the text to display on center of each. But my viewport doesn't have enough space for text because I draw circle first, when I increase svg width & height my circle becomes bigger which I don't want it happens. I need space to draw lines and texts. Please suggest me how can I keep the circle size (300,300) and have space. Thanks
<svg width="300" height="300" viewBox="0 0 300 300">
<g transform="translate(150,150)" fill="none" stroke-width="45">
<path stroke="#CBA135" d="M-110 0 A110 110 0 0 10-110"/>
<path stroke="#7EC34F" d="M0 -110 A110 110 0 0 1110 0"/>
<path stroke="#ABDB92" d="M110 0 A110 110 0 0 1-110 0"/>
</g>
<g transform="translate(150,150)" fill="none" stroke-width="2">
<path stroke="#fff" d="M0 -137 L0,-82"/>
<path stroke="#fff" d="M-137 0 L-82,0"/>
<path stroke="#fff" d="M137 0 L82,0"/>
<path stroke="#333" d="M117 -99 L137,-119"/>
<path stroke="#333" d="M0 147 L0,167"/>
<path stroke="#333" d="M-117 -99 L-137,-119"/>
<text x="-169" y="-140" font-family="sans-serif" font-size="18px"
fill="#438736">WORD1 WORD2</text>
Here's link to my codepen
When I increase svg width & height my circle becomes bigger which I don't want
The viewBox attribute allows you to specify that a given set of graphics stretch to fit a particular container element.
https://developer.mozilla.org/en/docs/Web/SVG/Attribute/viewBox
I would suggest to remove your ViewBox element and just set the height and width on your SVG. As much as you need.
I don't know much about making circles or anything, but maybe it would work if you make the circle position: relative and the text field position: absolute

Understanding svg's viewbox attribute

I am trying to use Turkey's map which was created before here.
I have changed the dimensions of svg element
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve"
width="800"
height="600"
style="shape-rendering:geometricPrecision;
text-rendering:geometricPrecision;
image-rendering:optimizeQuality;
fill-rule:evenodd;
clip-rule:evenodd"
viewBox="0 0 180 180">
When I change viewBox values to
viewBox="0 0 200 200"
the map is rendered in smaller size.
If I set the values to "0 0 10 10" image is not displayed.
If I set the values to "0 0 50 50" a huge image comes overflowing the page
If I set the values to "0 0 100 100" image is fine.
But I don't understand how it is working.
An SVG canvas is basically an endless plain, where you can put all your objects in.
The width and height you define the size of the image as shown in the browser. This works pretty much like with any other image you can include in HTML.
With the viewBox attribute on the other hand you select the part of that plain, you currently want to view.
So with a value of "0 0 10 10" you set the upper left point of the shown image to the point 0/0 and from there select 10 units to the right and bottom for your image. In your example in those upper 10 times 10 units area there is nothing, hence you see just a transparent area, which you perceive as "not being displayed".
With a value of "0 0 50 50" the shown area gets bigger and you'll start to see the upper left corner of the picture. The first parts of the map become visible.
Finally with "0 0 100 100" you can see pretty much half of the upper map.
The area you select using viewBox is the scaled to the height and width of the SVG element. With both combined you can enabled stuff like zooming into an SVG.
You can control the scaling with the attribute preserveAspectRatio.
You can add the following code at the bottom of the SVG file and see the displayed boxes (make sure to set the viewBox="0 0 180 180" before):
<rect x="0" y="0" width="100" height="100" style="stroke: blue; stroke-width: 1; fill: white; opacity: 0.8" />
<rect x="0" y="0" width="50" height="50" style="stroke: green; stroke-width: 1; fill: none;" />
<rect x="0" y="0" width="10" height="10" style="stroke: red; stroke-width: 1; fill: none;" />
<text x="5" y="97" style="fill: blue; font-size: 5px;">100x100</text>
<text x="5" y="47" style="fill: green; font-size: 5px;">50x50</text>
<text x="5" y="15" style="fill: red; font-size: 5px;">10x10</text>
So to conclude the purpose of viewBox is to select that part of the endless SVG plane, that should actually be rendered. If you choose the wrong values here, you might just see an empty space.
Links:
preserveAspectRatio #MDN
viewBox #MDN
Viewbox with the width and height values are how you can do zooming, and panning with SVG.
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve"
width="800"
height="600"
viewBox="0 0 800 600">
no scaling 800 pixels width in the viewbox equals 800 pixel width on the screen
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve"
width="800"
height="600"
viewBox="0 0 1600 1200">
Everything is scaled down so that a line 1600 pixels wide only shows up as 800 pixels wide.
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve"
width="800"
height="600"
viewBox="400 300 400 300">
Zoom into the bottom right hand 1/4 of the 800 by 600 screen making that portion fill the entire svg, don't show anything above the 300 pixel line or to the left of the 400 pixel line.

SVG Circle Scaling

I have a simple SVG circle:
<svg version="1.1"
xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
x="0px" y="0px" width="100px" height="100px" viewBox="0 0 100 100" overflow="visible" enable-background="new 0 0 100 100"
xml:space="preserve">
<circle fill="#6E6F6F" cx="50" cy="50" r="49"/>
</svg>
This image is being used as a background, and resized to 22px:
background: transparent url('++resource++svg/star_neg.svg') no-repeat 0 0 / 22px 22px;
When I view this in the browser, the right and bottom sides of the circle appear flat in Firefox (Chrome looks fine). If I zoom in on Firefox, the circle appears complete as expected. How can I fix this?
Finally found a simple solution. The div displaying the svg as the background should be larger than the SVG. I had the box at 22px square, the same size I was setting the SVG. To fix the issue, I displayed the box as 25px, with the background image centered instead of set in a corner.

Resources