This is the result i want to make with divs How can i achieve this result?
Edit: my goal was not to use divs only that i didn’t want to use canvas. But i haven’t thought about SVG’s so thank you!
Advertisement
Answer
Here’s a quick and dirty alternative example using SVG arcs
JavaScript
x
34
34
1
const cx = 100; // Circle centre
2
const cy = 100;
3
const width = 40; // Width of line
4
const radius = 100; // Radius of circle
5
const TwoPi = Math.PI * 2;
6
7
// Compute circumference
8
const circ = TwoPi * radius;
9
const height = circ / 12; // Length of each segment
10
const parent = document.getElementById("curve");
11
12
for (let i = 0; i < circ; i += height) {
13
let seg = document.createElementNS("http://www.w3.org/2000/svg", "path");
14
15
let rs = (i / circ) * TwoPi;
16
let re = ((i + height) / circ) * TwoPi;
17
18
let ss = Math.sin(rs);
19
let cs = Math.cos(rs);
20
let se = Math.sin(re);
21
let ce = Math.cos(re);
22
23
// Build wedge path element
24
seg.setAttribute("d",
25
`M${(cs * radius) + cx},${(ss * radius) + cy}` +
26
`A${radius},${radius} ${((re - rs) / Math.PI) * 180},0,1 ${(ce * radius) + cx},${(se * radius) + cy}` +
27
`L${(ce * (radius - width)) + cx},${(se * (radius - width)) + cy}` +
28
`A${radius - width},${radius - width} ${((re - rs) / Math.PI) * -180},0,0 ${(cs * (radius - width)) + cx},${(ss * (radius - width)) + cy}z`
29
);
30
31
seg.setAttribute("class", "pathSeg");
32
33
parent.appendChild(seg);
34
}
JavaScript
1
2
1
.pathSeg { stroke: black; stroke-width: 3px; fill: white }
2
.pathSeg:hover { fill: red }
JavaScript
1
3
1
<svg width="200" height="200" viewBox="0 0 200 200">
2
<g id="curve"></g>
3
</svg>