Horizontal Text In A Circular SVG Menu
I need to do a circular menu, with an unknown/variable number of elements (I underline this point because not interested in 3 or 4 elements static solutions). I decided to use SVG.
Solution 1:
Something like this?
let links = [
{
text: "Our products",
url: "https://stackoverflow.com"
},
{
text: "Services",
url: "https://stackoverflow.com"
},
{
text: "Achievements",
url: "https://stackoverflow.com"
},
{
text: "something else",
url: "https://stackoverflow.com"
},
{
text: "test",
url: "https://stackoverflow.com"
},
{
text: "more stuff",
url: "https://stackoverflow.com"
}
];
const RADIUS_PADDING = 10;
function makeMenu(circleElementId, linksData)
{
var circle = document.getElementById(circleElementId);
var svg = circle.ownerSVGElement;
var r = circle.r.baseVal.value + RADIUS_PADDING;
var cx = circle.cx.baseVal.value;
var cy = circle.cx.baseVal.value;
for (var i = 0; i < linksData.length; i++)
{
var angle = i * 2 * Math.PI / linksData.length;
var o = {'x': cx + r * Math.sin(angle),
'y': cy - r * Math.cos(angle),
'text-anchor': (angle <= Math.PI) ? "start" : "end"};
// Make a link (a) element
var aLink = addLink(linksData[i].url, svg);
// Make a text element for the link text
addText(o, linksData[i].text, aLink);
}
}
function addLink(url, parent)
{
var link = document.createElementNS(parent.namespaceURI, "a");
link.setAttributeNS("http://www.w3.org/1999/xlink", "href", url);
parent.appendChild(link);
return link;
}
function addText(o, txt, parent)
{
var text = document.createElementNS(parent.namespaceURI, "text");
for (var name in o) {
if (o.hasOwnProperty(name)) {
text.setAttribute(name, o[name]);
}
text.textContent = txt;
}
parent.appendChild(text);
return text;
}
makeMenu("menu-circle", links)
svg {
border: 1px solid;
width:90vh;
}
circle {
fill: none;
stroke: #d9d9d9;
}
text {
font-size: 8px;
font-family:consolas;
dominant-baseline: middle;
}
a:hover{fill:red}
<div class="container">
<svg viewBox="-10 0 220 200">
<circle id="menu-circle" r="60" cx="100" cy="100" stroke="black" fill="none"/>
</svg>
</div>
Solution 2:
No complex JS libraries, however there is some JS used to calculate the position of the text. The text is horizontal. I don't really like the outcome (aesthetically speaking). For a circular menu I would use icons.
const SVG_NS = "http://www.w3.org/2000/svg";
const SVG_XLINK = "http://www.w3.org/1999/xlink";
let links = [
{
text: "Our products",
parent: "_a"
},
{
text: "Services",
parent: "_b"
},
{
text: "Achievements",
parent: "_c"
},
{
text: "something else",
parent: "_d"
},
{
text: "test",
parent: "_e"
}
];
let R = 60;
let center = { x: 100, y: 100 };
for (let i = 0; i < links.length; i++) {
let angle = i * 2 * Math.PI / links.length;
let o = {};
o.x = center.x + R * Math.cos(angle);
o.y = center.y + R * Math.sin(angle);
let theparent = document.querySelector("#" + links[i].parent);
drawText(o, links[i].text, theparent);
}
function drawText(o, txt, parent) {
var text = document.createElementNS(SVG_NS, "text");
for (var name in o) {
if (o.hasOwnProperty(name)) {
text.setAttributeNS(null, name, o[name]);
}
text.textContent = txt;
}
parent.appendChild(text);
return text;
}
svg {
border: 1px solid;
width:90vh;
}
circle {
fill: none;
stroke: #d9d9d9;
}
text {
font-size: 12px;
font-family:consolas;
dominant-baseline: middle;
text-anchor: middle;
}
a:hover{fill:red}
<div class="container">
<svg viewBox="-10 0 220 200">
<circle r="60" cx="100" cy="100" stroke="black" fill="none"/>
<a xlink:href="https://stackoverflow.com" id="_a"></a>
<a xlink:href="https://stackoverflow.com" id="_b"></a>
<a xlink:href="https://stackoverflow.com" id="_c"></a>
<a xlink:href="https://stackoverflow.com" id="_d"></a>
<a xlink:href="https://stackoverflow.com" id="_e"></a>
</svg>
</div>
See a codepen demo
Post a Comment for "Horizontal Text In A Circular SVG Menu"