Skip to content
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

Add TypeScript support #148

Open
frostburn opened this issue May 17, 2022 · 6 comments
Open

Add TypeScript support #148

frostburn opened this issue May 17, 2022 · 6 comments

Comments

@frostburn
Copy link

It would be nice to have a ganja.d.ts file so that the TypeScript compiler can understand what's going on without having to write local type declarations.

@kungfooman
Copy link
Contributor

TypeScript doesn't support operator overloading so far, so even with a 100% complete ganja.d.ts it wouldn't be useful for inline functions without hacking operator overloading into TypeScript itself.

There are JavaScript engines that do in fact support operator overloading (QuickJS), but its far from mainstream yet.

@frostburn
Copy link
Author

That's true. It's unfortunate that power users cannot benefit from TypeScript and inline mode at the same time. However the way I'm using ganja.js in my project is pretty minimalistic so I don't need the inline definitions.

@kungfooman
Copy link
Contributor

I don't know if you started the process already, but a little start:

ganja.d.ts

/**
 * If there is an inline function, it returns whatever that function returns.
 * Otherwise `Algebra` returns `class Element extends generator`
 * 
 * @param p 
 * @param q 
 * @param r 
 * @param f 
 * @returns 
 */
export function Algebra<T>(
  p: number | Options,
  q?: number | (()=>T),
  r?: number | (()=>T),
  f?: ()=>T
): unknown extends T ? typeof GanjaElement : T
declare class MultiVector {
  constructor(a: any);
  Grade(grade: any, res: any): any;
}
/**
 * `class Element extends generator`
 * Issue is name space collision with HTML5 Element
 */
declare class GanjaElement  {

}
interface Options {
  scale?: number;
  conformal?: boolean;
  even?: boolean;
  dual?: number;
  camera?: GanjaElement;
  width?: number;
  height?: number;
  canvas?: HTMLCanvasElement;
  ni?: GanjaElement;
  no?: GanjaElement;
  gl?: boolean; // WebGL
  up?: boolean; // WebGL, return graphGL2
  alpha?: boolean; // WebGL
  spin?: number; // WebGL camera
  h?: number; // WebGL camera
  p?: number; // WebGL camera
  z?: number; // WebGL camera
  posx?: number; // WebGL camera
  posy?: number; // WebGL camera
  posz?: number; // WebGL camera
  maxSteps?: number; // WebGL, default 64
  stepSize?: number; // WebGL, default 0.25
  thresh?: number; // WebGL, default 0.2
  devicePixelRatio?: number; // default 1
  pointRadius?: number; // default 1
  lineWidth?: number; // default 1, doesn't work anyway (WebGL limitation)
  /*
    shader: `
      gl_FragColor =  gl_FragColor*0.2 + 0.4*l*vec4(Col,1.) + 0.7 * vec4(Col,1.);
      float m = mod(Col.r*40.,1.); 
      gl_FragColor += m<0.8?0.:0.3*sin((m-0.8)*5.*3.1415);
    `
  */
  shader?: string; // default '', special shader for programcol
  ipns?: boolean;
  still?: boolean;
  animate?: boolean;
  useUnnaturalLineDisplayForPointPairs?: boolean;
  htmlText?: boolean;
  grid?: boolean;
  metric?: number[];
  Cayley?: string[][];
  basis?: string[];
  q?: number;
  r?: number;
  mix?: boolean;
  graded?: boolean;
  grades?: number[];
  baseType?: Array<number> | Float32ArrayConstructor | Float64ArrayConstructor;
  tot?: number;
  over?: typeof GanjaElement;
  fontSize?: number;
  arrows?: boolean; // graph with arrows or not (looks a bit convoluted in some examples)
  format?: "table" | "canvas";
  master?: boolean;
}

Its absolutely not perfect and I don't have time to add more types right now, but at least a start to flesh out more details whenever you hit a type error.

@frostburn
Copy link
Author

Thanks it's a nice start indeed. Do I need to fork ganja to be able to use these or is there a way to include them in my existing project?

@kungfooman
Copy link
Contributor

Thanks it's a nice start indeed. Do I need to fork ganja to be able to use these or is there a way to include them in my existing project?

Its such a little file, that its best to just add it directly. Then you can easily add types as needed without messing around in node_modules too.

@frostburn
Copy link
Author

Ended up creating my own flavor of ganja.js instead. It has types and should be a bit slimmer for production distributions using tree-shaking:
https://github.com/frostburn/ts-geometric-algebra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants