@glyph-engine/web 0.0.0-stage → 0.1.0

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.
@@ -0,0 +1,13 @@
1
+ /**
2
+ @module camera
3
+ @desc Webcam init and helper
4
+ @category public
5
+
6
+ Initializes a user-facing camera,
7
+ returns a video element (initialised asynchronously).
8
+ */
9
+ declare const _default: {
10
+ init: typeof init;
11
+ };
12
+ export default _default;
13
+ declare function init(callback?: (stream: MediaStream) => void): HTMLVideoElement;
@@ -0,0 +1,65 @@
1
+ /**
2
+ @module canvas
3
+ @desc A wrapper for a canvas element
4
+ @category public
5
+
6
+ A canvas 'wrapper' class.
7
+ The purpose is to offer a ready to use buffer (a "pixel" array
8
+ of {r, g, b, (a, v)} objects) of the same size of the ASCII context (or not)
9
+ which can be read or sampled.
10
+ Some convenience functions are provided.
11
+
12
+ Resizes the canvas:
13
+ - resize(w, h)
14
+
15
+ Five main functions are implemented to copy a source (canvas, video, image)
16
+ to the internal canvas:
17
+ - image(source) // resizes the canvas to the source image and copies it
18
+ - copy(source, ...)
19
+ - cover(source, ...)
20
+ - fit(source, ...)
21
+ - center(source, ...)
22
+
23
+ A call to these functions will also update the internal 'pixels' array trough:
24
+ - loadPixels()
25
+
26
+ A few extra functions are provided to manipulate the array directly:
27
+ - mirrorX()
28
+ - normalize() // only v values
29
+ - quantize()
30
+
31
+ Finally the whole buffer can be copied to a destination trough:
32
+ - writeTo()
33
+
34
+ Or accessed with:
35
+ - get(x, y)
36
+ - sample(x, y)
37
+ */
38
+ import { type RGB } from '@glyph-engine/core';
39
+ type Source = HTMLImageElement | HTMLVideoElement | HTMLCanvasElement;
40
+ export declare const MODE_COVER: unique symbol;
41
+ export declare const MODE_FIT: unique symbol;
42
+ export declare const MODE_CENTER: unique symbol;
43
+ export default class Canvas {
44
+ canvas: HTMLCanvasElement;
45
+ ctx: CanvasRenderingContext2D;
46
+ pixels: Array<RGB>;
47
+ constructor(sourceCanvas?: HTMLCanvasElement);
48
+ get width(): number;
49
+ get height(): number;
50
+ resize(dWidth: number, dHeight: number): this;
51
+ copy(source: Source, sx: number, sy: number, sWidth: number, sHeight: number, dx: number, dy: number, dWidth: number, dHeight: number): this;
52
+ image(source: Source): this;
53
+ cover(source: Source, aspect?: number): this;
54
+ fit(source: Source, aspect?: number): this;
55
+ center(source: Source, scaleX?: number, scaleY?: number): this;
56
+ mirrorX(): this;
57
+ normalize(): this;
58
+ quantize(palette: RGB[]): this;
59
+ get(x: number, y: number): RGB;
60
+ sample(sx: number, sy: number, gray?: boolean): number | RGB;
61
+ loadPixels(): this;
62
+ writeTo(buf: RGB[]): this;
63
+ display(target: HTMLElement, x?: number, y?: number): void;
64
+ }
65
+ export {};
@@ -0,0 +1,11 @@
1
+ /**
2
+ @module exportframe
3
+ @desc Exports a single frame (or a range) to an image
4
+ @category public
5
+
6
+ Exports a frame as image.
7
+ Expects the canvas renderer as the active renderer.
8
+ Tested on Safari, FF, Chrome
9
+ */
10
+ import type { Context } from '@glyph-engine/core';
11
+ export declare function exportFrame(context: Context, filename: string, from?: number, to?: number): void;
@@ -0,0 +1,12 @@
1
+ /**
2
+ @module filedownload
3
+ @desc Exports a file via Blob
4
+ @category internal
5
+
6
+ Downloads a Blob as file and this “hack”:
7
+ creates an anchor with a “download” attribute
8
+ and then emits a click event.
9
+ See: https://github.com/tomast1337/FileSaver.ts
10
+ */
11
+ export declare function saveSourceAsFile(src: string, filename: string): void;
12
+ export declare function saveBlobAsFile(blob: Blob, filename: string): void;
@@ -0,0 +1,22 @@
1
+ /**
2
+ @module image
3
+ @desc Image loader and helper
4
+ @category public
5
+
6
+ Loads an image and draws it on a canvas.
7
+ The returned object is a canvas wrapper and its methods (get, sample, etc.)
8
+ can be used before the image has completely loaded.
9
+
10
+ Usage:
11
+ // Starts async loading:
12
+ const img = Image.load('res/pattern.png')
13
+ // Returns a black color until the image has been loaded:
14
+ const color = img.get(10, 10)
15
+
16
+ */
17
+ import Canvas from './canvas';
18
+ declare const _default: {
19
+ load: typeof load;
20
+ };
21
+ export default _default;
22
+ declare function load(path: string): Canvas;
@@ -0,0 +1,37 @@
1
+ /**
2
+ @module loader
3
+ @desc Various file type loader, returns a Promise
4
+ @category internal
5
+
6
+ Example:
7
+
8
+ import Load from './load.js'
9
+
10
+ // Usage: load different file types with one callback
11
+ Promise.all([
12
+ Load.text('assets/1/text.txt'),
13
+ Load.image('assets/1/blocks.png'),
14
+ Load.image('assets/1/colors.png'),
15
+ Load.json('data.json'),
16
+ ]).then(function(res) {
17
+ console.log('Everything has loaded!')
18
+ console.log(res)
19
+ }).catch(function() {
20
+ console.log('Error')
21
+ })
22
+
23
+ // Usage: load a single resource
24
+ Load.image('assets/1/colors.png').then( img => {
25
+ console.log(`Image has loaded, size is: ${img.width}x${img.height}`)
26
+ })
27
+
28
+ */
29
+ declare const _default: {
30
+ json: typeof json;
31
+ image: typeof image;
32
+ text: typeof text;
33
+ };
34
+ export default _default;
35
+ declare function image(url: string): Promise<unknown>;
36
+ declare function text(url: string): Promise<string>;
37
+ declare function json(url: string): Promise<any>;
@@ -0,0 +1,11 @@
1
+ /**
2
+ @module sort
3
+ @desc Sorts a set of characters by brightness
4
+ @category public
5
+
6
+ Paints chars on a temporary canvas and counts the pixels.
7
+ This could be done once and then stored / hardcoded.
8
+ The fontFamily paramter needs to be set because it's used by the canvas element
9
+ to draw the correct font.
10
+ */
11
+ export declare function sort(charSet: Array<string> | string, fontFamily: string, ascending?: boolean): string | string[];
@@ -0,0 +1,7 @@
1
+ import type { Context, Frame } from '@glyph-engine/core';
2
+ import type { GpuStep } from './webgpurenderer';
3
+ export type RenderModes = string;
4
+ export type Render = {
5
+ preferredElementNodeName: RenderModes;
6
+ render: (context: Context, frame: Frame, gpu?: GpuStep) => void;
7
+ };
package/dist/run.d.ts ADDED
@@ -0,0 +1,17 @@
1
+ /**
2
+ Runner
3
+ */
4
+ import { type Program, type Settings } from '@glyph-engine/core';
5
+ export type WebSettings = Settings & {
6
+ element: HTMLElement | HTMLCanvasElement | null;
7
+ };
8
+ export declare function run(program: Program, runSettings?: Partial<WebSettings>, userData?: object): Promise<unknown>;
9
+ export declare function copyContent(el: HTMLElement): void;
10
+ export declare function calcMetrics(el: HTMLElement | HTMLCanvasElement): {
11
+ aspect: number;
12
+ cellWidth: number;
13
+ lineHeight: number;
14
+ fontFamily: string;
15
+ fontSize: number;
16
+ _update: () => void;
17
+ };
@@ -0,0 +1,10 @@
1
+ /**
2
+ Save and restore a JSON object to and from local storage.
3
+ */
4
+ interface Storage {
5
+ store: (key: string, obj: unknown) => boolean;
6
+ restore: <T extends object>(key: string, target: T) => T;
7
+ clear: (key: string) => void;
8
+ }
9
+ declare const storage: Storage;
10
+ export default storage;
@@ -0,0 +1,8 @@
1
+ /**
2
+ @module textrenderer
3
+ @desc renders to a text element
4
+ @category renderer
5
+ */
6
+ import type { Render } from './renderer';
7
+ declare const _default: Render;
8
+ export default _default;
@@ -0,0 +1,8 @@
1
+ /**
2
+ @module webglrenderer
3
+ @desc renders to canvas with WebGL2: the whole grid in one draw call
4
+ @category renderer
5
+ */
6
+ import type { Render } from './renderer';
7
+ declare const _default: Render;
8
+ export default _default;
@@ -0,0 +1,16 @@
1
+ /**
2
+ @module webgpurenderer
3
+ @desc renders to canvas with WebGPU; runs WGSL programs on the GPU
4
+ @category renderer
5
+ */
6
+ import type { Cursor } from '@glyph-engine/core';
7
+ import type { Render } from './renderer';
8
+ export type GpuStep = {
9
+ wgsl: string;
10
+ glyphs: string;
11
+ cursor: Cursor;
12
+ };
13
+ declare const _default: Render;
14
+ export default _default;
15
+ export declare function initWebGPU(): Promise<boolean>;
16
+ export declare function compileGpuProgram(wgsl: string, glyphs: string): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,42 @@
1
1
  {
2
- "name": "@glyph-engine/web",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@glyph-engine/web",
3
+ "version": "0.1.0",
4
+ "description": "GlyphEngine in the browser: run() with text, canvas, WebGL2 and WebGPU renderers.",
5
+ "keywords": [
6
+ "ascii",
7
+ "ascii-art",
8
+ "canvas",
9
+ "webgl",
10
+ "webgpu",
11
+ "creative-coding"
12
+ ],
13
+ "license": "Apache-2.0",
14
+ "homepage": "https://github.com/tomast1337/GlyphEngine#readme",
15
+ "bugs": "https://github.com/tomast1337/GlyphEngine/issues",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/tomast1337/GlyphEngine.git",
19
+ "directory": "packages/web"
20
+ },
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ }
27
+ },
28
+ "files": [
29
+ "dist"
30
+ ],
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "scripts": {
35
+ "build": "rm -rf dist && bun build src/index.ts --target browser --outdir dist --external @glyph-engine/core && tsc -p tsconfig.build.json",
36
+ "prepublishOnly": "bun run build",
37
+ "prepack": "cp ../../LICENSE ."
38
+ },
39
+ "dependencies": {
40
+ "@glyph-engine/core": "0.1.0"
41
+ }
42
+ }