javascriptjquerymathquadratic-curve

Quadratic function for increment speedup of countup


I'm writing a jQuery plugin for fast-counting up to a value when a page loads. Since javascript can't run as fast as I want it to for larger numbers, I want to increase the increment step so that it completes within a given timeframe, so I'd need a quadratic function that passes through origo and has it's turning point at y = target counting value and x = target duration, but I can't get a grip on the math for doing this. Since both the number and the duration can change, I need to be able to calculate it in javascript aswell.

Hopefully someone can help me with this one!


Solution

  • // Create a quadratic function that passes through origo and has a given extremum.
    // x and y are the coordinates for the extremum.
    // Returns a function that takes a number and returns a number.
    var quadratic = function (x, y) {
        var a = - (y / (x * x));
        var b = (2 * y) / x;
        return function (x) {
            return a * x * x + b * x;
        };
    };