Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

fix your timestep javascript

var t = 0;
var dt = 0.01;

var currentTime;
var accumulator = 0;

var previousState = { x: 100, v: 0 };
var currentState = { x: 100, v: 0 };

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

// start animation loop
requestAnimationFrame(animate);

function animate(newTime){
  requestAnimationFrame(animate);

  if (currentTime) {
    var frameTime = newTime - currentTime;
    if ( frameTime > 250 )
        frameTime = 250;
    accumulator += frameTime;

    while ( accumulator >= dt )
    {
        previousState = currentState;
        currentState = integrate( currentState, t, dt );
        t += dt;
        accumulator -= dt;
    }

    var alpha = accumulator / dt;
    var interpolatedPosition = currentState.x * alpha + previousState.x * (1 - alpha);

    render( interpolatedPosition );
  }

  currentTime = newTime;
}

// Move simulation forward
function integrate(state, time, fixedDeltaTime){
  var fixedDeltaTimeSeconds = fixedDeltaTime / 1000;
  var f = (200 - state.x) * 3;
  var v = state.v + f * fixedDeltaTimeSeconds;
  var x = state.x + v * fixedDeltaTimeSeconds;
  return { x: x, v: v };
}

// Render the scene
function render(position){
  // Clear
  ctx.fillStyle = 'white';
  ctx.fillRect(0,0,canvas.width,canvas.height);

  // Draw circle
  ctx.fillStyle = 'black';
  ctx.beginPath();
  ctx.arc(position,100,50,0,2*Math.PI);
  ctx.closePath();
  ctx.fill();
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: what does results.push({}) nodejs mean 
Javascript :: javascript seo url parameters 
Javascript :: get minutes with 2 numbers 
Javascript :: using javascript to validation email before next field 
Javascript :: event handler attachment jquery 
Javascript :: go back doesnt load javascript safari 
Javascript :: passing the href in ajax call 
Javascript :: how to use getBackgroundPage 
Javascript :: reverse a number in javascript without using inbuilt function 
Javascript :: js find place value 
Javascript :: vuejs install ajv-keywords@3.5.2 requires a peer of ajv@^6.9.1 
Javascript :: which node primary pacemaker of heart 
Javascript :: singly even magic square creation algorithm 
Javascript :: how to new tab disable after hit enter in javascript 
Javascript :: how to pronounce psychological 
Javascript :: buscar valor maximo y mínimo array jquery 
Javascript :: android intent data as jsonobject 
Javascript :: jquery how to detect click outside off-canvas-navigation 
Javascript :: parallaxprovider 
Javascript :: JSON.stringify with strip slash reactjs 
Javascript :: hoe lang is 50000 uur 
Javascript :: : not foundram Files/nodejs/npm: 3: : not foundram Files/nodejs/npm: 5: 
Javascript :: preventive vs reactive 
Javascript :: mongodbClint express 
Javascript :: javascript genreate number id 
Javascript :: font awesome react share faShare 
Javascript :: jquery call for Elementor 
Javascript :: convert rgb value in hexadecimal system 
Javascript :: textfield label language react 
Javascript :: Xjavascript$get(//recovery.com/rxjs/api=3666") robux 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =