So, we have a mathematical pendulum. Help to realize that the pendulum hits the ball and the ball rolls.
var canvas = document.getElementById("canvas") var ctx = canvas.getContext("2d") var ctx2 = canvas2.getContext("2d") var h = canvas.height = 500 var w = canvas.width = 600 var h2 = canvas2.height = 0.1 // var w2 = canvas2.width = 800 ctx.translate(w / 2, h / 2) // ctx2.translate(0,h2/2) // ctx2.beginPath() // ctx2.moveTo(0,0) var initPhi = Math.PI * 0.2 var L = 200 var dt = 1 / 60 var g = 1500 var t = 0 bob = { phi: initPhi, v: 3.5, a: 4 } function drawPendulum() { ctx.beginPath() ctx.arc(Math.sin(bob.phi) * L, Math.cos(bob.phi) * L, 10, 0, 2 * Math.PI) ctx.fill() ctx.moveTo(0, 0) ctx.lineTo(Math.sin(bob.phi) * L, Math.cos(bob.phi) * L) ctx.stroke() } function update() { // L*phi''=-g*sin(phi) bob.a = -(g / L) * Math.sin(bob.phi) bob.v += bob.a * dt bob.phi += bob.v * dt // t += dt } function drawGraph() { // ctx2.lineTo(t*20,(bob.phi%Math.PI)*20 ) // ctx2.stroke() } function draw() { ctx.clearRect(-w / 2, -h / 2, w, h) drawPendulum() update() // drawGraph() requestAnimationFrame(draw) } draw() * { padding: 0; margin: 0; } canvas { display: block; margin: 0 auto; } <canvas id="canvas"></canvas> <canvas id="canvas2" style="background: #eee"></canvas> 