Guest User

Untitled

a guest
Mar 24th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. <meta charset="utf-8">
  2.  
  3. <canvas id="myCanvas" width="800" height="600"></canvas>
  4. <br>
  5. <button onclick="horizLines()">Horizontal</button>
  6. <button onclick="diagonalLines()">Diagonal</button>
  7. <button onclick="clearScreen()">Clear</button>
  8.  
  9.  
  10. <script>
  11. var myCanvas = document.getElementById("myCanvas")
  12. var context = myCanvas.getContext("2d");
  13. var counter = 0;
  14.  
  15. function horizLines(){
  16. var loop = setInterval( ()=>
  17. {
  18. var moveDistance = 20 * counter;
  19. context.beginPath();
  20. context.moveTo(100, 200 + moveDistance);
  21. context.lineTo(120 + moveDistance, 200 + moveDistance);
  22. context.stroke();
  23. counter +=1;
  24. console.log(counter);
  25. if (counter > 10){
  26. counter = 0;
  27. clearInterval(loop);
  28. }
  29. }, 300);
  30. }
  31.  
  32.  
  33. function diagonalLines() {
  34. var drawLine = function(){
  35.  
  36. var moveY = 20 * counter;
  37. var steepness = 50;
  38. context.beginPath();
  39. context.moveTo(100, 100 + moveY)
  40. context.lineTo(400, 100 + moveY + steepness);
  41. context.stroke();
  42. counter +=1;
  43. console.log(counter);
  44.  
  45. if (counter > 10) {
  46. counter = 0;
  47. clearInterval(loop);
  48. }
  49. }
  50.  
  51. var loop = setInterval(drawLine, 300);
  52. }
  53.  
  54.  
  55. function clearScreen() {
  56. context.clearRect(0, 0, myCanvas.width, 600)
  57. }
  58.  
  59.  
  60.  
  61. </script>
Add Comment
Please, Sign In to add comment