Pomax / bezierjs

A nodejs and client-side library for (cubic) Bezier curve work
MIT License
1.7k stars 230 forks source link

How to make a "strict" version of bezier.project(point)? #199

Open justvanrossum opened 11 months ago

justvanrossum commented 11 months ago

I'd like to implement a version of bezier.project(), that doesn't clamp ft between 0 and 1, but will return a null or undefined result when ft is out of bounds.

My first idea was to subclass Bezier, and add my own method, which would be mostly a copy of project(). However, project() uses utils, which isn't exported.

If you're open to a PR that would make this possible, what would be the best way? I see some options:

  1. export utils
  2. add an optional strict argument to project() that does what I need
  3. add the unclamped ft value as a property to the resulting p object

If you see a different way how I could implement what I need, I'd be all ears.

Thanks for your consideration.

justvanrossum commented 11 months ago

Option three may be the simplest thing to do:

diff --git a/src/bezier.js b/src/bezier.js
index 20b61b3..2d6aa3d 100644
--- a/src/bezier.js
+++ b/src/bezier.js
@@ -295,9 +295,10 @@ class Bezier {
         ft = t;
       }
     }
-    ft = ft < 0 ? 0 : ft > 1 ? 1 : ft;
-    p = this.compute(ft);
-    p.t = ft;
+    t = ft < 0 ? 0 : ft > 1 ? 1 : ft;
+    p = this.compute(t);
+    p.t = t;
+    p.ft = ft;
     p.d = mdist;
     return p;
   }
Pomax commented 11 months ago

I'm open to a PR but only if that functionality is controlled by an argument flag, i.e. project(uncapped = true).