Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var canvas = document.getElementById('canvas')
  2. var context = canvas.getContext('2d')
  3.  
  4. function line(x1, y1, x2, y2) {
  5.     context.beginPath()
  6.     context.moveTo(x1, y1)
  7.     context.lineTo(x2, y2)
  8.     context.stroke()
  9. }
  10.  
  11. function circle(x, y, r) {
  12.     context.beginPath()
  13.     context.arc(x, y, r, 0, Math.PI * 2)
  14.     context.stroke()
  15. }
  16.  
  17. function ball(x, y, r) {
  18.     context.beginPath()
  19.     context.arc(x, y, r, 0, Math.PI * 2)
  20.     context.fill()
  21. }
  22.  
  23. context.fillStyle = '#FFDAB9'
  24. context.fillRect(0, 0, 500, 700)
  25.  
  26. var r = 30
  27. var x = r
  28. var y = r
  29. var dx = 2
  30. var dy = 2
  31. var dr = 1
  32. var minr = 30
  33. var maxr = 50
  34.  
  35. var storona = 50
  36. var a = 15
  37. var da = 2
  38. var b = 15
  39. var db = 2
  40.  
  41. function draw() {
  42.     window.requestAnimationFrame(draw)
  43.  
  44.     context.fillStyle = '#FFDAB9'
  45.     context.fillRect(0, 0, 500, 700)
  46.  
  47.     if (Math.sqrt((a - x) * (a - x) + (b - y) * (b - y)) < r) {
  48.         circle(x, y, r)
  49.         line(a, b, a + storona, b)
  50.         line(a + storona, b, a + storona, b + storona)
  51.         line(a + storona, b + storona, a, b + storona)
  52.         line(a, b + storona, a, b)
  53.     } else {
  54.         context.fillStyle = 'red'
  55.         context.fillRect(a, b, storona, storona)
  56.  
  57.         circle(x, y, r)
  58.     }
  59.     x = x + dx
  60.     y = y + dy
  61.     r = r + dr
  62.    
  63.     console.log("" + r + " " + dr)
  64.     if (r == minr) {
  65.         dr++
  66.     }
  67.     if (r == maxr) {
  68.         dr--
  69.     }
  70.  
  71.     if (x < r || x > 500 - r) {
  72.         dx = -dx
  73.  
  74.     }
  75.     if (y < r || y > 700 - r) {
  76.         dy = -dy
  77.     }
  78.  
  79.     while (x < r || x > 500 - r) {
  80.         x += dx
  81.     }
  82.     while (y < r || y > 700 - r) {
  83.         y += dy
  84.     }
  85.  
  86.     a = a + da
  87.     b = b + db
  88.  
  89.     if (a < 0 || a > 500 - storona) {
  90.         da = -da
  91.     }
  92.  
  93.     if (b < 0 || b > 700 - storona) {
  94.         db = -db
  95.     }
  96. }
  97. draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement