I need to animate this so that it’ll look like a running conveyor belt, is there are any way to achieve it?
const cx = 50;
const cy = 50;
const width = 40;
const radius = 100; // CV size
const TwoPi = Math.PI * 0.5;
const circ = TwoPi * radius;
const height = circ / 4; // Roller counts
const parent = document.getElementById("bend");
for (let i = 0; i < circ; i += height) {
let seg = document.createElementNS("http://www.w3.org/2000/svg", "path");
let rs = (i / circ) * TwoPi;
let re = ((i + height) / circ) * TwoPi;
let ss = Math.sin(rs);
let cs = Math.cos(rs);
let se = Math.sin(re);
let ce = Math.cos(re);
seg.setAttribute("d",
`M${(cs * radius) + cx},${(ss * radius) + cy}` +
`A${radius},${radius} ${((re - rs) / Math.PI) * 180},0,1 ${(ce * radius) + cx},${(se * radius) + cy}` +
`L${(ce * (radius - width)) + cx},${(se * (radius - width)) + cy}` +
`A${radius - width},${radius - width} ${((re - rs) / Math.PI) * -180},0,0 ${(cs * (radius - width)) + cx},${(ss * (radius - width)) + cy}z`
);
seg.setAttribute("fill", "url(#grad2)");
seg.setAttribute("stroke", "#008000");
parent.appendChild(seg);
}
<svg width="200" height="200" viewBox="0 0 200 200" style="transform: rotate(-90deg)">
<defs>
<linearGradient id="grad2" x1="0%" y1="0%" x2="40%" y2="100%">
<stop offset="0%" style="stop-color:#008000;stop-opacity:1"/>
<stop offset="100%" style="stop-color:#c7fdc7;stop-opacity:1"/>
</linearGradient>
</defs>
<g id="bend"></g>
</svg>
Here’s the preview of the above code, and this is the running animation example.
Thankyou.
2
Answers
Here is a possible solution (run the snippet below):
Here is a version made with a slice of a circle that is repeated. The animation is made with the
<animateTransform>
element.Less calculations and more construction.