JSFiddle - React, Tailwind, and code Playground
by dylanfm
HTML
<div id="canvas"> </div>
CSS
body {
background: #f7fafb;
}
JavaScript
var amount = prompt("What perccent? (0-100)", "50");
var archtype = Raphael("canvas", 105, 105);
var getFinalPoint = function (xloc, yloc, value, total, R) {
var alpha = 360 / total * value,
a = (90 - alpha) * Math.PI / 180,
x = xloc + R * Math.cos(a),
y = yloc - R * Math.sin(a);
return [x,y];
};
archtype.customAttributes.arc = function (xloc, yloc, value, total, R) {
var alpha = 360 / total * value,
a = (90 - alpha) * Math.PI / 180,
x = xloc + R * Math.cos(a),
y = yloc - R * Math.sin(a),
path = [["M", xloc, yloc - R]];
if (total === value) {
path.push(["A", R, R, 0, 1, 1, xloc - 0.01, yloc - R]);
} else {
path.push(["A", R, R, 0, +(alpha > 180), 1, x, y]);
}
return {
path: path
};
};
// Make an arc at 50,50 with a radius of 48 that grows from 0 to amount of 100 with a bounce
var my_arc = archtype.path().attr({
"stroke": "#18abe1",
"stroke-width": 2,
arc: [50, 50, 0, 100, 48]
});
my_arc.animate({
arc: [50, 50, amount, 100, 48]
}, 500, "easeIn");
// Add a dot at the final point of the arc, if it's < 100
if (amount < 100) {
var finalPoint = getFinalPoint(50, 50, amount, 100, 48),
dot = archtype
.circle(finalPoint[0], finalPoint[1], 0)
.attr({ fill: '#18abe1', stroke: 'none' }),
anim = Raphael.animation({ r: 4 }, 300, "easeOut");
dot.animate(anim.delay(500));
}