@mapbox/unitbezier
Advanced tools
Comparing version 0.0.0 to 0.0.1
115
index.js
@@ -1,28 +0,2 @@ | ||
/* | ||
* Copyright (C) 2008 Apple Inc. All Rights Reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY | ||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR | ||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* Ported from Webkit | ||
* http://svn.webkit.org/repository/webkit/trunk/Source/WebCore/platform/graphics/UnitBezier.h | ||
*/ | ||
'use strict'; | ||
@@ -42,3 +16,3 @@ module.exports = UnitBezier; | ||
this.p1x = p1x; | ||
this.p1y = p2y; | ||
this.p1y = p1y; | ||
this.p2x = p2x; | ||
@@ -48,60 +22,59 @@ this.p2y = p2y; | ||
UnitBezier.prototype.sampleCurveX = function(t) { | ||
// `ax t^3 + bx t^2 + cx t' expanded using Horner's rule. | ||
return ((this.ax * t + this.bx) * t + this.cx) * t; | ||
}; | ||
UnitBezier.prototype = { | ||
sampleCurveX: function (t) { | ||
// `ax t^3 + bx t^2 + cx t' expanded using Horner's rule. | ||
return ((this.ax * t + this.bx) * t + this.cx) * t; | ||
}, | ||
UnitBezier.prototype.sampleCurveY = function(t) { | ||
return ((this.ay * t + this.by) * t + this.cy) * t; | ||
}; | ||
sampleCurveY: function (t) { | ||
return ((this.ay * t + this.by) * t + this.cy) * t; | ||
}, | ||
UnitBezier.prototype.sampleCurveDerivativeX = function(t) { | ||
return (3.0 * this.ax * t + 2.0 * this.bx) * t + this.cx; | ||
}; | ||
sampleCurveDerivativeX: function (t) { | ||
return (3.0 * this.ax * t + 2.0 * this.bx) * t + this.cx; | ||
}, | ||
UnitBezier.prototype.solveCurveX = function(x, epsilon) { | ||
if (typeof epsilon === 'undefined') epsilon = 1e-6; | ||
solveCurveX: function (x, epsilon) { | ||
if (epsilon === undefined) epsilon = 1e-6; | ||
var t0, t1, t2, x2, i; | ||
if (x < 0.0) return 0.0; | ||
if (x > 1.0) return 1.0; | ||
// First try a few iterations of Newton's method -- normally very fast. | ||
for (t2 = x, i = 0; i < 8; i++) { | ||
var t = x; | ||
x2 = this.sampleCurveX(t2) - x; | ||
if (Math.abs(x2) < epsilon) return t2; | ||
// First try a few iterations of Newton's method - normally very fast. | ||
for (var i = 0; i < 8; i++) { | ||
var x2 = this.sampleCurveX(t) - x; | ||
if (Math.abs(x2) < epsilon) return t; | ||
var d2 = this.sampleCurveDerivativeX(t2); | ||
if (Math.abs(d2) < 1e-6) break; | ||
var d2 = this.sampleCurveDerivativeX(t); | ||
if (Math.abs(d2) < 1e-6) break; | ||
t2 = t2 - x2 / d2; | ||
} | ||
t = t - x2 / d2; | ||
} | ||
// Fall back to the bisection method for reliability. | ||
t0 = 0.0; | ||
t1 = 1.0; | ||
t2 = x; | ||
// Fall back to the bisection method for reliability. | ||
var t0 = 0.0; | ||
var t1 = 1.0; | ||
t = x; | ||
if (t2 < t0) return t0; | ||
if (t2 > t1) return t1; | ||
for (i = 0; i < 20; i++) { | ||
x2 = this.sampleCurveX(t); | ||
if (Math.abs(x2 - x) < epsilon) break; | ||
while (t0 < t1) { | ||
if (x > x2) { | ||
t0 = t; | ||
} else { | ||
t1 = t; | ||
} | ||
x2 = this.sampleCurveX(t2); | ||
if (Math.abs(x2 - x) < epsilon) return t2; | ||
if (x > x2) { | ||
t0 = t2; | ||
} else { | ||
t1 = t2; | ||
t = (t1 - t0) * 0.5 + t0; | ||
} | ||
t2 = (t1 - t0) * 0.5 + t0; | ||
return t; | ||
}, | ||
solve: function (x, epsilon) { | ||
return this.sampleCurveY(this.solveCurveX(x, epsilon)); | ||
} | ||
// Failure. | ||
return t2; | ||
}; | ||
UnitBezier.prototype.solve = function(x, epsilon) { | ||
return this.sampleCurveY(this.solveCurveX(x, epsilon)); | ||
}; |
{ | ||
"name": "@mapbox/unitbezier", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "unit bezier curve interpolation", | ||
"main": "index.js", | ||
"typings": "index.d.ts", | ||
"scripts": { | ||
"test": "tap --coverage test/*.js" | ||
"pretest": "eslint index.js test/*.js", | ||
"test": "node test/unitbezier.js" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"repository": { | ||
@@ -26,10 +32,9 @@ "type": "git", | ||
"devDependencies": { | ||
"cz-conventional-changelog": "1.2.0", | ||
"tap": "~9.0.3" | ||
"eslint": "^8.0.1", | ||
"eslint-config-mourner": "^2.0.3", | ||
"tape": "^5.3.1" | ||
}, | ||
"config": { | ||
"commitizen": { | ||
"path": "./node_modules/cz-conventional-changelog" | ||
} | ||
"eslintConfig": { | ||
"extends": "mourner" | ||
} | ||
} |
@@ -15,10 +15,4 @@ [![Build Status](https://travis-ci.org/mapbox/unitbezier.svg)](https://travis-ci.org/mapbox/unitbezier) | ||
### bezier.sampleCurveX(t) | ||
### bezier.solve(x, epsilon) | ||
### bezier.sampleCurveY(t) | ||
### bezier.sampleCurveDerivativeX(t) | ||
### bezier.solveCurveX(t) | ||
### bezier.solve(x, epsilon) | ||
Evaluate bezier for value `x` (ranging from 0 to 1) with `epsilon` precision (1e-6 by default). |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
5015
3
67
18
1