beannshie

Clock and Counter

Aug 23rd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <body>
  5.  
  6. <input type="text" id="myText" value="">
  7.  
  8. <button onclick="myFunction()">Start</button>
  9.  
  10. <canvas id="gameCanvas" width="1350" height="600"></canvas>
  11.  
  12. <script>
  13.  
  14. var clock = false;
  15. var counter = false;
  16.  
  17. window.onload = function() {
  18. canvas = document.getElementById('gameCanvas');
  19. canvasContext = canvas.getContext('2d');
  20.  
  21. var framesPerSecond = 1
  22. setInterval(function() {
  23. if(clock == true) {
  24. drawClock();
  25. countSec();
  26. countMin();
  27. countHour();
  28. }
  29. }, 1000/framesPerSecond);
  30.  
  31. var framesPerSecond2 = 30
  32. setInterval(function() {
  33. if(counter == true) {
  34. drawCounter();
  35. count();
  36. }
  37. }, 1000/framesPerSecond2);
  38. }
  39.  
  40. function myFunction() {
  41. var text = document.getElementById("myText").value;
  42. if(text == "clock") {
  43. counter = false;
  44. clock=true;
  45. }
  46.  
  47. if(text == "counter") {
  48. clock = false;
  49. counter=true;
  50. }
  51.  
  52. if(text == "reset clock") {
  53. second = 0;
  54. minute = 0;
  55. hour = 0;
  56. }
  57.  
  58. if(text == "reset counter") {
  59. Count = 0;
  60. }
  61. }
  62.  
  63. function drawRect(X,Y, width,height, color) {
  64. canvasContext.fillStyle = color;
  65. canvasContext.fillRect(X,Y, width,height);
  66. }
  67.  
  68. var second = 0
  69. var minute = 0
  70. var hour = 0
  71.  
  72. var paused = false
  73.  
  74. function countSec() {
  75. second ++
  76. }
  77.  
  78. function countMin() {
  79. if(second == 60) {
  80. second = 0;
  81. minute ++
  82. }
  83. }
  84.  
  85. function countHour() {
  86. if(minute == 60) {
  87. minute = 0;
  88. hour ++
  89. }
  90. }
  91.  
  92.  
  93. function drawClock() {
  94. drawRect(0,0, 750,750, 'white');
  95. canvasContext.font="40px Arial";
  96. canvasContext.fillStyle = 'blue';
  97.  
  98. canvasContext.fillText("Seconds", 50,50);
  99. canvasContext.fillText(second, 230,50);
  100.  
  101. canvasContext.fillText("Minutes", 50,100);
  102. canvasContext.fillText(minute, 230,100);
  103.  
  104. canvasContext.fillText("Hours", 50,150);
  105. canvasContext.fillText(hour, 230,150);
  106.  
  107. canvasContext.fillText(":", 215,50);
  108. canvasContext.fillText(":", 215,100);
  109. canvasContext.fillText(":", 215,150);
  110. }
  111.  
  112. var Count = 0
  113.  
  114. function count() {
  115. console.log(Count);
  116. Count ++
  117. }
  118.  
  119. function drawCounter() {
  120. drawRect(0,0, 750,750, 'white');
  121. canvasContext.font="40px Arial";
  122. canvasContext.fillStyle = 'blue';
  123. canvasContext.fillText("Counter :", 50,50);
  124. canvasContext.fillText(Count, 230,50);
  125. }
  126.  
  127. </script>
  128.  
  129. </body>
  130. </html>
Advertisement
Add Comment
Please, Sign In to add comment