@lijuhong1981/jsmath 1.1.9 → 1.1.11

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/index.js CHANGED
@@ -55,6 +55,9 @@ import randomNumber from "./src/randomNumber.js";
55
55
  import randomInteger from "./src/randomInteger.js";
56
56
  import toFixed from "./src/toFixed.js";
57
57
  import Interval from "./src/Interval.js";
58
+ import windowPositionToNDC from "./src/windowPositionToNDC.js";
59
+ import getNDCInElement from "./src/getNDCInElement.js";
60
+ import ndcToWindowPosition from "./src/ndcToWindowPosition.js";
58
61
 
59
62
  export {
60
63
  PI,
@@ -113,4 +116,7 @@ export {
113
116
  randomInteger,
114
117
  toFixed,
115
118
  Interval,
119
+ windowPositionToNDC,
120
+ getNDCInElement,
121
+ ndcToWindowPosition,
116
122
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lijuhong1981/jsmath",
3
- "version": "1.1.9",
3
+ "version": "1.1.11",
4
4
  "description": "> TODO: description",
5
5
  "author": "lijuhong1981",
6
6
  "homepage": "https://github.com/lijuhong1981/jslibs#readme",
@@ -18,5 +18,5 @@
18
18
  "bugs": {
19
19
  "url": "https://github.com/lijuhong1981/jslibs/issues"
20
20
  },
21
- "gitHead": "728afda1d6b6237b89025658bcba18c18d58d94e"
21
+ "gitHead": "fde69a7f1188bc71f49cd03831c4265de45f8394"
22
22
  }
@@ -0,0 +1,14 @@
1
+ import windowPositionToNDC from "./windowPositionToNDC.js";
2
+
3
+ /**
4
+ * 根据一个HTMLElement中的像素位置计算NDC坐标(NormalizedDeviceCoordinates)
5
+ * @param {HTMLElement} element HTMLElement对象
6
+ * @param {Object} posInElement 在HTMLElement中的像素坐标,{x: 500, y: 300}
7
+ * @param {Object} result 输出结果对象
8
+ * @returns {Object} 输出结果对象, 如:{x: 0.5, y: -0.5}
9
+ */
10
+ function getNDCInElement(element, posInElement, result = {}) {
11
+ return windowPositionToNDC(posInElement, element.clientWidth, element.clientHeight, result);
12
+ };
13
+
14
+ export default getNDCInElement;
@@ -0,0 +1,30 @@
1
+ import Check from '@lijuhong1981/jscheck/src/Check.js';
2
+ import isValid from '@lijuhong1981/jscheck/src/isValid.js';
3
+
4
+ /**
5
+ * ndc坐标(NormalizedDeviceCoordinates)转换为窗口像素坐标
6
+ * @param {Object} ndc 需转换的ndc坐标, {x: 0.5, y: -0.5, z: 0.0}
7
+ * @param {Number} width 窗口像素宽
8
+ * @param {Number} height 窗口像素高
9
+ * @param {Object} result 输出结果对象
10
+ * @returns {Object} 输出结果对象, 如:{x: 123, y: 321}
11
+ */
12
+ function ndcToWindowPosition(ndc, width, height, result = {}) {
13
+ Check.typeOf.object('ndc', ndc);
14
+ Check.typeOf.number('width', width);
15
+ Check.typeOf.number('height', height);
16
+
17
+ // invisible
18
+ if (isValid(ndc.z) && (ndc.z < 0 || ndc.z > 1))
19
+ return;
20
+
21
+ const x = (ndc.x + 1) / 2 * width;
22
+ const y = (1 - ndc.y) / 2 * height;
23
+
24
+ result.x = x;
25
+ result.y = y;
26
+
27
+ return result;
28
+ }
29
+
30
+ export default ndcToWindowPosition;
@@ -0,0 +1,22 @@
1
+ import Check from '@lijuhong1981/jscheck/src/Check.js';
2
+
3
+ /**
4
+ * 窗口像素坐标转换为NDC坐标(NormalizedDeviceCoordinates)
5
+ * @param {Object} posInWindow 需转换的窗口像素坐标,{x: 500, y: 300}
6
+ * @param {Number} width 窗口像素宽
7
+ * @param {Number} height 窗口像素高
8
+ * @param {Object} result 输出结果对象
9
+ * @returns {Object} 输出结果对象, 如:{x: 0.5, y: -0.5}
10
+ */
11
+ function windowPositionToNDC(posInWindow, width, height, result = {}) {
12
+ Check.typeOf.object('posInWindow', posInWindow);
13
+ Check.typeOf.number('width', width);
14
+ Check.typeOf.number('height', height);
15
+
16
+ result.x = (posInWindow.x / width) * 2 - 1;
17
+ result.y = -(posInWindow.y / height) * 2 + 1;
18
+
19
+ return result;
20
+ };
21
+
22
+ export default windowPositionToNDC;