modern-canvas 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "modern-canvas",
3
3
  "type": "module",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "packageManager": "pnpm@7.26.2",
6
6
  "description": "A data driven modern canvas library",
7
7
  "author": "wxm",
package/types/app.d.ts ADDED
@@ -0,0 +1,63 @@
1
+ import type { QueryOptions } from './query';
2
+ import type { RenderNodeOptions } from './render';
3
+ import type { System } from './system';
4
+ import type { Texture } from './texture';
5
+ import type { Shape, UserShape } from './shape';
6
+ import type { Node } from './node';
7
+ import type { Material, UserMaterial } from './material';
8
+ import type { Plugin } from './plugin';
9
+ import type { Container } from './container';
10
+ export interface App extends Container {
11
+ view: HTMLCanvasElement;
12
+ children: Node[];
13
+ context: WebGLRenderingContext;
14
+ defaultTexture: WebGLTexture;
15
+ framebuffers: {
16
+ buffer: WebGLFramebuffer | null;
17
+ depthBuffer: WebGLRenderbuffer | null;
18
+ texture: WebGLTexture | null;
19
+ resize(): void;
20
+ }[];
21
+ drawModes: {
22
+ points: GLenum;
23
+ linear: GLenum;
24
+ triangles: GLenum;
25
+ triangleStrip: GLenum;
26
+ triangleFan: GLenum;
27
+ };
28
+ slTypes: Record<number, 'float' | 'vec2' | 'vec3' | 'vec4' | 'int' | 'ivec2' | 'ivec3' | 'ivec4' | 'uint' | 'uvec2' | 'uvec3' | 'uvec4' | 'bool' | 'bvec2' | 'bvec3' | 'bvec4' | 'mat2' | 'mat3' | 'mat4' | 'sampler2D' | 'samplerCube' | 'sampler2DArray'>;
29
+ extensions: {
30
+ loseContext: WEBGL_lose_context | null;
31
+ };
32
+ width: number;
33
+ height: number;
34
+ plugins: Map<string, Plugin>;
35
+ shapes: Map<string, Shape>;
36
+ registerShape(name: string, shape: UserShape): void;
37
+ materials: Map<string, Material>;
38
+ registerMaterial(name: string, material: UserMaterial): void;
39
+ textures: Map<number, Texture>;
40
+ registerTexture(id: number, source: TexImageSource): Promise<void>;
41
+ lastState?: {
42
+ material: string;
43
+ shape: string;
44
+ };
45
+ entities: Map<number, {
46
+ path: number[];
47
+ archetypeId: string;
48
+ }>;
49
+ components: Map<string, number>;
50
+ archetypes: Map<string, {
51
+ entityIds: Set<number>;
52
+ codePoints: number[];
53
+ }>;
54
+ systems: System[];
55
+ registerSystem(system: System): void;
56
+ query(where?: QueryOptions): Node;
57
+ setup(): void;
58
+ load(): Promise<void>;
59
+ renderNode(options: RenderNodeOptions): void;
60
+ render(time?: number): void;
61
+ start(): void;
62
+ destroy(): void;
63
+ }
@@ -9,6 +9,5 @@ export interface Container {
9
9
  set<T = any>(key: string, value: T): void;
10
10
  bind<T = any>(key: string, value: () => T, shared?: boolean): void;
11
11
  singleton<T = any>(key: string, value: () => T): void;
12
- [key: string]: any;
13
12
  }
14
13
  export declare function createContainer(): Container;
@@ -0,0 +1,3 @@
1
+ import type { Options } from './options';
2
+ import type { App } from './app';
3
+ export declare function createApp(options?: Options): App;
package/types/index.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  export * from './plugins';
2
- export * from './canvas';
2
+ export * from './app';
3
+ export * from './create-app';
4
+ export type { Node } from './node';
@@ -1,26 +1,24 @@
1
- import type { GlSlType } from './gl';
2
- import type { Canvas } from './canvas';
3
- export interface Material {
4
- name: string;
1
+ import type { App } from './app';
2
+ export interface UserMaterial {
5
3
  vertexShader: string;
6
4
  fragmentShader: string;
7
5
  uniforms?: Record<string, any>;
8
6
  }
9
- export type InternalMaterial = Material & {
7
+ export type Material = UserMaterial & {
10
8
  program: WebGLProgram;
11
9
  attributes: Record<string, MaterialAttribute>;
12
10
  uniforms: Record<string, MaterialUniform>;
13
- setAttributes(attributes: Record<string, any>): void;
14
- setUniforms(uniforms: Record<string, any>): void;
11
+ setupAttributes(attributes: Record<string, any>): void;
12
+ setupUniforms(uniforms: Record<string, any>): void;
15
13
  };
16
14
  export interface MaterialAttribute {
17
- type: GlSlType;
15
+ type: App['slTypes'][keyof App['slTypes']];
18
16
  isArray: boolean;
19
17
  location: GLint;
20
18
  }
21
19
  export interface MaterialUniform {
22
- type: GlSlType;
20
+ type: App['slTypes'][keyof App['slTypes']];
23
21
  isArray: boolean;
24
22
  location: WebGLUniformLocation | null;
25
23
  }
26
- export declare function registerMaterial(canvas: Canvas, material: Material): void;
24
+ export declare function registerMaterial(app: App, name: string, userMaterial: UserMaterial): void;
package/types/node.d.ts CHANGED
@@ -1,10 +1,4 @@
1
- import type { Canvas } from './canvas';
2
1
  export interface Node {
3
2
  [key: string]: any;
4
- x?: number;
5
- y?: number;
6
- width?: number;
7
- height?: number;
8
3
  children?: Node[];
9
4
  }
10
- export declare function forEachNode(canvas: Canvas, callbackFn: (node: Node, path: number[]) => void): void;
@@ -0,0 +1,7 @@
1
+ import type { Node } from './node';
2
+ import type { Plugin } from './plugin';
3
+ export interface Options extends WebGLContextAttributes {
4
+ view?: HTMLCanvasElement;
5
+ children?: Node[];
6
+ plugins?: Plugin[];
7
+ }
package/types/plugin.d.ts CHANGED
@@ -1,8 +1,6 @@
1
- import type { Canvas } from './canvas';
1
+ import type { App } from './app';
2
2
  export interface Plugin {
3
3
  name: string;
4
- register?(canvas: Canvas): void;
5
- beforeRender?(canvas: Canvas): void;
6
- afterRender?(canvas: Canvas): void;
4
+ register?(app: App): void;
7
5
  }
8
6
  export declare function definePlugin(plugin: Plugin | (() => Plugin)): Plugin;
@@ -1,7 +1 @@
1
- export * from './base';
2
- export * from './fade';
3
- export * from './node-image';
4
- export * from './node-text';
5
- export * from './selector2d';
6
- export * from './transform2d';
7
- export declare const plugins: import("../plugin").Plugin[];
1
+ export * from './renderer2d';
@@ -0,0 +1 @@
1
+ export declare const Renderer2d: import("../plugin").Plugin;
@@ -0,0 +1,4 @@
1
+ import type { App } from './app';
2
+ import type { Node } from './node';
3
+ export type QueryOptions = string | string[];
4
+ export declare function query(app: App, options?: QueryOptions): Node[];
package/types/render.d.ts CHANGED
@@ -1,2 +1,13 @@
1
- import type { Canvas } from './canvas';
2
- export declare function render(canvas: Canvas, time?: number): void;
1
+ import type { App } from './app';
2
+ export interface RenderNodeOptions {
3
+ shape: string;
4
+ material: string;
5
+ uniforms?: Record<string, any>;
6
+ extraRenderers?: {
7
+ shape: string;
8
+ material: string;
9
+ uniforms?: Record<string, any>;
10
+ }[];
11
+ }
12
+ export declare function renderNode(app: App, options: RenderNodeOptions): void;
13
+ export declare function render(app: App, time?: number): void;
@@ -0,0 +1,2 @@
1
+ import type { App } from './app';
2
+ export declare function setup(app: App): void;
package/types/shape.d.ts CHANGED
@@ -1,14 +1,12 @@
1
- import type { GlDrawModes } from './gl';
2
- import type { Canvas } from './canvas';
3
- export interface Shape {
4
- name: string;
1
+ import type { App } from './app';
2
+ export interface UserShape {
5
3
  type?: '2d' | '3d';
6
- mode?: keyof GlDrawModes;
7
- data?: Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array;
4
+ mode?: keyof App['drawModes'];
5
+ buffer?: Uint8Array | Uint8ClampedArray | Uint16Array | Uint32Array | Int8Array | Int16Array | Int32Array | BigUint64Array | BigInt64Array | Float32Array | Float64Array;
8
6
  }
9
- export interface InternalShape {
7
+ export interface Shape {
10
8
  mode: GLenum;
11
9
  count: number;
12
10
  buffer: WebGLBuffer | null;
13
11
  }
14
- export declare function registerShape(canvas: Canvas, shape: Shape): void;
12
+ export declare function registerShape(app: App, name: string, userShape: UserShape): void;
@@ -0,0 +1,5 @@
1
+ import type { App } from './app';
2
+ export interface System {
3
+ update?(time?: number): void;
4
+ }
5
+ export declare function registerSystem(app: App, system: System): void;
@@ -0,0 +1,7 @@
1
+ import type { App } from './app';
2
+ export interface Texture {
3
+ loading: boolean;
4
+ value: WebGLTexture | null;
5
+ source: TexImageSource;
6
+ }
7
+ export declare function registerTexture(app: App, name: number, source: TexImageSource): Promise<void>;
@@ -0,0 +1,11 @@
1
+ export declare class Matrix3 extends Float32Array {
2
+ static identity(): Matrix3;
3
+ static translation(tx: number, ty: number): Matrix3;
4
+ static rotation(radians: number): Matrix3;
5
+ static scaling(sx: number, sy: number): Matrix3;
6
+ multiply(matrix3: Matrix3): this;
7
+ }
8
+ export declare function getArchetypeIdCodePoints(componentIds: number[]): number[];
9
+ export declare function createVideo(url: string): HTMLVideoElement;
10
+ export declare function createImage(url: string): HTMLImageElement;
11
+ export declare function resolveColor(value: string, defaultValue?: number[]): [number, number, number, number];
package/types/canvas.d.ts DELETED
@@ -1,42 +0,0 @@
1
- import type { InternalResource, Resource } from './resource';
2
- import type { InternalShape, Shape } from './shape';
3
- import type { GlDrawModes, GlExtensions, GlFramebuffer, GlSlTypes } from './gl';
4
- import type { InternalNodeRenderer, NodeRenderer } from './node-renderer';
5
- import type { Node } from './node';
6
- import type { InternalMaterial, Material } from './material';
7
- import type { Plugin } from './plugin';
8
- import type { Container } from './container';
9
- export interface CanvasOptions extends WebGLContextAttributes {
10
- view?: HTMLCanvasElement;
11
- children?: Node[];
12
- plugins?: Plugin[];
13
- }
14
- export interface Canvas extends Container {
15
- view: HTMLCanvasElement;
16
- children: Node[];
17
- gl: WebGLRenderingContext;
18
- glDefaultTexture: WebGLTexture;
19
- glFramebuffers: GlFramebuffer[];
20
- glDrawModes: GlDrawModes;
21
- glSlTypes: GlSlTypes;
22
- glExtensions: GlExtensions;
23
- width: number;
24
- height: number;
25
- plugins: Map<string, Plugin>;
26
- beforeRenderPlugins: Plugin[];
27
- afterRenderPlugins: Plugin[];
28
- shapes: Map<string, InternalShape>;
29
- registerShape(shape: Shape): void;
30
- materials: Map<string, InternalMaterial>;
31
- registerMaterial(options: Material): void;
32
- resources: Map<string, InternalResource>;
33
- registerResource(options: Resource): void;
34
- nodeRenderers: Map<string, InternalNodeRenderer>;
35
- registerNodeRenderer(options: NodeRenderer): void;
36
- forEachNode(callbackFn: (node: Node, path: number[]) => void): void;
37
- load(): Promise<void>;
38
- render(time?: number): void;
39
- startRenderLoop(): void;
40
- destroy(): void;
41
- }
42
- export declare function createCanvas(options?: CanvasOptions): Canvas;
package/types/gl.d.ts DELETED
@@ -1,20 +0,0 @@
1
- import type { Canvas } from './canvas';
2
- export interface GlDrawModes {
3
- points: GLenum;
4
- linear: GLenum;
5
- triangles: GLenum;
6
- triangleStrip: GLenum;
7
- triangleFan: GLenum;
8
- }
9
- export type GlSlType = 'float' | 'vec2' | 'vec3' | 'vec4' | 'int' | 'ivec2' | 'ivec3' | 'ivec4' | 'uint' | 'uvec2' | 'uvec3' | 'uvec4' | 'bool' | 'bvec2' | 'bvec3' | 'bvec4' | 'mat2' | 'mat3' | 'mat4' | 'sampler2D' | 'samplerCube' | 'sampler2DArray';
10
- export type GlSlTypes = Record<number, GlSlType>;
11
- export interface GlFramebuffer {
12
- buffer: WebGLFramebuffer | null;
13
- depthBuffer: WebGLRenderbuffer | null;
14
- texture: WebGLTexture | null;
15
- resize(): void;
16
- }
17
- export interface GlExtensions {
18
- loseContext: WEBGL_lose_context | null;
19
- }
20
- export declare function provideGl(canvas: Canvas, options?: WebGLContextAttributes): void;
@@ -1,18 +0,0 @@
1
- import type { Canvas } from './canvas';
2
- import type { Material } from './material';
3
- import type { Shape } from './shape';
4
- import type { Node } from './node';
5
- export interface NodeRenderer {
6
- name: string;
7
- include?: (node: Node, path: number[]) => boolean;
8
- exclude?: (node: Node, path: number[]) => boolean;
9
- shape: string | Omit<Shape, 'name'>;
10
- material: string | Omit<Material, 'name'>;
11
- update?(node: Node, time: number): undefined | Record<string, any>;
12
- }
13
- export interface InternalNodeRenderer extends NodeRenderer {
14
- shape: string;
15
- material: string;
16
- render(node: Node, time: number): void;
17
- }
18
- export declare function registerNodeRenderer(canvas: Canvas, renderer: NodeRenderer): void;
@@ -1 +0,0 @@
1
- export declare const basePlugin: import("../plugin").Plugin;
@@ -1 +0,0 @@
1
- export declare const fadePlugin: import("../plugin").Plugin;
@@ -1 +0,0 @@
1
- export declare const nodeImagePlugin: import("../plugin").Plugin;
@@ -1 +0,0 @@
1
- export declare const nodeTextPlugin: import("../plugin").Plugin;
@@ -1 +0,0 @@
1
- export declare const selector2dPlugin: import("../plugin").Plugin;
@@ -1 +0,0 @@
1
- export declare const transform2dPlugin: import("../plugin").Plugin;
@@ -1,10 +0,0 @@
1
- import type { Canvas } from './canvas';
2
- export interface Resource {
3
- name: string;
4
- data: TexImageSource;
5
- }
6
- export type InternalResource = {
7
- loading: boolean;
8
- texture: WebGLTexture | null;
9
- };
10
- export declare function registerResource(canvas: Canvas, resource: Resource): void;
@@ -1 +0,0 @@
1
- export * from './matrix3';
@@ -1,7 +0,0 @@
1
- export declare class Matrix3 extends Float32Array {
2
- static identity(): Matrix3;
3
- static translation(tx: number, ty: number): Matrix3;
4
- static rotation(radians: number): Matrix3;
5
- static scaling(sx: number, sy: number): Matrix3;
6
- multiply(matrix3: Matrix3): this;
7
- }