Created
December 11, 2018 09:39
-
-
Save dtanphat9388/ee4bca035c8a16cce7e62b8eb00b9327 to your computer and use it in GitHub Desktop.
draw circle process indicator by canvas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let canvas = document.querySelector('#canvas'); // id of canvas element | |
let ctx = canvas.getContext('2d') | |
ctx.lineWidth = 10; | |
ctx.lineCap = "round" | |
let process = 0 | |
let middle = canvas.width * 0.5; | |
function draw(process) { | |
console.log(process) | |
if (Number.parseInt(process) == 1) return; | |
process += 0.01 | |
requestAnimationFrame(draw) | |
ctx.save() | |
ctx.translate(middle, middle) | |
ctx.rotate(-Math.PI * 0.5) | |
ctx.translate(-middle, -middle) | |
ctx.clearRect(0,0,300,300) | |
ctx.beginPath(); | |
ctx.arc(middle, middle, width, 0, process * Math.PI * 2) | |
ctx.strokeStyle = `rgb(${255 - process * 255 },${process*255},0)` | |
ctx.stroke() | |
ctx.closePath() | |
ctx.restore() | |
} | |
draw() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment