Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

208
Views
Trying to invert a easing curve

I'm currently working with canvas, i move an object from left to right perfectly through this easing function :

export const easeInQuad = (t, b, c, d) => {
  return c * (t /= d) * t + b;
};

I pass as parameter the following : elapsedTime (ms), beginningValue (px), totalChange (beginning - ending in px), totalDuration (ms)

It works perfectly in the two side EXCEPT when the side changes, it makes a double dash.

Do I mess something ?

Here is where we found the function : https://riptutorial.com/html5-canvas/example/18488/easing-using-robert-penners-equations

Here is the full animation function :

requestAnimationFrame(animate);

function animate(time) {
  ctx.clearRect(0, 0, canvasWidth, canvasHeight);
  ctx.beginPath();

  for (let [playerKey, player] of Object.entries(room.players)) {
    beginningValue = cache[playerKey].x * areaX;
    endingValue = room.players[playerKey].x * areaX;
    totalChange = endingValue - beginningValue;

    let height = 0;
    if (playerKey === "p" + (local.playerID + 1)) {
      height = canvasHeight - paddleHeight;
      ctx.fillStyle = "#56dbd4";
    } else {
      ctx.fillStyle = "#d86ef0";
    }
    if (!startTime) {
      startTime = time;
    }
    let elapsedTime = Math.min(time - startTime, totalDuration);
    let easedX;
    console.log(beginningValue + " --- to --- " + endingValue);
    if (endingValue > beginningValue) {
      easedX = easeInQuad(
        elapsedTime,
        beginningValue,
        totalChange,
        totalDuration
      );
    } else if (endingValue < beginningValue) {
      easedX = easeInQuad(
        elapsedTime,
        endingValue,
        totalChange,
        totalDuration
      );
    } else {
      easedX = endingValue;
    }
    if (easedX > endingValue) {
      easedX = endingValue;
    }
    ctx.rect(easedX - paddleWidth / 2, height, paddleWidth, paddleHeight);
    ctx.fill();
    if (time < startTime + totalDuration) {
      requestAnimationFrame(animate);
    }
  }
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to change final condition to check if never pass limit :

if(beginningValue > endingValue) {
  if(easedX<endingValue){
    easedX=endingValue;
  }
} else {
  if(easedX>endingValue){
    easedX=endingValue;
  }
}

Check this codepen : https://codepen.io/fernandezremi/pen/XWeoejW

Thanks to @Trehinos for his help and this answer (on Discord "Les joies du code")

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!