@thi.ng/rasterize 0.3.20 → 1.0.1

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/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-09-21T21:37:59Z
3
+ - **Last updated**: 2022-10-03T16:07:55Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,16 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ # [1.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/rasterize@1.0.0) (2022-09-27)
13
+
14
+ #### 🛑 Breaking changes
15
+
16
+ - add defBlendF/I shader fns, update Shader2D ([4656c50](https://github.com/thi-ng/umbrella/commit/4656c50))
17
+ - BREAKING CHANGE: update Shader2D args
18
+ - add [@thi.ng/porter-duff](https://github.com/thi-ng/umbrella/tree/main/packages/porter-duff) dependency
19
+ - update Shader2D args, pass target buffer as new initial arg
20
+ - add `defBlendF()`/`defBlendI()` shader fns for PD blend ops
21
+
12
22
  ## [0.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/rasterize@0.3.0) (2021-11-17)
13
23
 
14
24
  #### 🚀 Features
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <!-- This file is generated - DO NOT EDIT! -->
2
2
 
3
- # ![rasterize](https://media.thi.ng/umbrella/banners-20220914/thing-rasterize.svg?6dbe9c48)
3
+ # ![rasterize](https://media.thi.ng/umbrella/banners-20220914/thing-rasterize.svg?75411efe)
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/@thi.ng/rasterize.svg)](https://www.npmjs.com/package/@thi.ng/rasterize)
6
6
  ![npm downloads](https://img.shields.io/npm/dm/@thi.ng/rasterize.svg)
@@ -20,6 +20,7 @@ This project is part of the
20
20
  - [Related packages](#related-packages)
21
21
  - [Installation](#installation)
22
22
  - [Dependencies](#dependencies)
23
+ - [Usage examples](#usage-examples)
23
24
  - [API](#api)
24
25
  - [Authors](#authors)
25
26
  - [License](#license)
@@ -153,7 +154,7 @@ node --experimental-repl-await
153
154
  > const rasterize = await import("@thi.ng/rasterize");
154
155
  ```
155
156
 
156
- Package sizes (gzipped, pre-treeshake): ESM: 1.45 KB
157
+ Package sizes (gzipped, pre-treeshake): ESM: 1.59 KB
157
158
 
158
159
  ## Dependencies
159
160
 
@@ -161,9 +162,22 @@ Package sizes (gzipped, pre-treeshake): ESM: 1.45 KB
161
162
  - [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
162
163
  - [@thi.ng/equiv](https://github.com/thi-ng/umbrella/tree/develop/packages/equiv)
163
164
  - [@thi.ng/grid-iterators](https://github.com/thi-ng/umbrella/tree/develop/packages/grid-iterators)
165
+ - [@thi.ng/porter-duff](https://github.com/thi-ng/umbrella/tree/develop/packages/porter-duff)
164
166
  - [@thi.ng/random](https://github.com/thi-ng/umbrella/tree/develop/packages/random)
165
167
  - [@thi.ng/transducers](https://github.com/thi-ng/umbrella/tree/develop/packages/transducers)
166
168
 
169
+ ## Usage examples
170
+
171
+ Several demos in this repo's
172
+ [/examples](https://github.com/thi-ng/umbrella/tree/develop/examples)
173
+ directory are using this package.
174
+
175
+ A selection:
176
+
177
+ | Screenshot | Description | Live demo | Source |
178
+ |:-----------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------|:------------------------------------------------------|:-----------------------------------------------------------------------------------|
179
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/rasterize-blend.jpg" width="240"/> | Steering behavior drawing with alpha-blended shapes | [Demo](https://demo.thi.ng/umbrella/rasterize-blend/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/rasterize-blend) |
180
+
167
181
  ## API
168
182
 
169
183
  [Generated API docs](https://docs.thi.ng/umbrella/rasterize/)
package/api.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import type { Fn2 } from "@thi.ng/api";
2
- export declare type Shader2D<T> = Fn2<number, number, T>;
1
+ import type { Fn3, IGrid2D } from "@thi.ng/api";
2
+ export declare type Shader2D<T> = Fn3<IGrid2D<any, T>, number, number, T>;
3
3
  //# sourceMappingURL=api.d.ts.map
package/draw.js CHANGED
@@ -28,12 +28,12 @@ export const __drawShader2D = (pts, grid, shader) => {
28
28
  if (isPrimitive(grid.getAtUnsafe(0, 0))) {
29
29
  const { data, offset, stride: [sx, sy], } = grid;
30
30
  for (let { 0: x, 1: y } of pts) {
31
- data[offset + x * sx + y * sy] = shader(x, y);
31
+ data[offset + x * sx + y * sy] = shader(grid, x, y);
32
32
  }
33
33
  }
34
34
  else {
35
35
  for (let { 0: x, 1: y } of pts) {
36
- grid.setAtUnsafe(x, y, shader(x, y));
36
+ grid.setAtUnsafe(x, y, shader(grid, x, y));
37
37
  }
38
38
  }
39
39
  return grid;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/rasterize",
3
- "version": "0.3.20",
3
+ "version": "1.0.1",
4
4
  "description": "2D shape drawing & rasterization",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,16 +34,17 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.4.2",
38
- "@thi.ng/checks": "^3.2.5",
39
- "@thi.ng/equiv": "^2.1.10",
40
- "@thi.ng/grid-iterators": "^2.3.13",
41
- "@thi.ng/random": "^3.3.8",
42
- "@thi.ng/transducers": "^8.3.14"
37
+ "@thi.ng/api": "^8.4.3",
38
+ "@thi.ng/checks": "^3.2.6",
39
+ "@thi.ng/equiv": "^2.1.11",
40
+ "@thi.ng/grid-iterators": "^2.3.15",
41
+ "@thi.ng/porter-duff": "^2.1.14",
42
+ "@thi.ng/random": "^3.3.9",
43
+ "@thi.ng/transducers": "^8.3.16"
43
44
  },
44
45
  "devDependencies": {
45
46
  "@microsoft/api-extractor": "^7.31.1",
46
- "@thi.ng/testament": "^0.3.0",
47
+ "@thi.ng/testament": "^0.3.1",
47
48
  "rimraf": "^3.0.2",
48
49
  "tools": "^0.0.1",
49
50
  "typedoc": "^0.22.17",
@@ -118,5 +119,5 @@
118
119
  "status": "alpha",
119
120
  "year": 2021
120
121
  },
121
- "gitHead": "973139c5aa3b50081020f4cc726a7cc330f77fc7\n"
122
+ "gitHead": "8600007d81a7dc92634a1d54e2c32b14ab30ba80\n"
122
123
  }
package/poly.js CHANGED
@@ -59,7 +59,7 @@ export const fillPoly = (grid, pts, val) => {
59
59
  x1 < minX && (x1 = minX);
60
60
  x2 > maxX && (x2 = maxX);
61
61
  for (let x = x1; x <= x2; x++) {
62
- grid.setAtUnsafe(x, y, shader(x, y));
62
+ grid.setAtUnsafe(x, y, shader(grid, x, y));
63
63
  }
64
64
  }
65
65
  }
package/rect.js CHANGED
@@ -25,14 +25,14 @@ export const drawRect = (grid, x, y, w, h, val, fill = false) => {
25
25
  for (let { 0: xx, 1: yy } of pts) {
26
26
  xx += x;
27
27
  yy += y;
28
- data[offset + xx * sx + yy * sy] = shader(xx, yy);
28
+ data[offset + xx * sx + yy * sy] = shader(grid, xx, yy);
29
29
  }
30
30
  }
31
31
  else {
32
32
  for (let { 0: xx, 1: yy } of pts) {
33
33
  xx += x;
34
34
  yy += y;
35
- grid.setAtUnsafe(xx, yy, shader(xx, yy));
35
+ grid.setAtUnsafe(xx, yy, shader(grid, xx, yy));
36
36
  }
37
37
  }
38
38
  return grid;
package/shader.d.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { IGrid2D, TypedArray } from "@thi.ng/api";
1
+ import type { IGrid2D, NumericArray, TypedArray } from "@thi.ng/api";
2
+ import type { BlendFnF, BlendFnI, ReadonlyColor } from "@thi.ng/porter-duff";
2
3
  import type { IRandom } from "@thi.ng/random";
3
4
  import type { Shader2D } from "./api.js";
4
5
  export declare const defPattern: <T extends any[] | TypedArray, P>(pattern: IGrid2D<T, P>) => Shader2D<P>;
@@ -17,4 +18,36 @@ export interface RandomShaderOpts<T> {
17
18
  b: T;
18
19
  }
19
20
  export declare const defNoise: <T = number>(opts: RandomShaderOpts<T>) => Shader2D<T>;
21
+ /**
22
+ * Defines a shader (for floating point target buffers) which blends given color
23
+ * (or another shader) with a Porter-Duff operator. Unless `isPremultiplied` is
24
+ * true, both the given color and existing pixel values in the target buffer are
25
+ * considered non-premultiplied.
26
+ *
27
+ * @remarks
28
+ * The default PD blend op is `SRC_OVER_I`. See
29
+ * [@thi.ng/porter-duff](https://thi.ng/porter-duff) for more info.
30
+ *
31
+ * See {@link defBlendI} for integer based buffers.
32
+ *
33
+ * @param col
34
+ * @param blend
35
+ * @param isPremultiplied
36
+ */
37
+ export declare const defBlendF: (col: ReadonlyColor | Shader2D<NumericArray>, blend?: BlendFnF, isPremultiplied?: boolean) => Shader2D<NumericArray>;
38
+ /**
39
+ * Similar to {@link defBlendF}, but for integer based target buffers. Defines a
40
+ * shader which blends given color (or another shader) with a Porter-Duff
41
+ * operator. Unless `isPremultiplied` is true, both the given color and existing
42
+ * pixel values in the target buffer are considered non-premultiplied.
43
+ *
44
+ * @remarks
45
+ * The default PD blend op is `SRC_OVER_I`. See
46
+ * [@thi.ng/porter-duff](https://thi.ng/porter-duff) for more info.
47
+ *
48
+ * @param col
49
+ * @param blend
50
+ * @param isPremultiplied
51
+ */
52
+ export declare const defBlendI: (col: number | Shader2D<number>, blend?: BlendFnI, isPremultiplied?: boolean) => Shader2D<number>;
20
53
  //# sourceMappingURL=shader.d.ts.map
package/shader.js CHANGED
@@ -1,13 +1,15 @@
1
+ import { isFunction } from "@thi.ng/checks/is-function";
2
+ import { porterDuffP, porterDuffPInt, SRC_OVER_F, SRC_OVER_I, } from "@thi.ng/porter-duff/porter-duff";
1
3
  import { SYSTEM } from "@thi.ng/random/system";
2
4
  export const defPattern = (pattern) => {
3
5
  const [w, h] = pattern.size;
4
- return (x, y) => pattern.getAtUnsafe(x % w, y % h);
6
+ return (_, x, y) => pattern.getAtUnsafe(x % w, y % h);
5
7
  };
6
8
  export const defStripes = ({ dir, size, sizeA, a, b, }) => dir === "h"
7
- ? (x) => (x % size < sizeA ? a : b)
9
+ ? (_, x) => (x % size < sizeA ? a : b)
8
10
  : dir === "v"
9
- ? (_, y) => (y % size < sizeA ? a : b)
10
- : (x, y) => ((x + y) % size < sizeA ? a : b);
11
+ ? (_, __, y) => (y % size < sizeA ? a : b)
12
+ : (_, x, y) => ((x + y) % size < sizeA ? a : b);
11
13
  export const defNoise = (opts) => {
12
14
  const { probability, rnd, a, b } = {
13
15
  probability: 0.5,
@@ -16,3 +18,51 @@ export const defNoise = (opts) => {
16
18
  };
17
19
  return () => (rnd.float() < probability ? a : b);
18
20
  };
21
+ /**
22
+ * Defines a shader (for floating point target buffers) which blends given color
23
+ * (or another shader) with a Porter-Duff operator. Unless `isPremultiplied` is
24
+ * true, both the given color and existing pixel values in the target buffer are
25
+ * considered non-premultiplied.
26
+ *
27
+ * @remarks
28
+ * The default PD blend op is `SRC_OVER_I`. See
29
+ * [@thi.ng/porter-duff](https://thi.ng/porter-duff) for more info.
30
+ *
31
+ * See {@link defBlendI} for integer based buffers.
32
+ *
33
+ * @param col
34
+ * @param blend
35
+ * @param isPremultiplied
36
+ */
37
+ export const defBlendF = (col, blend = SRC_OVER_F, isPremultiplied = false) => {
38
+ blend = isPremultiplied ? blend : porterDuffP(blend);
39
+ return isFunction(col)
40
+ ? (buf, x, y) => {
41
+ const dest = buf.getAtUnsafe(x, y);
42
+ return blend(dest, col(buf, x, y), dest);
43
+ }
44
+ : (buf, x, y) => {
45
+ const dest = buf.getAtUnsafe(x, y);
46
+ return blend(dest, col, dest);
47
+ };
48
+ };
49
+ /**
50
+ * Similar to {@link defBlendF}, but for integer based target buffers. Defines a
51
+ * shader which blends given color (or another shader) with a Porter-Duff
52
+ * operator. Unless `isPremultiplied` is true, both the given color and existing
53
+ * pixel values in the target buffer are considered non-premultiplied.
54
+ *
55
+ * @remarks
56
+ * The default PD blend op is `SRC_OVER_I`. See
57
+ * [@thi.ng/porter-duff](https://thi.ng/porter-duff) for more info.
58
+ *
59
+ * @param col
60
+ * @param blend
61
+ * @param isPremultiplied
62
+ */
63
+ export const defBlendI = (col, blend = SRC_OVER_I, isPremultiplied = false) => {
64
+ blend = isPremultiplied ? blend : porterDuffPInt(blend);
65
+ return isFunction(col)
66
+ ? (buf, x, y) => blend(col(buf, x, y), buf.getAtUnsafe(x, y))
67
+ : (buf, x, y) => blend(col, buf.getAtUnsafe(x, y));
68
+ };