@fulate/share 1.0.14 → 1.0.15

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/README.md CHANGED
@@ -4,7 +4,8 @@
4
4
 
5
5
  - 点:`PointView`、`MutablePointView`、`createPointView()`、`transformPoint()`、距离判断;
6
6
  - 矩形:`Rect`、`BoundingBox`、可变 `Bound` 及 points/rects 边界计算;
7
- - 矩阵和数学:`isIdentityMatrix()`、`qrDecompose()`、角度/弧度换算、矩形边缘位置;
7
+ - 矩阵和数学:Fulate 唯一的 2D 仿射矩阵 `Matrix2D` / `ReadonlyMatrix2D`、`qrDecompose()`、
8
+ 角度/弧度换算、矩形边缘位置;
8
9
  - 几何:`Intersection`、`clipPolygonToRect()`;
9
10
  - 颜色和渐变:颜色解析/格式化、alpha 合成、混色和 Canvas gradient 创建。
10
11
 
@@ -16,7 +17,7 @@ import {
16
17
  colorWithAlpha
17
18
  } from "@fulate/share";
18
19
 
19
- const point = transformPoint(createPointView(10, 20), matrix);
20
+ const point = transformPoint(matrix, createPointView(10, 20));
20
21
  const bounds = Bound.fromPoints([point]);
21
22
  const overlay = colorWithAlpha("#6750a4", 0.12);
22
23
  ```
package/dist/index.d.ts CHANGED
@@ -2,7 +2,8 @@ export { transformPoint, createPointView, isPointWithinDistance } from "./point"
2
2
  export type { MutablePointView, PointView } from "./point";
3
3
  export { Bound, makeBoundingBoxFromRects } from "./rect";
4
4
  export type { Rect, RectWithCenter, BoundingBox } from "./rect";
5
- export { isIdentityMatrix, qrDecompose } from "./matrix";
5
+ export { Matrix2D, qrDecompose } from "./matrix";
6
+ export type { ReadonlyMatrix2D } from "./matrix";
6
7
  export { degreesToRadians, radiansToDegrees, rectEdgePosition } from "./math";
7
8
  export type { Edge } from "./math";
8
9
  export { Intersection, clipPolygonToRect } from "./Intersection";
package/dist/index.js CHANGED
@@ -164,9 +164,89 @@ function rectEdgePosition(w, h, edge, ratio) {
164
164
  }
165
165
  //#endregion
166
166
  //#region packages/share/src/matrix.ts
167
- function isIdentityMatrix(matrix) {
168
- return matrix.a === 1 && matrix.b === 0 && matrix.c === 0 && matrix.d === 1 && matrix.e === 0 && matrix.f === 0;
169
- }
167
+ var RADIANS_PER_DEGREE = Math.PI / 180;
168
+ /**
169
+ * Fulate 唯一的 2D 仿射矩阵。分量是普通 JS 字段,读写不经过宿主对象绑定;
170
+ * `*Self` 方法原地修改并返回自身,其余方法返回新矩阵。
171
+ *
172
+ * 每个运算结果都以 `+ 0` 规范化零的符号:规范仿射乘积总含 `0·0` 项,零分量为 `+0`;
173
+ * 保留 `-0` 会让 `atan2()` 等分解在 ±180° 之间翻转。
174
+ */
175
+ var Matrix2D = class Matrix2D {
176
+ constructor(a = 1, b = 0, c = 0, d = 1, e = 0, f = 0) {
177
+ this.a = a;
178
+ this.b = b;
179
+ this.c = c;
180
+ this.d = d;
181
+ this.e = e;
182
+ this.f = f;
183
+ }
184
+ /** 复制任意只读矩阵的六个分量。 */
185
+ static from(matrix) {
186
+ return new Matrix2D(matrix.a, matrix.b, matrix.c, matrix.d, matrix.e, matrix.f);
187
+ }
188
+ get isIdentity() {
189
+ return this.a === 1 && this.b === 0 && this.c === 0 && this.d === 1 && this.e === 0 && this.f === 0;
190
+ }
191
+ multiply(other) {
192
+ const { a, b, c, d, e, f } = this;
193
+ return new Matrix2D(a * other.a + c * other.b + 0, b * other.a + d * other.b + 0, a * other.c + c * other.d + 0, b * other.c + d * other.d + 0, a * other.e + c * other.f + e + 0, b * other.e + d * other.f + f + 0);
194
+ }
195
+ inverse() {
196
+ const { a, b, c, d, e, f } = this;
197
+ const determinant = a * d - b * c;
198
+ if (determinant === 0) return new Matrix2D(NaN, NaN, NaN, NaN, NaN, NaN);
199
+ return new Matrix2D(d / determinant + 0, -b / determinant + 0, -c / determinant + 0, a / determinant + 0, (c * f - d * e) / determinant + 0, (b * e - a * f) / determinant + 0);
200
+ }
201
+ /** 原地右乘平移。 */
202
+ translateSelf(tx, ty = 0) {
203
+ this.e = this.a * tx + this.c * ty + this.e + 0;
204
+ this.f = this.b * tx + this.d * ty + this.f + 0;
205
+ return this;
206
+ }
207
+ /** 原地右乘旋转;90° 的整数倍使用精确的 sin/cos。 */
208
+ rotateSelf(degrees) {
209
+ let sin;
210
+ let cos;
211
+ if (degrees % 90 === 0) {
212
+ const quarter = (degrees / 90 % 4 + 4) % 4;
213
+ sin = quarter === 1 ? 1 : quarter === 3 ? -1 : 0;
214
+ cos = quarter === 0 ? 1 : quarter === 2 ? -1 : 0;
215
+ } else {
216
+ const radians = degrees * RADIANS_PER_DEGREE;
217
+ sin = Math.sin(radians);
218
+ cos = Math.cos(radians);
219
+ }
220
+ const { a, b, c, d } = this;
221
+ this.a = a * cos + c * sin + 0;
222
+ this.b = b * cos + d * sin + 0;
223
+ this.c = c * cos - a * sin + 0;
224
+ this.d = d * cos - b * sin + 0;
225
+ return this;
226
+ }
227
+ /** 原地右乘缩放;省略 `sy` 时与 `sx` 相同。 */
228
+ scaleSelf(sx, sy = sx) {
229
+ this.a = this.a * sx + 0;
230
+ this.b = this.b * sx + 0;
231
+ this.c = this.c * sy + 0;
232
+ this.d = this.d * sy + 0;
233
+ return this;
234
+ }
235
+ /** 原地右乘 x 轴错切(单位为度)。 */
236
+ skewXSelf(degrees) {
237
+ const tan = Math.tan(degrees * RADIANS_PER_DEGREE);
238
+ this.c = this.a * tan + this.c + 0;
239
+ this.d = this.b * tan + this.d + 0;
240
+ return this;
241
+ }
242
+ /** 原地右乘 y 轴错切(单位为度)。 */
243
+ skewYSelf(degrees) {
244
+ const tan = Math.tan(degrees * RADIANS_PER_DEGREE);
245
+ this.a = this.a + this.c * tan + 0;
246
+ this.b = this.b + this.d * tan + 0;
247
+ return this;
248
+ }
249
+ };
170
250
  function qrDecompose(matrix) {
171
251
  const { a, b, c, d } = matrix;
172
252
  const angleRad = Math.atan2(b, a);
@@ -175,11 +255,12 @@ function qrDecompose(matrix) {
175
255
  const a1 = a * cos - b * sin;
176
256
  const c1 = c * cos - d * sin;
177
257
  const d1 = c * sin + d * cos;
258
+ const skewRad = d1 < 0 ? Math.atan2(-c1, -d1) : Math.atan2(c1, d1);
178
259
  return {
179
260
  angle: radiansToDegrees(angleRad),
180
261
  scaleX: a1,
181
262
  scaleY: d1,
182
- skewX: radiansToDegrees(Math.atan2(c1, d1))
263
+ skewX: radiansToDegrees(skewRad) + 0
183
264
  };
184
265
  }
185
266
  //#endregion
@@ -453,4 +534,4 @@ function createCanvasGradient(ctx, gradient, width, height) {
453
534
  return result;
454
535
  }
455
536
  //#endregion
456
- export { Bound, Intersection, blendColor, clipPolygonToRect, colorWithAlpha, createCanvasGradient, createPointView, degreesToRadians, formatColor, formatPremultipliedColor, isGradient, isIdentityMatrix, isPointWithinDistance, makeBoundingBoxFromRects, parseColor, parsePremultipliedColor, qrDecompose, radiansToDegrees, rectEdgePosition, transformPoint };
537
+ export { Bound, Intersection, Matrix2D, blendColor, clipPolygonToRect, colorWithAlpha, createCanvasGradient, createPointView, degreesToRadians, formatColor, formatPremultipliedColor, isGradient, isPointWithinDistance, makeBoundingBoxFromRects, parseColor, parsePremultipliedColor, qrDecompose, radiansToDegrees, rectEdgePosition, transformPoint };
package/dist/matrix.d.ts CHANGED
@@ -1,5 +1,53 @@
1
- export declare function isIdentityMatrix(matrix: DOMMatrixReadOnly): boolean;
2
- export declare function qrDecompose(matrix: DOMMatrixReadOnly): {
1
+ /**
2
+ * 2D 仿射矩阵的只读视图,分量顺序与 Canvas `setTransform(a, b, c, d, e, f)` 相同:
3
+ * `x' = a·x + c·y + e`,`y' = b·x + d·y + f`。
4
+ */
5
+ export interface ReadonlyMatrix2D {
6
+ readonly a: number;
7
+ readonly b: number;
8
+ readonly c: number;
9
+ readonly d: number;
10
+ readonly e: number;
11
+ readonly f: number;
12
+ /** 是否为单位矩阵。 */
13
+ readonly isIdentity: boolean;
14
+ /** 返回 `this × other` 的新矩阵,即先应用 `other` 再应用当前矩阵。 */
15
+ multiply(other: ReadonlyMatrix2D): Matrix2D;
16
+ /** 返回逆矩阵;不可逆时全部分量为 `NaN`。 */
17
+ inverse(): Matrix2D;
18
+ }
19
+ /**
20
+ * Fulate 唯一的 2D 仿射矩阵。分量是普通 JS 字段,读写不经过宿主对象绑定;
21
+ * `*Self` 方法原地修改并返回自身,其余方法返回新矩阵。
22
+ *
23
+ * 每个运算结果都以 `+ 0` 规范化零的符号:规范仿射乘积总含 `0·0` 项,零分量为 `+0`;
24
+ * 保留 `-0` 会让 `atan2()` 等分解在 ±180° 之间翻转。
25
+ */
26
+ export declare class Matrix2D implements ReadonlyMatrix2D {
27
+ a: number;
28
+ b: number;
29
+ c: number;
30
+ d: number;
31
+ e: number;
32
+ f: number;
33
+ constructor(a?: number, b?: number, c?: number, d?: number, e?: number, f?: number);
34
+ /** 复制任意只读矩阵的六个分量。 */
35
+ static from(matrix: ReadonlyMatrix2D): Matrix2D;
36
+ get isIdentity(): boolean;
37
+ multiply(other: ReadonlyMatrix2D): Matrix2D;
38
+ inverse(): Matrix2D;
39
+ /** 原地右乘平移。 */
40
+ translateSelf(tx: number, ty?: number): this;
41
+ /** 原地右乘旋转;90° 的整数倍使用精确的 sin/cos。 */
42
+ rotateSelf(degrees: number): this;
43
+ /** 原地右乘缩放;省略 `sy` 时与 `sx` 相同。 */
44
+ scaleSelf(sx: number, sy?: number): this;
45
+ /** 原地右乘 x 轴错切(单位为度)。 */
46
+ skewXSelf(degrees: number): this;
47
+ /** 原地右乘 y 轴错切(单位为度)。 */
48
+ skewYSelf(degrees: number): this;
49
+ }
50
+ export declare function qrDecompose(matrix: ReadonlyMatrix2D): {
3
51
  angle: number;
4
52
  scaleX: number;
5
53
  scaleY: number;
package/dist/point.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { ReadonlyMatrix2D } from "./matrix";
1
2
  export type PointView = {
2
3
  readonly x: number;
3
4
  readonly y: number;
@@ -7,5 +8,5 @@ export type MutablePointView = {
7
8
  };
8
9
  export declare function createPointView(x: number, y: number): PointView;
9
10
  export declare function isPointWithinDistance(point: PointView, target: PointView, threshold: number): boolean;
10
- export declare function transformPoint(m: DOMMatrixReadOnly, point: PointView): PointView;
11
- export declare function transformPoint<T extends MutablePointView>(m: DOMMatrixReadOnly, point: PointView, out: T): T;
11
+ export declare function transformPoint(m: ReadonlyMatrix2D, point: PointView): PointView;
12
+ export declare function transformPoint<T extends MutablePointView>(m: ReadonlyMatrix2D, point: PointView, out: T): T;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fulate/share",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",