diff --git a/demo/usage-notes.html b/demo/usage-notes.html index d9fb80e..061043b 100644 --- a/demo/usage-notes.html +++ b/demo/usage-notes.html @@ -198,6 +198,11 @@

Linear

$$\textrm{where } x = [-1,1] \textrm{ and } f(x) = [0,1]$$ +
var linear = function(delta, threshold) {
+	if (delta >= threshold) return 1;
+	if (delta <= -threshold) return 0;
+	return 0.5 * (delta/threshold + 1);
+};

The simplest of all, this function simply transforms $x$ linearly, from its original range of $[-1,1]$, to $f(x)$ with a range of $[0,1]$. This is the default smoothing function (a misnomer, actually, since it is not smoothing anything) for cursor position.

  • @@ -213,6 +218,11 @@

    Tangent

    $$\textrm{where } x = [-1,1] \textrm{ and } f(x) = [0,1]$$ +
    var tangent = function(delta, threshold) {
    +	if (delta >= threshold) return 1;
    +	if (delta <= -threshold) return 0;
    +	return 0.5 * (0.5 * Math.tan((delta/threshold) * (Math.PI * 0.351)) + 1);
    +};

    This function transforms $x$ in the range of $[-1,1]$ via a tangent function. Since the transformation of the tangent curve is imprecise and only down to three decimal places, the range of $f(x)$ lies approximately between $[0,1]$.

  • @@ -228,6 +238,11 @@

    Cosine

    $$\textrm{where } x = [-1,1] \textrm{ and } f(x) = [0,1]$$ +
    var cosine: function(delta, threshold) {
    +	if (delta >= threshold) return 1;
    +	if (delta <= -threshold) return 0;
    +	return 0.5 * (Math.sin((delta/threshold) * (Math.PI/2)) + 1);
    +};

    This function transforms $x$ in the range of $[-1,1]$ via a cosine function. The output, $f(x)$, again lies in the range of $[0,1]$.

  • @@ -251,6 +266,25 @@

    Gaussian

    $$ +
    var normalcdf = function(mean, sigma, to) {
    +	var z = (to-mean)/Math.sqrt(2*sigma*sigma),
    +		t = 1/(1+0.3275911*Math.abs(z)),
    +		a1 =  0.254829592,
    +		a2 = -0.284496736,
    +		a3 =  1.421413741,
    +		a4 = -1.453152027,
    +		a5 =  1.061405429,
    +		erf = 1-(((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*Math.exp(-z*z),
    +		sign = 1;
    +	if(z < 0) sign = -1;
    +	return (1/2)*(1+sign*erf);
    +};
    +
    +var gaussian = function(delta, threshold) {
    +	if (delta >= threshold) return 1;
    +	if (delta <= -threshold) return 0;
    +	return normalcdf(0, 0.375, delta/threshold);
    +}

    This function is a simplified equation of the cumulative distribution function of a normal distribution. This is the default smoothing function for device tilt angles, provided by the gyroscope (if present). Note that since the error function $\textrm{erf}()$ is simply an approximation (code adapted from a StackOverflow answer), the range of $f(x)$ will only be accurate to at most 9 decimal points (which is more than sufficient, anyway).

    diff --git a/jquery.paver.js b/jquery.paver.js index b33a679..eb35857 100644 --- a/jquery.paver.js +++ b/jquery.paver.js @@ -354,15 +354,15 @@ //// ------------------ //// // Adapted from http://stackoverflow.com/questions/5259421/cumulative-distribution-function-in-javascript normalcdf: function(mean, sigma, to) { - var z = (to-mean)/Math.sqrt(2*sigma*sigma); - var t = 1/(1+0.3275911*Math.abs(z)); - var a1 = 0.254829592; - var a2 = -0.284496736; - var a3 = 1.421413741; - var a4 = -1.453152027; - var a5 = 1.061405429; - var erf = 1-(((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*Math.exp(-z*z); - var sign = 1; + var z = (to-mean)/Math.sqrt(2*sigma*sigma), + t = 1/(1+0.3275911*Math.abs(z)), + a1 = 0.254829592, + a2 = -0.284496736, + a3 = 1.421413741, + a4 = -1.453152027, + a5 = 1.061405429, + erf = 1-(((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*Math.exp(-z*z), + sign = 1; if(z < 0) sign = -1; return (1/2)*(1+sign*erf); },