@oliversalzburg/js-utils 0.0.7 → 0.0.9
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/LICENSES.md +17 -0
- package/_site/assets/highlight.css +57 -0
- package/_site/assets/main.js +59 -0
- package/_site/assets/navigation.js +1 -0
- package/_site/assets/search.js +1 -0
- package/_site/assets/style.css +1383 -0
- package/_site/classes/AbstractError.html +28 -0
- package/_site/classes/Canvas.html +44 -0
- package/_site/classes/InternalError.html +32 -0
- package/_site/classes/InvalidArgumentError.html +28 -0
- package/_site/classes/InvalidOperationError.html +28 -0
- package/_site/classes/NotImplementedError.html +27 -0
- package/_site/classes/PermissionViolationError.html +28 -0
- package/_site/classes/Random.html +43 -0
- package/_site/classes/RenderLoop.html +30 -0
- package/_site/classes/ResourceConflictError.html +27 -0
- package/_site/classes/UnexpectedNilError.html +15 -0
- package/_site/classes/UnknownError.html +31 -0
- package/_site/classes/Vector2.html +113 -0
- package/_site/classes/Vector3.html +28 -0
- package/_site/functions/addVector2.html +5 -0
- package/_site/functions/blend.html +8 -0
- package/_site/functions/blendAdditive.html +8 -0
- package/_site/functions/blendSubtractive.html +8 -0
- package/_site/functions/cosDeg.html +4 -0
- package/_site/functions/deg2rad.html +4 -0
- package/_site/functions/difference.html +7 -0
- package/_site/functions/distance.html +7 -0
- package/_site/functions/errorToJSON.html +4 -0
- package/_site/functions/errorToRecord.html +4 -0
- package/_site/functions/errorToSimpleSerializable.html +4 -0
- package/_site/functions/filterType.html +7 -0
- package/_site/functions/formatString.html +7 -0
- package/_site/functions/formatStringTemplate.html +7 -0
- package/_site/functions/fromRGB.html +7 -0
- package/_site/functions/fromRGBA.html +7 -0
- package/_site/functions/getA.html +4 -0
- package/_site/functions/getB.html +4 -0
- package/_site/functions/getDocumentElementTypeById.html +9 -0
- package/_site/functions/getG.html +4 -0
- package/_site/functions/getR.html +4 -0
- package/_site/functions/intersect.html +8 -0
- package/_site/functions/is.html +7 -0
- package/_site/functions/isError.html +4 -0
- package/_site/functions/isInteger.html +4 -0
- package/_site/functions/isNil.html +5 -0
- package/_site/functions/isqrt.html +4 -0
- package/_site/functions/multiplyVector2.html +5 -0
- package/_site/functions/mustExist.html +6 -0
- package/_site/functions/nextPalette.html +2 -0
- package/_site/functions/putPixel32.html +7 -0
- package/_site/functions/putPixel32Add.html +7 -0
- package/_site/functions/putPixel32Sub.html +7 -0
- package/_site/functions/putSubPixel32.html +8 -0
- package/_site/functions/rad2deg.html +4 -0
- package/_site/functions/randomRange.html +6 -0
- package/_site/functions/safeRGBComponent.html +4 -0
- package/_site/functions/seedFromString.html +5 -0
- package/_site/functions/shuffleArray.html +5 -0
- package/_site/functions/sinDeg.html +4 -0
- package/_site/functions/sleep.html +4 -0
- package/_site/functions/somecolor.html +4 -0
- package/_site/functions/subtractVector2.html +5 -0
- package/_site/functions/toGrayScale.html +4 -0
- package/_site/functions/unknownToError.html +6 -0
- package/_site/index.html +1 -0
- package/_site/modules.html +72 -0
- package/_site/types/AnyConstructor.html +2 -0
- package/_site/types/AnyFunction.html +2 -0
- package/_site/types/ConstructorOf.html +2 -0
- package/_site/types/FunctionReturning.html +2 -0
- package/_site/types/Maybe.html +2 -0
- package/_site/types/Mixin.html +3 -0
- package/_site/types/Nil.html +3 -0
- package/_site/types/RenderLoopCallback.html +2 -0
- package/_site/types/SerializedError.html +2 -0
- package/_site/variables/PALETTES.html +2 -0
- package/_site/variables/TWO_PI.html +2 -0
- package/_site/variables/random-1.html +2 -0
- package/lib/array.d.ts +8 -0
- package/lib/array.js +8 -0
- package/lib/array.js.map +1 -1
- package/lib/dom/core.d.ts +13 -0
- package/lib/dom/core.js +24 -0
- package/lib/dom/core.js.map +1 -0
- package/lib/dom/index.d.ts +1 -0
- package/lib/dom/index.js +2 -0
- package/lib/dom/index.js.map +1 -0
- package/lib/error-serializer.d.ts +12 -6
- package/lib/error-serializer.js +11 -7
- package/lib/error-serializer.js.map +1 -1
- package/lib/error-serializer.test.d.ts +1 -0
- package/lib/error-serializer.test.js +117 -0
- package/lib/error-serializer.test.js.map +1 -0
- package/lib/errors/AbstractError.d.ts +4 -16
- package/lib/errors/AbstractError.js +4 -16
- package/lib/errors/AbstractError.js.map +1 -1
- package/lib/errors/InternalError.d.ts +6 -5
- package/lib/errors/InternalError.js +6 -7
- package/lib/errors/InternalError.js.map +1 -1
- package/lib/errors/InvalidArgumentError.d.ts +2 -1
- package/lib/errors/InvalidArgumentError.js +2 -1
- package/lib/errors/InvalidArgumentError.js.map +1 -1
- package/lib/errors/InvalidOperationError.d.ts +2 -1
- package/lib/errors/InvalidOperationError.js +2 -1
- package/lib/errors/InvalidOperationError.js.map +1 -1
- package/lib/errors/NotImplementedError.d.ts +2 -1
- package/lib/errors/NotImplementedError.js +2 -1
- package/lib/errors/NotImplementedError.js.map +1 -1
- package/lib/errors/PermissionViolationError.d.ts +2 -1
- package/lib/errors/PermissionViolationError.js +2 -1
- package/lib/errors/PermissionViolationError.js.map +1 -1
- package/lib/errors/ResourceConflictError.d.ts +2 -1
- package/lib/errors/ResourceConflictError.js +2 -1
- package/lib/errors/ResourceConflictError.js.map +1 -1
- package/lib/errors/UnknownError.d.ts +6 -5
- package/lib/errors/UnknownError.js +6 -5
- package/lib/errors/UnknownError.js.map +1 -1
- package/lib/graphics/canvas.d.ts +55 -19
- package/lib/graphics/canvas.js +66 -18
- package/lib/graphics/canvas.js.map +1 -1
- package/lib/graphics/core.d.ts +18 -0
- package/lib/graphics/core.js +18 -0
- package/lib/graphics/core.js.map +1 -1
- package/lib/graphics/render-loop.d.ts +25 -4
- package/lib/graphics/render-loop.js +19 -2
- package/lib/graphics/render-loop.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/math/core.d.ts +12 -1
- package/lib/math/core.js +13 -2
- package/lib/math/core.js.map +1 -1
- package/lib/math/index.d.ts +2 -0
- package/lib/math/index.js +2 -0
- package/lib/math/index.js.map +1 -1
- package/lib/math/vector.d.ts +2 -33
- package/lib/math/vector.js +2 -41
- package/lib/math/vector.js.map +1 -1
- package/lib/math/vector2.d.ts +195 -0
- package/lib/math/vector2.js +299 -0
- package/lib/math/vector2.js.map +1 -0
- package/lib/math/vector3.d.ts +46 -0
- package/lib/math/vector3.js +59 -0
- package/lib/math/vector3.js.map +1 -0
- package/lib/nil.d.ts +1 -0
- package/lib/nil.js +1 -0
- package/lib/nil.js.map +1 -1
- package/lib/random.d.ts +40 -4
- package/lib/random.js +175 -14
- package/lib/random.js.map +1 -1
- package/lib/string-formatter.d.ts +2 -0
- package/lib/string-formatter.js +2 -0
- package/lib/string-formatter.js.map +1 -1
- package/package.json +10 -10
- package/docs/README.md +0 -3
- package/docs/classes/AbstractError.md +0 -271
- package/docs/classes/Canvas.md +0 -177
- package/docs/classes/InternalError.md +0 -301
- package/docs/classes/InvalidArgumentError.md +0 -277
- package/docs/classes/InvalidOperationError.md +0 -277
- package/docs/classes/NotImplementedError.md +0 -276
- package/docs/classes/PermissionViolationError.md +0 -277
- package/docs/classes/Random.md +0 -109
- package/docs/classes/RenderLoop.md +0 -49
- package/docs/classes/ResourceConflictError.md +0 -276
- package/docs/classes/UnexpectedNilError.md +0 -169
- package/docs/classes/UnknownError.md +0 -280
- package/docs/classes/Vector2.md +0 -79
- package/docs/classes/Vector3.md +0 -106
- package/docs/modules.md +0 -1313
- package/typedoc.json +0 -8
package/lib/graphics/canvas.d.ts
CHANGED
|
@@ -1,80 +1,116 @@
|
|
|
1
1
|
/// <reference types="@types/web" />
|
|
2
2
|
/**
|
|
3
|
-
* A wrapper around {@linkcode
|
|
3
|
+
* A wrapper around {@linkcode !HTMLCanvasElement}
|
|
4
4
|
* to provide some convience for double-buffered drawing.
|
|
5
|
+
* @group Graphics
|
|
5
6
|
*/
|
|
6
7
|
export declare class Canvas {
|
|
8
|
+
/**
|
|
9
|
+
* The {@linkcode !HTMLCanvasElement} we're interacting with.
|
|
10
|
+
*/
|
|
7
11
|
readonly canvas: HTMLCanvasElement;
|
|
12
|
+
/**
|
|
13
|
+
* The width of the canvas.
|
|
14
|
+
*/
|
|
8
15
|
readonly width: number;
|
|
16
|
+
/**
|
|
17
|
+
* The height of the canvas.
|
|
18
|
+
*/
|
|
9
19
|
readonly height: number;
|
|
20
|
+
/**
|
|
21
|
+
* The {@linkcode !CanvasRenderingContext2D} we're using to draw to the canvas.
|
|
22
|
+
*/
|
|
10
23
|
readonly context: CanvasRenderingContext2D;
|
|
11
|
-
readonly pixMap: ImageData;
|
|
12
|
-
readonly buffer: Uint8ClampedArray;
|
|
13
24
|
/**
|
|
14
|
-
*
|
|
25
|
+
* The {@linkcode !ImageData} object
|
|
26
|
+
* that represents our frontbuffer, which is the canvas itself.
|
|
27
|
+
*/
|
|
28
|
+
pixMap: ImageData;
|
|
29
|
+
/**
|
|
30
|
+
* Our back buffer.
|
|
31
|
+
*/
|
|
32
|
+
buffer: Uint8ClampedArray;
|
|
33
|
+
/**
|
|
34
|
+
* Was this {@linkcode Canvas} created with the ability to copy the front buffer back into
|
|
35
|
+
* the back buffer?
|
|
36
|
+
*/
|
|
37
|
+
readonly supportReadBack: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Constructs a new {@linkcode Canvas}.
|
|
15
40
|
* @param canvas The canvas to wrap.
|
|
16
41
|
* @param width The width of the canvas.
|
|
17
42
|
* @param height The height of the canvas.
|
|
43
|
+
* @param supportReadBack Should this canvas support reading back from the front buffer?
|
|
44
|
+
* You will need this, if you plan to use regular canvas drawing on the front buffer, and
|
|
45
|
+
* read the results back into the back buffer.
|
|
18
46
|
*/
|
|
19
|
-
constructor(canvas: HTMLCanvasElement, width: number, height: number);
|
|
47
|
+
constructor(canvas: HTMLCanvasElement, width: number, height: number, supportReadBack?: boolean);
|
|
20
48
|
/**
|
|
21
|
-
* Draws the
|
|
49
|
+
* Draws the back buffer onto the canvas.
|
|
22
50
|
*/
|
|
23
51
|
update(): void;
|
|
24
52
|
/**
|
|
25
|
-
*
|
|
53
|
+
* Reads the front buffer back into the back buffer.
|
|
54
|
+
*/
|
|
55
|
+
bufferReadBack(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Returns the color of a pixel in the back buffer.
|
|
26
58
|
* @param x The X coordinate to retrieve.
|
|
27
59
|
* @param y The X coordinate to retrieve.
|
|
28
60
|
* @returns The color of the pixel at the given local.
|
|
29
61
|
*/
|
|
30
62
|
getPixel32(x: number, y: number): number;
|
|
31
63
|
/**
|
|
32
|
-
* Colors a pixel in the
|
|
64
|
+
* Colors a pixel in the back buffer.
|
|
33
65
|
* @param x The X coordinate to color.
|
|
34
66
|
* @param y The Y coordinate to color.
|
|
35
67
|
* @param color The color to place at the coordinate.
|
|
36
68
|
*/
|
|
37
69
|
setPixel32(x: number, y: number, color: number): void;
|
|
38
70
|
/**
|
|
39
|
-
* Fills the entire
|
|
40
|
-
* @param color The color to fill the
|
|
71
|
+
* Fills the entire back buffer with the given color.
|
|
72
|
+
* @param color The color to fill the back buffer with.
|
|
41
73
|
*/
|
|
42
74
|
clearWith(color: number): void;
|
|
43
75
|
}
|
|
44
76
|
/**
|
|
45
|
-
* Linearly blends a new pixel with an existing color value in a {@
|
|
46
|
-
* @param canvas The {@
|
|
77
|
+
* Linearly blends a new pixel with an existing color value in a {@linkcode Canvas}.
|
|
78
|
+
* @param canvas The {@linkcode Canvas} to interact with.
|
|
47
79
|
* @param x The X coordinate at which to place the pixel.
|
|
48
80
|
* @param y The Y coordinate at which to place the pixel.
|
|
49
81
|
* @param color The color of the pixel.
|
|
50
82
|
* @param alpha The alpha value to use to blend the pixel with existing color at the location.
|
|
83
|
+
* @group Graphics
|
|
51
84
|
*/
|
|
52
85
|
export declare const putPixel32: (canvas: Canvas, x: number, y: number, color: number, alpha: number) => void;
|
|
53
86
|
/**
|
|
54
|
-
* Additively blends a new pixel with an existing color value in a {@
|
|
55
|
-
* @param canvas The {@
|
|
87
|
+
* Additively blends a new pixel with an existing color value in a {@linkcode Canvas}.
|
|
88
|
+
* @param canvas The {@linkcode Canvas} to interact with.
|
|
56
89
|
* @param x The X coordinate at which to place the pixel.
|
|
57
90
|
* @param y The Y coordinate at which to place the pixel.
|
|
58
91
|
* @param color The color of the pixel.
|
|
59
92
|
* @param alpha The alpha value to use to blend the pixel with existing color at the location.
|
|
93
|
+
* @group Graphics
|
|
60
94
|
*/
|
|
61
95
|
export declare const putPixel32Add: (canvas: Canvas, x: number, y: number, color: number, alpha: number) => void;
|
|
62
96
|
/**
|
|
63
|
-
* Subtractively blends a new pixel with an existing color value in a {@
|
|
64
|
-
* @param canvas The {@
|
|
97
|
+
* Subtractively blends a new pixel with an existing color value in a {@linkcode Canvas}.
|
|
98
|
+
* @param canvas The {@linkcode Canvas} to interact with.
|
|
65
99
|
* @param x The X coordinate at which to place the pixel.
|
|
66
100
|
* @param y The Y coordinate at which to place the pixel.
|
|
67
101
|
* @param color The color of the pixel.
|
|
68
102
|
* @param alpha The alpha value to use to blend the pixel with existing color at the location.
|
|
103
|
+
* @group Graphics
|
|
69
104
|
*/
|
|
70
105
|
export declare const putPixel32Sub: (canvas: Canvas, x: number, y: number, color: number, alpha: number) => void;
|
|
71
106
|
/**
|
|
72
|
-
* Linearly blends a new pixel with an existing color value in a {@
|
|
73
|
-
* Compared to {@
|
|
74
|
-
* @param canvas The {@
|
|
107
|
+
* Linearly blends a new pixel with an existing color value in a {@linkcode Canvas}.
|
|
108
|
+
* Compared to {@linkcode putPixel32}, this function supports sub-pixel placement, but is dramatically slower.
|
|
109
|
+
* @param canvas The {@linkcode Canvas} to interact with.
|
|
75
110
|
* @param x The X coordinate at which to place the pixel.
|
|
76
111
|
* @param y The Y coordinate at which to place the pixel.
|
|
77
112
|
* @param color The color of the pixel.
|
|
78
113
|
* @param alpha The alpha value to use to blend the pixel with existing color at the location.
|
|
114
|
+
* @group Graphics
|
|
79
115
|
*/
|
|
80
116
|
export declare const putSubPixel32: (canvas: Canvas, x: number, y: number, color: number, alpha: number) => void;
|
package/lib/graphics/canvas.js
CHANGED
|
@@ -1,39 +1,83 @@
|
|
|
1
|
+
import { InvalidOperationError } from "../errors/InvalidOperationError.js";
|
|
1
2
|
import { mustExist } from "../nil.js";
|
|
2
3
|
import { blend, blendAdditive, blendSubtractive, fromRGBA, getA, getB, getG, getR, } from "./core.js";
|
|
3
4
|
/**
|
|
4
|
-
* A wrapper around {@linkcode
|
|
5
|
+
* A wrapper around {@linkcode !HTMLCanvasElement}
|
|
5
6
|
* to provide some convience for double-buffered drawing.
|
|
7
|
+
* @group Graphics
|
|
6
8
|
*/
|
|
7
9
|
export class Canvas {
|
|
10
|
+
/**
|
|
11
|
+
* The {@linkcode !HTMLCanvasElement} we're interacting with.
|
|
12
|
+
*/
|
|
8
13
|
canvas;
|
|
14
|
+
/**
|
|
15
|
+
* The width of the canvas.
|
|
16
|
+
*/
|
|
9
17
|
width;
|
|
18
|
+
/**
|
|
19
|
+
* The height of the canvas.
|
|
20
|
+
*/
|
|
10
21
|
height;
|
|
22
|
+
/**
|
|
23
|
+
* The {@linkcode !CanvasRenderingContext2D} we're using to draw to the canvas.
|
|
24
|
+
*/
|
|
11
25
|
context;
|
|
26
|
+
/**
|
|
27
|
+
* The {@linkcode !ImageData} object
|
|
28
|
+
* that represents our frontbuffer, which is the canvas itself.
|
|
29
|
+
*/
|
|
12
30
|
pixMap;
|
|
31
|
+
/**
|
|
32
|
+
* Our back buffer.
|
|
33
|
+
*/
|
|
13
34
|
buffer;
|
|
14
35
|
/**
|
|
15
|
-
*
|
|
36
|
+
* Was this {@linkcode Canvas} created with the ability to copy the front buffer back into
|
|
37
|
+
* the back buffer?
|
|
38
|
+
*/
|
|
39
|
+
supportReadBack;
|
|
40
|
+
/**
|
|
41
|
+
* Constructs a new {@linkcode Canvas}.
|
|
16
42
|
* @param canvas The canvas to wrap.
|
|
17
43
|
* @param width The width of the canvas.
|
|
18
44
|
* @param height The height of the canvas.
|
|
45
|
+
* @param supportReadBack Should this canvas support reading back from the front buffer?
|
|
46
|
+
* You will need this, if you plan to use regular canvas drawing on the front buffer, and
|
|
47
|
+
* read the results back into the back buffer.
|
|
19
48
|
*/
|
|
20
|
-
constructor(canvas, width, height) {
|
|
49
|
+
constructor(canvas, width, height, supportReadBack = false) {
|
|
21
50
|
this.canvas = canvas;
|
|
22
51
|
this.width = width;
|
|
23
52
|
this.height = height;
|
|
24
|
-
this.context = mustExist(this.canvas.getContext("2d"
|
|
53
|
+
this.context = mustExist(this.canvas.getContext("2d", {
|
|
54
|
+
alpha: false,
|
|
55
|
+
desynchronized: true,
|
|
56
|
+
willReadFrequently: supportReadBack,
|
|
57
|
+
}), "Unable to create rendering context.");
|
|
58
|
+
this.supportReadBack = supportReadBack;
|
|
25
59
|
this.pixMap = this.context.createImageData(width, height);
|
|
26
60
|
this.buffer = this.pixMap.data;
|
|
27
61
|
this.context.font = "12px sans-serif";
|
|
28
62
|
}
|
|
29
63
|
/**
|
|
30
|
-
* Draws the
|
|
64
|
+
* Draws the back buffer onto the canvas.
|
|
31
65
|
*/
|
|
32
66
|
update() {
|
|
33
67
|
this.context.putImageData(this.pixMap, 0, 0);
|
|
34
68
|
}
|
|
35
69
|
/**
|
|
36
|
-
*
|
|
70
|
+
* Reads the front buffer back into the back buffer.
|
|
71
|
+
*/
|
|
72
|
+
bufferReadBack() {
|
|
73
|
+
if (!this.supportReadBack) {
|
|
74
|
+
throw new InvalidOperationError("To use `bufferReadBack()`, you must construct the `Canvas` with `supportReadBack` set to `true`.");
|
|
75
|
+
}
|
|
76
|
+
this.pixMap = this.context.getImageData(0, 0, this.width, this.height);
|
|
77
|
+
this.buffer = this.pixMap.data;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns the color of a pixel in the back buffer.
|
|
37
81
|
* @param x The X coordinate to retrieve.
|
|
38
82
|
* @param y The X coordinate to retrieve.
|
|
39
83
|
* @returns The color of the pixel at the given local.
|
|
@@ -43,7 +87,7 @@ export class Canvas {
|
|
|
43
87
|
return fromRGBA(this.buffer[bufferOffset + 0], this.buffer[bufferOffset + 1], this.buffer[bufferOffset + 2], this.buffer[bufferOffset + 3]);
|
|
44
88
|
}
|
|
45
89
|
/**
|
|
46
|
-
* Colors a pixel in the
|
|
90
|
+
* Colors a pixel in the back buffer.
|
|
47
91
|
* @param x The X coordinate to color.
|
|
48
92
|
* @param y The Y coordinate to color.
|
|
49
93
|
* @param color The color to place at the coordinate.
|
|
@@ -56,8 +100,8 @@ export class Canvas {
|
|
|
56
100
|
this.buffer[bufferOffset + 3] = getA(color);
|
|
57
101
|
}
|
|
58
102
|
/**
|
|
59
|
-
* Fills the entire
|
|
60
|
-
* @param color The color to fill the
|
|
103
|
+
* Fills the entire back buffer with the given color.
|
|
104
|
+
* @param color The color to fill the back buffer with.
|
|
61
105
|
*/
|
|
62
106
|
clearWith(color) {
|
|
63
107
|
const r = getR(color);
|
|
@@ -73,12 +117,13 @@ export class Canvas {
|
|
|
73
117
|
}
|
|
74
118
|
}
|
|
75
119
|
/**
|
|
76
|
-
* Linearly blends a new pixel with an existing color value in a {@
|
|
77
|
-
* @param canvas The {@
|
|
120
|
+
* Linearly blends a new pixel with an existing color value in a {@linkcode Canvas}.
|
|
121
|
+
* @param canvas The {@linkcode Canvas} to interact with.
|
|
78
122
|
* @param x The X coordinate at which to place the pixel.
|
|
79
123
|
* @param y The Y coordinate at which to place the pixel.
|
|
80
124
|
* @param color The color of the pixel.
|
|
81
125
|
* @param alpha The alpha value to use to blend the pixel with existing color at the location.
|
|
126
|
+
* @group Graphics
|
|
82
127
|
*/
|
|
83
128
|
export const putPixel32 = (canvas, x, y, color, alpha) => {
|
|
84
129
|
if (canvas.width < x || x < 0 || canvas.height < y || y < 0) {
|
|
@@ -94,12 +139,13 @@ export const putPixel32 = (canvas, x, y, color, alpha) => {
|
|
|
94
139
|
canvas.setPixel32(x, y, newColor);
|
|
95
140
|
};
|
|
96
141
|
/**
|
|
97
|
-
* Additively blends a new pixel with an existing color value in a {@
|
|
98
|
-
* @param canvas The {@
|
|
142
|
+
* Additively blends a new pixel with an existing color value in a {@linkcode Canvas}.
|
|
143
|
+
* @param canvas The {@linkcode Canvas} to interact with.
|
|
99
144
|
* @param x The X coordinate at which to place the pixel.
|
|
100
145
|
* @param y The Y coordinate at which to place the pixel.
|
|
101
146
|
* @param color The color of the pixel.
|
|
102
147
|
* @param alpha The alpha value to use to blend the pixel with existing color at the location.
|
|
148
|
+
* @group Graphics
|
|
103
149
|
*/
|
|
104
150
|
export const putPixel32Add = (canvas, x, y, color, alpha) => {
|
|
105
151
|
if (canvas.width < x || x < 0 || canvas.height < y || y < 0) {
|
|
@@ -115,12 +161,13 @@ export const putPixel32Add = (canvas, x, y, color, alpha) => {
|
|
|
115
161
|
canvas.setPixel32(x, y, newColor);
|
|
116
162
|
};
|
|
117
163
|
/**
|
|
118
|
-
* Subtractively blends a new pixel with an existing color value in a {@
|
|
119
|
-
* @param canvas The {@
|
|
164
|
+
* Subtractively blends a new pixel with an existing color value in a {@linkcode Canvas}.
|
|
165
|
+
* @param canvas The {@linkcode Canvas} to interact with.
|
|
120
166
|
* @param x The X coordinate at which to place the pixel.
|
|
121
167
|
* @param y The Y coordinate at which to place the pixel.
|
|
122
168
|
* @param color The color of the pixel.
|
|
123
169
|
* @param alpha The alpha value to use to blend the pixel with existing color at the location.
|
|
170
|
+
* @group Graphics
|
|
124
171
|
*/
|
|
125
172
|
export const putPixel32Sub = (canvas, x, y, color, alpha) => {
|
|
126
173
|
if (canvas.width < x || x < 0 || canvas.height < y || y < 0) {
|
|
@@ -136,13 +183,14 @@ export const putPixel32Sub = (canvas, x, y, color, alpha) => {
|
|
|
136
183
|
canvas.setPixel32(x, y, newColor);
|
|
137
184
|
};
|
|
138
185
|
/**
|
|
139
|
-
* Linearly blends a new pixel with an existing color value in a {@
|
|
140
|
-
* Compared to {@
|
|
141
|
-
* @param canvas The {@
|
|
186
|
+
* Linearly blends a new pixel with an existing color value in a {@linkcode Canvas}.
|
|
187
|
+
* Compared to {@linkcode putPixel32}, this function supports sub-pixel placement, but is dramatically slower.
|
|
188
|
+
* @param canvas The {@linkcode Canvas} to interact with.
|
|
142
189
|
* @param x The X coordinate at which to place the pixel.
|
|
143
190
|
* @param y The Y coordinate at which to place the pixel.
|
|
144
191
|
* @param color The color of the pixel.
|
|
145
192
|
* @param alpha The alpha value to use to blend the pixel with existing color at the location.
|
|
193
|
+
* @group Graphics
|
|
146
194
|
*/
|
|
147
195
|
export const putSubPixel32 = (canvas, x, y, color, alpha) => {
|
|
148
196
|
if (canvas.width < x || x < 0 || canvas.height < y || y < 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"canvas.js","sourceRoot":"","sources":["../../source/graphics/canvas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EACL,KAAK,EACL,aAAa,EACb,gBAAgB,EAChB,QAAQ,EACR,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,GACL,MAAM,WAAW,CAAC;AAEnB
|
|
1
|
+
{"version":3,"file":"canvas.js","sourceRoot":"","sources":["../../source/graphics/canvas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EACL,KAAK,EACL,aAAa,EACb,gBAAgB,EAChB,QAAQ,EACR,IAAI,EACJ,IAAI,EACJ,IAAI,EACJ,IAAI,GACL,MAAM,WAAW,CAAC;AAEnB;;;;GAIG;AACH,MAAM,OAAO,MAAM;IACjB;;OAEG;IACM,MAAM,CAAoB;IAEnC;;OAEG;IACM,KAAK,CAAS;IAEvB;;OAEG;IACM,MAAM,CAAS;IAExB;;OAEG;IACM,OAAO,CAA2B;IAE3C;;;OAGG;IACH,MAAM,CAAY;IAElB;;OAEG;IACH,MAAM,CAAoB;IAE1B;;;OAGG;IACM,eAAe,CAAU;IAElC;;;;;;;;OAQG;IACH,YAAY,MAAy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CAAC,EAAE,KAAK,EAAE,MAAM,CAAC,CAAC;AAClD,CAAC,CAAC"}
|
package/lib/graphics/core.d.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Color palettes that can be used to quickly colorize a drawing.
|
|
3
|
+
* @group Graphics
|
|
4
|
+
*/
|
|
1
5
|
export declare const PALETTES: number[][];
|
|
2
6
|
/**
|
|
3
7
|
* Constrains a color component value to single-byte range (`0`-`255`).
|
|
4
8
|
* @param c A color component value.
|
|
5
9
|
* @returns The input value constrained into single-byte range.
|
|
10
|
+
* @group Graphics
|
|
6
11
|
*/
|
|
7
12
|
export declare const safeRGBComponent: (c: number) => number;
|
|
8
13
|
/**
|
|
@@ -12,6 +17,7 @@ export declare const safeRGBComponent: (c: number) => number;
|
|
|
12
17
|
* @param b The B component.
|
|
13
18
|
* @param a The A component.
|
|
14
19
|
* @returns The constructed color value.
|
|
20
|
+
* @group Graphics
|
|
15
21
|
*/
|
|
16
22
|
export declare const fromRGBA: (r: number, g: number, b: number, a: number) => number;
|
|
17
23
|
/**
|
|
@@ -21,30 +27,35 @@ export declare const fromRGBA: (r: number, g: number, b: number, a: number) => n
|
|
|
21
27
|
* @param g The G component.
|
|
22
28
|
* @param b The B component.
|
|
23
29
|
* @returns The constructed color value.
|
|
30
|
+
* @group Graphics
|
|
24
31
|
*/
|
|
25
32
|
export declare const fromRGB: (r: number, g: number, b: number) => number;
|
|
26
33
|
/**
|
|
27
34
|
* Extracts the R component from a color value.
|
|
28
35
|
* @param color The color value.
|
|
29
36
|
* @returns The R component of the color value.
|
|
37
|
+
* @group Graphics
|
|
30
38
|
*/
|
|
31
39
|
export declare const getR: (color: number) => number;
|
|
32
40
|
/**
|
|
33
41
|
* Extracts the G component from a color value.
|
|
34
42
|
* @param color The color value.
|
|
35
43
|
* @returns The G component of the color value.
|
|
44
|
+
* @group Graphics
|
|
36
45
|
*/
|
|
37
46
|
export declare const getG: (color: number) => number;
|
|
38
47
|
/**
|
|
39
48
|
* Extracts the B component from a color value.
|
|
40
49
|
* @param color The color value.
|
|
41
50
|
* @returns The B component of the color value.
|
|
51
|
+
* @group Graphics
|
|
42
52
|
*/
|
|
43
53
|
export declare const getB: (color: number) => number;
|
|
44
54
|
/**
|
|
45
55
|
* Extracts the A component from a color value.
|
|
46
56
|
* @param color The color value.
|
|
47
57
|
* @returns The A component of the color value.
|
|
58
|
+
* @group Graphics
|
|
48
59
|
*/
|
|
49
60
|
export declare const getA: (color: number) => number;
|
|
50
61
|
/**
|
|
@@ -55,6 +66,7 @@ export declare const getA: (color: number) => number;
|
|
|
55
66
|
* @param dst The destination color.
|
|
56
67
|
* @param alpha The alpha value to use for blending.
|
|
57
68
|
* @returns The blended color.
|
|
69
|
+
* @group Graphics
|
|
58
70
|
*/
|
|
59
71
|
export declare const blend: (src: number, dst: number, alpha: number) => number;
|
|
60
72
|
/**
|
|
@@ -65,6 +77,7 @@ export declare const blend: (src: number, dst: number, alpha: number) => number;
|
|
|
65
77
|
* @param dst The destination color.
|
|
66
78
|
* @param alpha The alpha value to use for blending.
|
|
67
79
|
* @returns The blended color.
|
|
80
|
+
* @group Graphics
|
|
68
81
|
*/
|
|
69
82
|
export declare const blendAdditive: (src: number, dst: number, alpha: number) => number;
|
|
70
83
|
/**
|
|
@@ -75,20 +88,25 @@ export declare const blendAdditive: (src: number, dst: number, alpha: number) =>
|
|
|
75
88
|
* @param dst The destination color.
|
|
76
89
|
* @param alpha The alpha value to use for blending.
|
|
77
90
|
* @returns The blended color.
|
|
91
|
+
* @group Graphics
|
|
78
92
|
*/
|
|
79
93
|
export declare const blendSubtractive: (src: number, dst: number, alpha: number) => number;
|
|
80
94
|
/**
|
|
81
95
|
* Retrieves a random color from the current global palette.
|
|
96
|
+
* Uses the global {@linkcode random} object.
|
|
82
97
|
* @returns A random color.
|
|
98
|
+
* @group Graphics
|
|
83
99
|
*/
|
|
84
100
|
export declare const somecolor: () => number;
|
|
85
101
|
/**
|
|
86
102
|
* Switches the global palette to the next available preset.
|
|
103
|
+
* @group Graphics
|
|
87
104
|
*/
|
|
88
105
|
export declare const nextPalette: () => void;
|
|
89
106
|
/**
|
|
90
107
|
* Converts a color value to grayscale.
|
|
91
108
|
* @param color The color to convert.
|
|
92
109
|
* @returns The grayscale value appropriate for the color.
|
|
110
|
+
* @group Graphics
|
|
93
111
|
*/
|
|
94
112
|
export declare const toGrayScale: (color: number) => number;
|
package/lib/graphics/core.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { random } from "../random.js";
|
|
2
2
|
let PALETTE_INDEX = 0;
|
|
3
|
+
/**
|
|
4
|
+
* Color palettes that can be used to quickly colorize a drawing.
|
|
5
|
+
* @group Graphics
|
|
6
|
+
*/
|
|
3
7
|
export const PALETTES = [
|
|
4
8
|
[
|
|
5
9
|
0xc2b799, 0xa6a183, 0xc5bfa7, 0x6d624e, 0xa8987e, 0xc1bca6, 0xe3decb, 0x896b51, 0x744d26,
|
|
@@ -322,6 +326,7 @@ export const PALETTES = [
|
|
|
322
326
|
* Constrains a color component value to single-byte range (`0`-`255`).
|
|
323
327
|
* @param c A color component value.
|
|
324
328
|
* @returns The input value constrained into single-byte range.
|
|
329
|
+
* @group Graphics
|
|
325
330
|
*/
|
|
326
331
|
export const safeRGBComponent = (c) => {
|
|
327
332
|
if (c < 0) {
|
|
@@ -339,6 +344,7 @@ export const safeRGBComponent = (c) => {
|
|
|
339
344
|
* @param b The B component.
|
|
340
345
|
* @param a The A component.
|
|
341
346
|
* @returns The constructed color value.
|
|
347
|
+
* @group Graphics
|
|
342
348
|
*/
|
|
343
349
|
export const fromRGBA = (r, g, b, a) => {
|
|
344
350
|
r = safeRGBComponent(r);
|
|
@@ -354,6 +360,7 @@ export const fromRGBA = (r, g, b, a) => {
|
|
|
354
360
|
* @param g The G component.
|
|
355
361
|
* @param b The B component.
|
|
356
362
|
* @returns The constructed color value.
|
|
363
|
+
* @group Graphics
|
|
357
364
|
*/
|
|
358
365
|
export const fromRGB = (r, g, b) => {
|
|
359
366
|
return fromRGBA(r, g, b, 255);
|
|
@@ -362,6 +369,7 @@ export const fromRGB = (r, g, b) => {
|
|
|
362
369
|
* Extracts the R component from a color value.
|
|
363
370
|
* @param color The color value.
|
|
364
371
|
* @returns The R component of the color value.
|
|
372
|
+
* @group Graphics
|
|
365
373
|
*/
|
|
366
374
|
export const getR = (color) => {
|
|
367
375
|
return (color >> 24) & 0xff;
|
|
@@ -370,6 +378,7 @@ export const getR = (color) => {
|
|
|
370
378
|
* Extracts the G component from a color value.
|
|
371
379
|
* @param color The color value.
|
|
372
380
|
* @returns The G component of the color value.
|
|
381
|
+
* @group Graphics
|
|
373
382
|
*/
|
|
374
383
|
export const getG = (color) => {
|
|
375
384
|
return (color >> 16) & 0xff;
|
|
@@ -378,6 +387,7 @@ export const getG = (color) => {
|
|
|
378
387
|
* Extracts the B component from a color value.
|
|
379
388
|
* @param color The color value.
|
|
380
389
|
* @returns The B component of the color value.
|
|
390
|
+
* @group Graphics
|
|
381
391
|
*/
|
|
382
392
|
export const getB = (color) => {
|
|
383
393
|
return (color >> 8) & 0xff;
|
|
@@ -386,6 +396,7 @@ export const getB = (color) => {
|
|
|
386
396
|
* Extracts the A component from a color value.
|
|
387
397
|
* @param color The color value.
|
|
388
398
|
* @returns The A component of the color value.
|
|
399
|
+
* @group Graphics
|
|
389
400
|
*/
|
|
390
401
|
export const getA = (color) => {
|
|
391
402
|
return color & 0xff;
|
|
@@ -398,6 +409,7 @@ export const getA = (color) => {
|
|
|
398
409
|
* @param dst The destination color.
|
|
399
410
|
* @param alpha The alpha value to use for blending.
|
|
400
411
|
* @returns The blended color.
|
|
412
|
+
* @group Graphics
|
|
401
413
|
*/
|
|
402
414
|
export const blend = (src, dst, alpha) => {
|
|
403
415
|
if (alpha >= 255) {
|
|
@@ -418,6 +430,7 @@ export const blend = (src, dst, alpha) => {
|
|
|
418
430
|
* @param dst The destination color.
|
|
419
431
|
* @param alpha The alpha value to use for blending.
|
|
420
432
|
* @returns The blended color.
|
|
433
|
+
* @group Graphics
|
|
421
434
|
*/
|
|
422
435
|
export const blendAdditive = (src, dst, alpha) => {
|
|
423
436
|
if (alpha >= 255) {
|
|
@@ -436,6 +449,7 @@ export const blendAdditive = (src, dst, alpha) => {
|
|
|
436
449
|
* @param dst The destination color.
|
|
437
450
|
* @param alpha The alpha value to use for blending.
|
|
438
451
|
* @returns The blended color.
|
|
452
|
+
* @group Graphics
|
|
439
453
|
*/
|
|
440
454
|
export const blendSubtractive = (src, dst, alpha) => {
|
|
441
455
|
if (alpha >= 255) {
|
|
@@ -451,7 +465,9 @@ export const blendSubtractive = (src, dst, alpha) => {
|
|
|
451
465
|
};
|
|
452
466
|
/**
|
|
453
467
|
* Retrieves a random color from the current global palette.
|
|
468
|
+
* Uses the global {@linkcode random} object.
|
|
454
469
|
* @returns A random color.
|
|
470
|
+
* @group Graphics
|
|
455
471
|
*/
|
|
456
472
|
export const somecolor = () => {
|
|
457
473
|
// pick some random good color
|
|
@@ -460,6 +476,7 @@ export const somecolor = () => {
|
|
|
460
476
|
};
|
|
461
477
|
/**
|
|
462
478
|
* Switches the global palette to the next available preset.
|
|
479
|
+
* @group Graphics
|
|
463
480
|
*/
|
|
464
481
|
export const nextPalette = () => {
|
|
465
482
|
++PALETTE_INDEX;
|
|
@@ -471,6 +488,7 @@ export const nextPalette = () => {
|
|
|
471
488
|
* Converts a color value to grayscale.
|
|
472
489
|
* @param color The color to convert.
|
|
473
490
|
* @returns The grayscale value appropriate for the color.
|
|
491
|
+
* @group Graphics
|
|
474
492
|
*/
|
|
475
493
|
export const toGrayScale = (color) => {
|
|
476
494
|
const gs = getR(color) * 0.3 + getG(color) * 0.59 + getB(color) * 0.11;
|