-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typescript support #65
Comments
Yes I've ported it to TypeScript, what version are you using? |
2.0.3
Then:
|
I'll have to look in to how @types works for v1.8 / v2.0. I'm not sure why it wouldn't be backward compatible. |
I just did an npm install on a fresh clone, no errors. What are the errors you see on npm install ? |
Throws:
|
Can you just |
Tried that (with https://github.com/danmarshall/bezierjs/blob/gh-pages/bezier.d.ts), but it exports |
When I import using
That line compiles (and VS Code hints the right definition):
So the It's just that at runtime I get complains:
If I If I import using
Which seems to be the definition of But then if I try
I get:
|
How about |
I can push the non-working code for you to play with. |
Also are you "packaging" the Node version of bezier-js or are you using a <script> tag ? |
Sure, please push your code someplace. |
It's all within NPM. In package.json:
So |
https://github.com/Izhaki/gefri/tree/bezier-ts If you clone the repository:
should get you going. The actual file to modify is https://github.com/Izhaki/gefri/blob/bezier-ts/src/view/viewees/visibles/path/PathSegments.ts Also note https://github.com/Izhaki/gefri/blob/bezier-ts/src/bezier.d.ts |
I'll take a look. Meanwhile, you might want to see how I do it in Maker.js : https://github.com/Microsoft/maker.js/blob/master/src/models/BezierCurve.ts |
In Maker.js seems to make make use of the global var in the browser. Do you have a non-browser example (ie, Node?) |
By the way, the reason your fork won't install is because the bezier repository hasn't got a master branch. This worked:
But tsc still complains:
|
OK. Have made some progress (with some help from this SO answer). If I just:
Add
And:
I can now call:
Looking at https://github.com/DefinitelyTyped/DefinitelyTyped/blob/types-2.0/bezier-js/index.d.ts, it seems to export a namespace, whereas my |
@Izhaki do you still get Intellisense for the rest of the Bezier properties? @paulvanbrenk should the definition be fixed in the @types repo ? |
Yes, any updates you make to definition should be in the Definitely Typed repo, or you can ship the d.ts file as part of the npm package... and keep them in the bezierjs repo. |
In Maker.js for Node, the same declaration works if a global var Bezier is brought into scope |
@danmarshall If I use my (4 lines) I'm not an expert on typings, but I have a feeling that the @types file simply doesn't need the namespace - it will make it work in node and looking at Maker.js, I'm pretty sure you'll get it working there as well. |
OK, just to confirm.
then if I manually edit Note that when installing via npm, // Type definitions for Bezier.js
// Project: https://github.com/Pomax/bezierjs
// Definitions by: Dan Marshall <https://github.com/danmarshall>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface Point {
x: number;
y: number;
z?: number;
}
interface Projection extends Point {
t?: number;
d?: number;
}
interface Inflection {
x: number[];
y: number[];
z?: number[];
values: number[];
}
interface Offset extends Point {
c: Point;
n: Point;
}
interface Pair {
left: Bezier;
right: Bezier;
}
interface Split extends Pair {
span: Point[];
_t1?: number;
_t2?: number;
}
interface MinMax {
min: number;
mid?: number;
max: number;
size?: number;
}
interface BBox {
x: MinMax;
y: MinMax;
z?: MinMax;
}
interface Line {
p1: Point;
p2: Point;
}
interface Arc extends Point {
e: number;
r: number;
s: number;
}
interface Shape {
startcap: BezierCap;
forward: Bezier;
back: Bezier;
endcap: BezierCap;
bbox: BBox;
intersections: (shape: Shape) => string[][] | number[][];
}
interface ABC {
A: Point;
B: Point;
C: Point;
}
interface Closest {
mdist: number;
mpos: number;
}
/**
* Bezier curve constructor. The constructor argument can be one of three things:
*
* 1. array/4 of {x:..., y:..., z:...}, z optional
* 2. numerical array/8 ordered x1,y1,x2,y2,x3,y3,x4,y4
* 3. numerical array/12 ordered x1,y1,z1,x2,y2,z2,x3,y3,z3,x4,y4,z4
*
*/
declare class Bezier {
private _linear;
clockwise: boolean;
_3d: boolean;
_t1: number;
_t2: number;
_lut: Point[];
dpoints: Point[][];
order: number;
points: Point[];
dims: string[];
dimlen: number;
constructor(points: Point[]);
constructor(coords: number[]);
constructor(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4?: number, y4?: number);
constructor(p1: Point, p2: Point, p3: Point, p4?: Point);
static fromSVG(svgString: string): Bezier;
static getABC(n: number, S: Point, B: Point, E: Point, t: number): ABC;
static quadraticFromPoints(p1: Point, p2: Point, p3: Point, t: number): Bezier;
static cubicFromPoints(S: Point, B: Point, E: Point, t: number, d1: number): Bezier;
static getUtils(): typeof utils;
getUtils(): typeof utils;
valueOf(): string;
toString(): string;
toSVG(): string;
update(): void;
computedirection(): void;
length(): number;
getLUT(steps?: number): Point[];
on(point: Point, error: number): number;
project(point: Point): Projection;
get(t: number): Point;
point(idx: number): Point;
compute(t: number): Point;
raise(): Bezier;
derivative(t: number): Point;
inflections(): number[];
normal(t: number): Point;
private __normal2(t);
private __normal3(t);
private __normal(t);
hull(t: number): Point[];
split(t1: number): Split;
split(t1: number, t2: number): Bezier;
extrema(): Inflection;
bbox(): BBox;
overlaps(curve: Bezier): boolean;
offset(t: number, d?: number): Offset | Bezier[];
simple(): boolean;
reduce(): Bezier[];
scale(d: Function): Bezier;
scale(d: number): Bezier;
outline(d1: number, d2?: number, d3?: number, d4?: number): PolyBezier;
outlineshapes(d1: number, d2: number, curveIntersectionThreshold?: number): Shape[];
intersects(curve: Bezier, curveIntersectionThreshold?: number): string[] | number[];
intersects(curve: Line): string[] | number[];
lineIntersects(line: Line): number[];
selfintersects(curveIntersectionThreshold?: number): string[];
curveintersects(c1: Bezier[], c2: Bezier[], curveIntersectionThreshold?: number): string[];
arcs(errorThreshold?: number): Arc[];
private _error(pc, np1, s, e);
private _iterate(errorThreshold, circles);
}
declare class BezierCap extends Bezier {
virtual: boolean;
}
declare namespace utils {
var Tvalues: number[];
var Cvalues: number[];
function arcfn(t: number, derivativeFn: Function): number;
function between(v: number, m: number, M: number): boolean;
function approximately(a: number, b: number, precision?: number): boolean;
function length(derivativeFn: Function): number;
function map(v: number, ds: number, de: number, ts: number, te: number): number;
function lerp(r: number, v1: Point, v2: Point): Point;
function pointToString(p: Point): string;
function pointsToString(points: Point[]): string;
function copy(obj: Object): any;
function angle(o: Point, v1: Point, v2: Point): number;
function round(v: number, d: number): number;
function dist(p1: Point, p2: Point): number;
function closest(LUT: Point[], point: Point): Closest;
function abcratio(t: number, n: number): number;
function projectionratio(t: number, n: number): number;
function lli8(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number, x4: number, y4: number): Point;
function lli4(p1: Point, p2: Point, p3: Point, p4: Point): Point;
function lli(v1: Offset, v2: Offset): Point;
function makeline(p1: Point, p2: Point): Bezier;
function findbbox(sections: Bezier[]): BBox;
function shapeintersections(s1: Shape, bbox1: BBox, s2: Shape, bbox2: BBox, curveIntersectionThreshold?: number): string[][] | number[][];
function makeshape(forward: Bezier, back: Bezier, curveIntersectionThreshold?: number): Shape;
function getminmax(curve: Bezier, d: string, list: number[]): MinMax;
function align(points: Point[], line: Line): Point[];
function roots(points: Point[], line: Line): number[];
function droots(p: number[]): number[];
function inflections(points: Point[]): number[];
function bboxoverlap(b1: BBox, b2: BBox): boolean;
function expandbox(bbox: BBox, _bbox: BBox): void;
function pairiteration(c1: Bezier, c2: Bezier, curveIntersectionThreshold?: number): string[];
function getccenter(p1: Point, p2: Point, p3: Point): Arc;
}
/**
* Poly Bezier
* @param {[type]} curves [description]
*/
declare class PolyBezier {
curves: Bezier[];
private _3d;
points: Point[];
constructor(curves: Bezier[]);
valueOf(): string;
toString(): string;
addCurve(curve: Bezier): void;
length(): number;
curve(idx: number): Bezier;
bbox(): BBox;
offset(d: number): PolyBezier;
}
declare module "bezier-js" {
export = Bezier
} |
I don't think the lib exports the PolyBezier constructor, it's just there for declaring the result of Bezier.outline() |
It's not exposed for a really simple reason: it's a shim at best. It's not a true PolyBezier that does proper on/off-curve point manipulation, etc. It's just an array of Beziers, with the smallest number of fall-through functions to make working with the result from |
Has this been fixed? I was unable to get this working too... I can use the library but typescript won't resolve the types. Is it possible there is a bug in the DefinitelyTyped definitions? |
@alexjlockwood In the interim, have a look how I got it working: https://github.com/Izhaki/gefri/blob/master/src/view/viewees/visibles/path/bezier-js.d.ts |
@Izhaki since you got it working would you mind sending a PR to the DefinitelyTyped repo? |
@danmarshall Sure. But will need to find a bit of time (to test it all). |
@Izhaki @alexjlockwood the declaration file seems to have been fixed in https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/bezier-js/index.d.ts |
hi |
@vjonas |
@Izhaki |
@vjonas you should now be able to |
The problem is still there. @tooxoot seems to have a solution in the reference above. The way I solved it without replacing the d.ts file is by importing the type and the constructor separately:
But this really is a bandaid fix |
Had major issues with trying to get this to work as I cannot use require for my current project. Is there a fix that works for standard import statements yet? Seemingly the problem I am seeing so far is that the Javascript file: 'bezier-js/lib/bezier.js' has:
Then when using:
I get error saying that Bezier does not exist on BezierJS. logging BezierJs is resolving to the class Bezier instead of a namespace/module object |
The immediate question is: does |
Problem still remains, exactly as described by @Izhaki .
|
Digging into this, I've learned that importing CommonJS with TypeScript uses the following format: Note that the ES6 Also just to be clear, we are only talking about TypeScript and not any other dialect like Babel. Here's a couple more discussions regarding this "legacy" syntax:
Now, there is a fault in the current declaration file: the final bit should look like this:
When this gets fixed (meaning a PR needs to be filed, merged, then deployed to npm/@types/bezier-js as part of the process of DefinitelyTyped) this will enable the import statement to look like this:
But, IMHO, this legacy syntax is a little muddy - one thinks they are using ES6 modules, but
Lastly, I just want to thank Mike @Pomax for enduring this discussion. This repo is about Bezier curve stuff. Discussions about TypeScript typings belong in the DefinitelyTyped GitHub repo. If there is a problem with the typings, everyone is free to send a PR to fix them up. |
This most recent comment does not seem to be a solution. My question is why doesn't this work:
then
or
Opening an issue in DefinitelyTyped. |
Now that version 3 of bezier-js has been released (cheers, @Pomax!) the existing typings also need an upgrade. |
Seems like this project lacks a reliable typescript binding. |
Correct, because I don't use TypeScript, so any TS support is community driven. |
Hi @Pomax, are you open for transforming this project to Typescript? I'm in the middle of rewriting it just for fun, and wanted to check if you're okay with a PR :) I believe keeping type definitions right with the project is a better way, than waiting for someone else to double check and rewrite the types manually in some other place. |
I am not: I have no interest in writing or using TS. |
Hi @Pomax @danmarshall I am using brezier-js as v6.1.4 with @@types/bezier-js as 4.1.3. I am trying to calculate curvature for 3d as below
|
@iamravisingh you might want to take a look at the types at DefinitelyTyped and see if they need to be updated. |
Having spent futile 4 hours trying to use this library in a typescript code base (ES6 module imports), I wonder if anyone has managed, and show how.
Seems like @danmarshall did some work on this, but none seem to work.
There's this typescript fork/port, which I couldn't install at all - it just throws errors when you run
npm install
; it is also at 2.1.0 at the moment.Then, there's the typings definitions, and an npm package for @types/bezier-js, but the devil will take me I only get compilation/run-time errors with these.
Anyone?
The text was updated successfully, but these errors were encountered: