jquery - Parabolic motion with javascript -
i'm making script simulate parabolic motion of ball. not understand i'm wrong, works jquery. think it's problem in frame.
<html> <head> <script> var c = document.getelementbyid("mycanvas"); var ctx = c.getcontext("2d"); var x = 100; function init() { setinterval("draw()", 1); } function draw() { ctx.beginpath(); ctx.arc(x, motopar(x + 2, 0, 100, 100), 30, 0, math.pi * 2); ctx.stroke(); } function motopar(x, x0, vx, vy){ var y; var g = 3; y = -1/2 * g * (x * x) / (vx * vx) + vy / vx * x; return y; } </script> </head> <body> <canvas id="mycanvas" width="600" height="600" style="border: 1px solid black;"> browser not support html5 canvas tag. </canvas> </body> </html>
i able ball moving, though not parabolically.
1 wrap starter code in window.onload , invoke init() inside of (though keep var declarations c, ctx, , x globals -- ie outside of window.onload function)):
var c, ctx, x; window.onload = function() { // <-- invoked when page ready c = document.getelementbyid("mycanvas"); // c null if dom isn't done ctx = c.getcontext("2d"); x = 100; init(); // <-- make sure call :) }
2 pass identifier draw
setinterval (function names variables too)
setinterval(draw, 1);
3 ball drawn won't moving. far motion, i'll leave you, threw in ++x
middle of draw()
function @ least gets ball slide bottom right
Comments
Post a Comment