Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. <template>
  2. <canvas id="timer"></canvas>
  3. </template>
  4.  
  5. <script>
  6. export default {
  7. name: 'Timer',
  8. props: {
  9. backgroundColor: {
  10. type: String,
  11. default: "#ccc"
  12. },
  13. fillColor: {
  14. type: String,
  15. default: "#707070"
  16. },
  17. fontSize: {
  18. type: String,
  19. default: "35px"
  20. },
  21. scale: {
  22. type: Number,
  23. default: 1
  24. },
  25. maxTime: Number,
  26. currentTime: Number
  27. },
  28. data () {
  29. return {
  30. canvas: null,
  31. context: null
  32. }
  33. },
  34. methods: {
  35. createTimer () {
  36. let size = 50 * this.scale
  37. let devicePixelRatio = window.devicePixelRatio
  38. this.canvas = document.getElementById('timer')
  39. this.context = this.canvas.getContext('2d')
  40. this.canvas.style.width = size + 'px'
  41. this.canvas.style.height = size + 'px'
  42. this.canvas.width = size * devicePixelRatio
  43. this.canvas.height = size * devicePixelRatio
  44. this.updateTimer()
  45. // setInterval(this.updateTimer, 100)
  46. },
  47. updateTimer () {
  48. let circleCalc = Math.PI * 2 - (this.currentTime * ((Math.PI * 2)/this.maxTime))
  49.  
  50. this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
  51. if (this.currentTime == this.maxTime) {
  52. this.context.beginPath()
  53. this.context.arc(this.canvas.width/2, this.canvas.height/2, this.canvas.height/2, 0, Math.PI * 2)
  54. this.context.closePath()
  55. this.context.fillStyle = this.backgroundColor
  56. this.context.fill()
  57. }
  58. if (this.currentTime != 0) {
  59. this.context.beginPath()
  60. this.context.moveTo(this.canvas.width/2, this.canvas.height/2)
  61. this.context.arc(this.canvas.width/2, this.canvas.height/2, this.canvas.height/2, 0, circleCalc, true)
  62. this.context.lineTo(this.canvas.width/2, this.canvas.height/2)
  63. this.context.closePath()
  64. this.context.fillStyle = this.backgroundColor
  65. this.context.fill()
  66. }
  67. this.context.beginPath()
  68. this.context.moveTo(this.canvas.width/2, this.canvas.height/2)
  69. this.context.arc(this.canvas.width/2, this.canvas.height/2, this.canvas.height/2, 0, circleCalc)
  70. this.context.lineTo(this.canvas.width/2, this.canvas.height/2)
  71. this.context.closePath()
  72. this.context.fillStyle = this.fillColor
  73. this.context.fill()
  74.  
  75. this.context.fillStyle = 'black'
  76. this.context.font = 'bold ' + this.fontSize + ' Arial'
  77. this.context.textAlign = 'center'
  78. this.context.textBaseline = 'middle'
  79. this.context.fillText(this.currentTime, this.canvas.width/2, this.canvas.height/2)
  80. }
  81. },
  82. watch: {
  83. currentTime: function () {
  84. this.updateTimer()
  85. if (this.currentTime == 0) {
  86. this.$emit('ended')
  87. }
  88. }
  89. },
  90. mounted () {
  91. this.createTimer()
  92. this.$emit('started')
  93. }
  94. }
  95. </script>
  96.  
  97. <style lang="scss" scoped>
  98. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement