Guest User

Untitled

a guest
Aug 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. function radians(degree) {
  2. return (180+degree)*(Math.PI/180)
  3. }
  4.  
  5.  
  6. class ProgressArc {
  7. constructor(parent_selector, size, title){
  8. this.parent = parent_selector
  9. this.w = size
  10. this.h = size
  11. this.config = {
  12. 'title':title,
  13. 'x': this.w/2,
  14. 'y': this.h/2,
  15. 'thickness':this.h/40,
  16. 'radius':this.h*3/7,
  17. 'bg':"#d6d6d6",
  18. 'fg': "blue",
  19. 'depth':50,
  20. }
  21. this.canvas = document.createElement('canvas')
  22. this.canvas.width = this.w
  23. this.canvas.height = this.h
  24. var p = document.querySelector(this.parent)
  25. this.canvas.id = p.id+"-canvas"
  26. p.appendChild(this.canvas)
  27. this.createContext()
  28. }
  29.  
  30. createContext() {
  31. var ctx = this.canvas.getContext('2d')
  32. ctx.clearRect(0,0,this.w,this.h)
  33. ctx.lineWidth=this.config.thickness;
  34. // ctx.arc(x, y, radius, startAngle, endAngle [, anticlockwise]);
  35. ctx.beginPath()
  36. ctx.strokeStyle = this.config.bg
  37. ctx.arc(this.config.x, this.config.y, this.config.radius, this.scale(0), this.scale(100))
  38. ctx.stroke()
  39. ctx.closePath()
  40. if (this.config.title!==undefined) {
  41. ctx.font = this.h/12+'px sans-serif'
  42. var textX = this.config.x-(ctx.measureText(this.config.title).width/2)
  43. var textY = this.config.y + this.h/4
  44. ctx.fillText(this.config.title, textX, textY, this.w)
  45. }
  46. return ctx
  47. }
  48.  
  49.  
  50. scale(val) {
  51. val = Math.min(100, Math.max(0, val))
  52. return radians((val * (1.80+(2*this.config.depth/100)))-this.config.depth)
  53. }
  54.  
  55. set(val) {
  56. var ctx = this.createContext()
  57. ctx.beginPath()
  58. ctx.strokeStyle = this.config.fg
  59. ctx.arc(this.config.x, this.config.y, this.config.radius, this.scale(0), this.scale(val))
  60. ctx.stroke()
  61. ctx.closePath()
  62. var text = Math.round(val).toString()
  63. ctx.font = this.h/5+'px sans-serif'
  64. var textX = this.config.x-(ctx.measureText(text).width/2)
  65. var textY = this.config.y + this.h/15
  66. ctx.fillText(text, textX, textY)
  67. }
  68. }
  69.  
  70. // Usage:
  71. // arc = new ProgressArc('#arc-test', 300, 'Completed')
  72. // arc.set(10)
  73. // arc.set(90)
Add Comment
Please, Sign In to add comment