Colleagues, what do you think, how can I slowly slow down the circle drawing at the end of the animation? Experience javascript = junior.
PS Updated js, added animation function, found here http://kodhus.com/easings/ Understand how to use it correctly
let canvas = document.querySelector('#cvs'); let ctx = canvas.getContext('2d'); let levelOfKnowledge = document.querySelector('#percent'); let nameOfKnowledge = 'HTML'; //ΠΈΠΌΡ Π·Π½Π°Π½ΠΈΡ let pi = Math.PI; let coefficient = pi * 2 / 100; let min = 0; //ΡΠΎΡΠΊΠ° ΡΡΠ°ΡΡΠ° Π°Π½ΠΈΠΌΠ°ΡΠΈΠΈ let maxPercent = 78; //ΠΏΡΠΎΡΠ΅Π½Ρ Π·Π½Π°Π½ΠΈΠΉ let circleStyle = 50; //ΡΡΠ½ΠΊΡΠΈΡ ΠΊΠΎΡΠΎΡΠ°Ρ Π²ΡΠΎΠ΄Π΅ ΠΊΠ°ΠΊ Π΄ΠΎΠ»ΠΆΠ½Π° ΠΏΠΎΠΌΠΎΡΡ Ρ ΠΏΠ»Π°Π²Π½ΠΎΡΡΡΡ Π°Π½ΠΈΠΌΠ°ΡΠΈΠΈ // t: current time/currnet step // b: starting position // c: amount of change (end - start) // d: total animation time/steps function easeInOutQuart(t, b, c, d) { if ((t/=d/2) < 1) return c/2*t*t*t*t + b; return -c/2 * ((t-=2)*t*t*t - 2) + b; } let start = 0; let frameRate = 60 / 1000; let duration = 1000; let currentStep = 0; let newStep = 0; let draw = () => { ctx.clearRect(0, 0, canvas.width, canvas.height); currentStep++; newStep = easeInOutQuart(currentStep, start, start - maxPercent, frameRate * duration); //Π·Π°ΠΏΠΈΡΡ Π² ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ ΡΡΠΎ Π±Ρ ΡΠΈΡΠ°Π»ΠΎΡΡ Π»ΡΡΡΠ΅ ctx.beginPath(); ctx.strokeStyle = "hsl(190, 80%," + circleStyle + "%" + ")"; ctx.lineWidth = "30"; ctx.arc(150, 150, 100, 0, min / newStep, false); // ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΡ Π·Π΄Π΅ΡΡ ctx.stroke(); levelOfKnowledge.innerHTML = `${nameOfKnowledge} ${Math.round(min)} %`; min++; circleStyle--; if (min <= maxPercent) { requestAnimationFrame(draw); } else { // alert(`ΠΠΎΠΉ ΡΠΊΠΈΠ»Π» ${nameOfKnowledge} ΡΠ°Π²Π΅Π½ ${maxPercent}% β`) } }; draw();
canvas { border: 1px solid #000; } #percent { color: green; position: absolute; left: 20px; bottom: 20px; } .doghnuts { display: flex; flex-direction: row; align-items: center; position: relative; }
<div class="doghnuts"> <canvas width="350" height="350" id="cvs"></canvas> <div id="percent"></div> </div>
Who is comfortable on the codepen: https://codepen.io/likeavenus/pen/XGdpzN
Ps Found such a function , it seems like it should help, but it does not work as it should. The description says:
t: current time / currnet step
b: starting position
c: amount of change (end - start)
d: total animation time / steps
function easeInOutQuart(t, b, c, d) { if ((t/=d/2) < 1) return c/2*t*t*t*t + b; return -c/2 * ((t-=2)*t*t*t - 2) + b; }