scoreCirle.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. class ScoreCirle {
  2. constructor(context: CanvasRenderingContext2D, config: {
  3. score: number,
  4. total: number,
  5. lineWidth:number
  6. fillColor?:string
  7. strokeColor?:string
  8. }) {
  9. this.lineWidth=config.lineWidth;
  10. this.rad = (Math.PI*2)/config.total
  11. this.strokeColor = config.strokeColor||"#2589fa"
  12. this.context = context
  13. this.score = config.score
  14. this.total = config.total
  15. this.width = context.canvas.width
  16. this.height = context.canvas.height
  17. this.radius1 = (context.canvas.width/2)-15
  18. this.radius2 = (context.canvas.width/2)-30
  19. }
  20. total: number
  21. rad:number
  22. lineWidth:number
  23. radius2: number
  24. score: number
  25. context:CanvasRenderingContext2D
  26. width:number
  27. height:number
  28. radius1:number
  29. strokeColor:string
  30. init() {
  31. this.context.save();
  32. this.context.lineCap = "round";
  33. this.context.lineWidth=this.lineWidth
  34. this.context.strokeStyle=this.strokeColor
  35. this.context.beginPath();
  36. this.context.arc(this.width/2,this.height/2,this.radius1, -Math.PI / 2, (-Math.PI / 2)+this.score*this.rad,false);
  37. this.context.moveTo(this.width/2,this.height/2);
  38. this.context.textBaseline = "middle"
  39. this.context.font = "45pt sans-serif";
  40. this.context.textAlign="center"
  41. this.context.fillStyle = "#333333"
  42. this.context.fillText(`${this.score}.00`,this.width/2,this.height/2)
  43. this.context.fillStyle = "#808080"
  44. this.context.font = "15pt sans-serif";
  45. this.context.fillText(`/${this.total}`,this.width/2,this.height/2+50)
  46. this.context.stroke();
  47. this.context.restore();
  48. }
  49. }
  50. export default ScoreCirle