@shijiu/jsview 1.9.730 → 1.9.747

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.
Files changed (116) hide show
  1. package/dom/jsv-browser-debug-dom.js +1 -1
  2. package/dom/jsv-dom.js +1 -1
  3. package/dom/jsv-engine-js-browser.js +1 -1
  4. package/dom/jsv-forge-define.js +1 -1
  5. package/package.json +14 -17
  6. package/patches/node_modules/vite/dist/node/chunks/dep-0fc8e132.js +8 -1
  7. package/patches/node_modules/vite/dist/node/jsview.vite.config.js +12 -6
  8. package/dom/jsview-dom/README.md +0 -5
  9. package/dom/jsview-dom/package-lock.json-perfect +0 -3898
  10. package/dom/jsview-dom/package.json +0 -23
  11. package/dom/jsview-dom/rollup/browser-debug-dom.rollup.config.js +0 -15
  12. package/dom/jsview-dom/rollup/dom.rollup.config.js +0 -14
  13. package/dom/jsview-dom/rollup/engine-js-browser.rollup.config.js +0 -14
  14. package/dom/jsview-dom/rollup/forge-define.rollup.config.js +0 -14
  15. package/dom/jsview-dom/scripts/release_dist.sh +0 -36
  16. package/dom/jsview-dom/src/dom-browser-hook/HookDocument.js +0 -118
  17. package/dom/jsview-dom/src/dom-browser-hook/OriginDocument.js +0 -19
  18. package/dom/jsview-dom/src/dom-browser-hook/StyleFormatCheck.js +0 -559
  19. package/dom/jsview-dom/src/dom-browser-hook/index.js +0 -6
  20. package/dom/jsview-dom/src/dom-wrapper/ForgeExtension.js +0 -226
  21. package/dom/jsview-dom/src/dom-wrapper/JsViewForgeApp.js +0 -85
  22. package/dom/jsview-dom/src/dom-wrapper/JsViewProxy.js +0 -51
  23. package/dom/jsview-dom/src/dom-wrapper/event/AnimationEvent.js +0 -7
  24. package/dom/jsview-dom/src/dom-wrapper/event/Event.js +0 -12
  25. package/dom/jsview-dom/src/dom-wrapper/event/FocusEvent.js +0 -9
  26. package/dom/jsview-dom/src/dom-wrapper/event/KeyboardEvent.js +0 -43
  27. package/dom/jsview-dom/src/dom-wrapper/event/LoadEvent.js +0 -8
  28. package/dom/jsview-dom/src/dom-wrapper/index.js +0 -16
  29. package/dom/jsview-dom/src/dom-wrapper/node/AnchorElement.js +0 -24
  30. package/dom/jsview-dom/src/dom-wrapper/node/AudioElement.js +0 -60
  31. package/dom/jsview-dom/src/dom-wrapper/node/Comment.js +0 -10
  32. package/dom/jsview-dom/src/dom-wrapper/node/DivElement.js +0 -498
  33. package/dom/jsview-dom/src/dom-wrapper/node/Document.js +0 -274
  34. package/dom/jsview-dom/src/dom-wrapper/node/Element.js +0 -852
  35. package/dom/jsview-dom/src/dom-wrapper/node/FDivElement.js +0 -48
  36. package/dom/jsview-dom/src/dom-wrapper/node/HeadElement.js +0 -47
  37. package/dom/jsview-dom/src/dom-wrapper/node/ImageElement.js +0 -203
  38. package/dom/jsview-dom/src/dom-wrapper/node/JsvAudioTrackElement.js +0 -22
  39. package/dom/jsview-dom/src/dom-wrapper/node/JsvElement.js +0 -40
  40. package/dom/jsview-dom/src/dom-wrapper/node/LinkElement.js +0 -48
  41. package/dom/jsview-dom/src/dom-wrapper/node/MediaElement.js +0 -230
  42. package/dom/jsview-dom/src/dom-wrapper/node/Node.js +0 -178
  43. package/dom/jsview-dom/src/dom-wrapper/node/SVGElement.js +0 -9
  44. package/dom/jsview-dom/src/dom-wrapper/node/ScriptElement.js +0 -45
  45. package/dom/jsview-dom/src/dom-wrapper/node/StyleElement.js +0 -33
  46. package/dom/jsview-dom/src/dom-wrapper/node/StyleElementCache.js +0 -41
  47. package/dom/jsview-dom/src/dom-wrapper/node/Text.js +0 -22
  48. package/dom/jsview-dom/src/dom-wrapper/node/UnknownElement.js +0 -4
  49. package/dom/jsview-dom/src/dom-wrapper/node/VideoElement.js +0 -237
  50. package/dom/jsview-dom/src/dom-wrapper/style/BorderImage.js +0 -25
  51. package/dom/jsview-dom/src/dom-wrapper/style/Inset.js +0 -26
  52. package/dom/jsview-dom/src/dom-wrapper/style/JsvStyleVariable.js +0 -65
  53. package/dom/jsview-dom/src/dom-wrapper/style/KeyframeRule.js +0 -11
  54. package/dom/jsview-dom/src/dom-wrapper/style/StyleDeclaration.js +0 -609
  55. package/dom/jsview-dom/src/dom-wrapper/style/StyleSheet.js +0 -51
  56. package/dom/jsview-dom/src/dom-wrapper/style/StyleValue.js +0 -385
  57. package/dom/jsview-dom/src/dom-wrapper/style/TextStylePackMap.js +0 -43
  58. package/dom/jsview-dom/src/dom-wrapper/style/URL.js +0 -144
  59. package/dom/jsview-dom/src/dom-wrapper/utils/EventHandler.js +0 -42
  60. package/dom/jsview-dom/src/dom-wrapper/utils/FDivRoot.js +0 -86
  61. package/dom/jsview-dom/src/dom-wrapper/utils/JsvLazySyncCache.js +0 -64
  62. package/dom/jsview-dom/src/dom-wrapper/utils/Log.js +0 -42
  63. package/dom/jsview-dom/src/dom-wrapper/utils/MutationObserver.js +0 -32
  64. package/dom/jsview-dom/src/dom-wrapper/utils/focusableNode.js +0 -715
  65. package/dom/jsview-dom/src/engine-js/ForgeDefine.js +0 -8
  66. package/dom/jsview-dom/src/engine-js/ForgeExtensionDefine.js +0 -14
  67. package/dom/jsview-dom/src/engine-js/browser/PlatformUtils.js +0 -100
  68. package/dom/jsview-dom/src/engine-js/browser/animation_base.js +0 -313
  69. package/dom/jsview-dom/src/engine-js/browser/animation_keyframe.js +0 -64
  70. package/dom/jsview-dom/src/engine-js/browser/animation_progress.js +0 -287
  71. package/dom/jsview-dom/src/engine-js/browser/animation_proxy.js +0 -906
  72. package/dom/jsview-dom/src/engine-js/browser/apic_decoder/demux.js +0 -143
  73. package/dom/jsview-dom/src/engine-js/browser/apic_decoder/gifDecoder.js +0 -12
  74. package/dom/jsview-dom/src/engine-js/browser/apic_decoder/libwebp-0.6.0.min.js +0 -160
  75. package/dom/jsview-dom/src/engine-js/browser/console_log.js +0 -25
  76. package/dom/jsview-dom/src/engine-js/browser/dynamic_key_frames.js +0 -95
  77. package/dom/jsview-dom/src/engine-js/browser/easing.js +0 -114
  78. package/dom/jsview-dom/src/engine-js/browser/html_shared_tools/element_transform.js +0 -122
  79. package/dom/jsview-dom/src/engine-js/browser/html_shared_tools/gjk.js +0 -450
  80. package/dom/jsview-dom/src/engine-js/browser/html_shared_tools/mat.js +0 -102
  81. package/dom/jsview-dom/src/engine-js/browser/html_shared_tools/range_model.js +0 -296
  82. package/dom/jsview-dom/src/engine-js/browser/html_shared_tools/test_collide.js +0 -10
  83. package/dom/jsview-dom/src/engine-js/browser/index.js +0 -38
  84. package/dom/jsview-dom/src/engine-js/browser/latex_parse.js +0 -336
  85. package/dom/jsview-dom/src/engine-js/browser/layout_params.js +0 -113
  86. package/dom/jsview-dom/src/engine-js/browser/layout_view.js +0 -3545
  87. package/dom/jsview-dom/src/engine-js/browser/layout_view_debug.js +0 -15
  88. package/dom/jsview-dom/src/engine-js/browser/media.js +0 -379
  89. package/dom/jsview-dom/src/engine-js/browser/page_base.js +0 -941
  90. package/dom/jsview-dom/src/engine-js/browser/particle_view.js +0 -526
  91. package/dom/jsview-dom/src/engine-js/browser/platform_timer.js +0 -61
  92. package/dom/jsview-dom/src/engine-js/browser/react_utils.js +0 -5
  93. package/dom/jsview-dom/src/engine-js/browser/rect_utils.js +0 -91
  94. package/dom/jsview-dom/src/engine-js/browser/render_bridge.js +0 -128
  95. package/dom/jsview-dom/src/engine-js/browser/render_texture_proxy.js +0 -155
  96. package/dom/jsview-dom/src/engine-js/browser/renderer_deprecated.js +0 -75
  97. package/dom/jsview-dom/src/engine-js/browser/sound_pool.js +0 -139
  98. package/dom/jsview-dom/src/engine-js/browser/steps_animation.js +0 -192
  99. package/dom/jsview-dom/src/engine-js/browser/text_style_cache.js +0 -454
  100. package/dom/jsview-dom/src/engine-js/browser/text_utils.js +0 -299
  101. package/dom/jsview-dom/src/engine-js/browser/text_view.js +0 -428
  102. package/dom/jsview-dom/src/engine-js/browser/texture_manager.js +0 -1060
  103. package/dom/jsview-dom/src/engine-js/browser/url.js +0 -69
  104. package/dom/jsview-dom/src/engine-js/browser/velocity.js +0 -40
  105. package/dom/jsview-dom/src/engine-js/browser/view_sensor/autofroze_sensor.js +0 -77
  106. package/dom/jsview-dom/src/engine-js/browser/view_sensor/drag_impact_sensor.js +0 -67
  107. package/dom/jsview-dom/src/engine-js/browser/view_sensor/impact_sensor.js +0 -98
  108. package/dom/jsview-dom/src/engine-js/browser/view_sensor/impact_sensor_callback.js +0 -21
  109. package/dom/jsview-dom/src/engine-js/browser/view_sensor/impact_sensor_manager.js +0 -24
  110. package/dom/jsview-dom/src/engine-js/browser/view_sensor/index.js +0 -9
  111. package/dom/jsview-dom/src/engine-js/browser/view_sensor/view_sensor.js +0 -268
  112. package/dom/jsview-dom/src/engine-js/browser/view_sensor/view_sensor_manager.js +0 -84
  113. package/dom/jsview-dom/src/engine-js/browser/view_sensor/visible_sensor.js +0 -213
  114. package/dom/jsview-dom/src/engine-js/browser/view_store.js +0 -41
  115. package/dom/jsview-dom/src/engine-js/index.js +0 -2
  116. package/dom/jsview-dom/src/engine-js/native/README.md +0 -1
@@ -1,114 +0,0 @@
1
- import Forge from "../ForgeDefine";
2
-
3
- Forge.EasingTypeIn = 0;
4
- Forge.EasingTypeOut = 1;
5
- Forge.EasingTypeInOut = 2;
6
- Forge.EasingTypeBezier = 3;
7
- Forge.EasingTypeBlink = 4;
8
- Forge.EasingTypeDeceleration = 5;
9
- Forge.EasingTypeSteps = 6;
10
- class EasingBase {
11
- constructor() {
12
- this.Pacakge = this.Package; // 兼容老版本的拼写错误
13
- }
14
-
15
- Package() {
16
- }
17
-
18
- _GetBasePackage(type) {
19
- return {
20
- T: type,
21
- St: null
22
- };
23
- }
24
- }
25
- Forge.EasingBase = EasingBase;
26
-
27
- class EasingIn extends Forge.EasingBase {
28
- Package() {
29
- return this._GetBasePackage(Forge.EasingTypeIn);
30
- }
31
- }
32
- Forge.EasingIn = EasingIn;
33
-
34
- class EasingOut extends Forge.EasingBase {
35
- Package() {
36
- return this._GetBasePackage(Forge.EasingTypeOut);
37
- }
38
- }
39
- Forge.EasingOut = EasingOut;
40
-
41
- class EasingInOut extends Forge.EasingBase {
42
- Package() {
43
- return this._GetBasePackage(Forge.EasingTypeInOut);
44
- }
45
- }
46
- Forge.EasingInOut = EasingInOut;
47
-
48
- class EasingDeceleration extends Forge.EasingBase {
49
- Package() {
50
- return this._GetBasePackage(Forge.EasingTypeDeceleration);
51
- }
52
- }
53
- Forge.EasingDeceleration = EasingDeceleration;
54
-
55
- class EasingBlink extends Forge.EasingBase {
56
- Package() {
57
- return this._GetBasePackage(Forge.EasingTypeBlink);
58
- }
59
- }
60
- Forge.EasingBlink = EasingBlink;
61
-
62
- class BezierEasing extends Forge.EasingBase {
63
- constructor(x1, y1, x2, y2, duration) {
64
- super();
65
- this._Inited = false;
66
- this._X1 = x1;
67
- this._X2 = x2;
68
- this._Y1 = y1;
69
- this._Y2 = y2;
70
- this._Duration = duration;
71
- }
72
-
73
- Package() {
74
- const data = this._GetBasePackage(Forge.EasingTypeBezier);
75
- data.St = {
76
- X1: this._X1,
77
- X2: this._X2,
78
- Y1: this._Y1,
79
- Y2: this._Y2,
80
- Dur: this._Duration
81
- };
82
- return data;
83
- }
84
- }
85
- Forge.BezierEasing = BezierEasing;
86
-
87
- class StepsEasing extends Forge.EasingBase {
88
- constructor(steps, type) {
89
- super();
90
- this._Steps = steps;
91
- this._Type = type;
92
- }
93
-
94
- Package() {
95
- const data = this._GetBasePackage(Forge.EasingTypeSteps);
96
- data.St = {
97
- S: this._Steps,
98
- T: this._Type
99
- };
100
- return data;
101
- }
102
- }
103
- Forge.StepsEasing = StepsEasing;
104
-
105
- Forge.Easing =
106
- {
107
- Circular: {
108
- In: new Forge.EasingIn(),
109
- Out: new Forge.EasingOut(),
110
- InOut: new Forge.EasingInOut(),
111
- Deceleration: new Forge.EasingDeceleration(),
112
- }
113
- };
114
- window.Easing = Forge.Easing; // Export object
@@ -1,122 +0,0 @@
1
- import Mat from './mat';
2
-
3
- const is_char = char_code => (char_code >= 65 && char_code <= 90) || (char_code >= 97 && char_code <= 122);
4
- const is_num = char_code => char_code >= 48 && char_code <= 57;
5
- const func_to_mat = (name, params) => {
6
- switch (name) {
7
- case "translate3d":
8
- return Mat.translate(params[0], params[1], params[2]);
9
- case "scale3d":
10
- return Mat.scale(params[0], params[1], params[2]);
11
- case "rotate3d":
12
- return Mat.rotate(params[0], params[1], params[2], params[3]);
13
- case "skew":
14
- return Mat.skew(params[0], params[1]);
15
- case "matrix":
16
- return Mat.transform2d(params[0], params[1], params[2], params[3], params[4], params[5]);
17
- case "matrix3d":
18
- return new Mat(4, 4, params);
19
- default:
20
- throw new Error(`no support type ${name}`);
21
- }
22
- };
23
-
24
- const parseToMat4 = (transform) => {
25
- let result = Mat.identity();
26
- if (!transform) {
27
- return result;
28
- }
29
- let func_name = "";
30
- let param = "";
31
- let param_list = [];
32
- let mode = 0; // 0 parse name; 1 parse params;
33
- for (let i = 0; i < transform.length; i++) {
34
- const char_code = transform.charCodeAt(i);
35
- if (mode === 0) {
36
- if (is_char(char_code) || is_num(char_code)) {
37
- func_name += transform[i];
38
- }
39
- } else {
40
- if (is_num(char_code) || char_code === 46 || char_code === 45 || char_code === 101) { // 科学计数
41
- param += transform[i];
42
- }
43
- }
44
-
45
- if (char_code === 40) { // (
46
- mode = 1;
47
- param = "";
48
- param_list = [];
49
- } else if (char_code === 41) { // )
50
- param_list.push(parseFloat(param));
51
- result = result.multiply(func_to_mat(func_name, param_list));
52
- func_name = "";
53
- mode = 0;
54
- } else if (char_code === 44) { // ,
55
- param_list.push(parseFloat(param));
56
- param = "";
57
- }
58
- }
59
- return result;
60
- };
61
-
62
- const pxToNum = px => {
63
- if (px.indexOf(" ") >= 0) {
64
- const list = px.split(" ");
65
- return list.map(str => parseInt(str.substr(0, str.length - 2), 10));
66
- }
67
- return parseInt(px.substr(0, px.length - 2), 10);
68
- };
69
-
70
- const getTransform = (ele) => {
71
- let cur_element = ele;
72
- const ele_width = pxToNum(cur_element.style.width);
73
- const ele_height = pxToNum(cur_element.style.height);
74
- let total_transform = Mat.identity();
75
- while (cur_element.parentElement) {
76
- const style = getComputedStyle(cur_element);
77
- const transform_str = style.transform ? style.transform : style.webkitTransform;
78
- if (transform_str) {
79
- const origin_str = style.transformOrigin ? style.transformOrigin : style.webkitTransformOrigin;
80
- const transform = parseToMat4(transform_str);
81
- if (origin_str) {
82
- const list = pxToNum(origin_str);
83
- const translate1 = Mat.translate(-list[0], -list[1], 0);
84
- const translate2 = Mat.translate(list[0], list[1], 0);
85
- const translate3 = Mat.translate(cur_element.offsetLeft, cur_element.offsetTop, 0);
86
- total_transform = translate3.multiply(translate2.multiply(transform.multiply(translate1.multiply(total_transform))));
87
- } else {
88
- total_transform = transform.multiply(total_transform);
89
- }
90
- }
91
- cur_element = cur_element.parentElement;
92
- }
93
- const size_matrix = new Mat(4, 4, [
94
- 0, 0, 0, 1,
95
- ele_width, 0, 0, 1,
96
- 0, ele_height, 0, 1,
97
- ele_width, ele_height, 0, 1
98
- ]);
99
- total_transform = total_transform.multiply(size_matrix);
100
-
101
- const points = total_transform.element;
102
- // 左上,右上,左下,右下的x,y坐标
103
- const result = [points[0], points[1], points[4], points[5], points[8], points[9], points[12], points[13]];
104
- return result;
105
- };
106
-
107
- // 获取能完全遮盖此element的正矩形(横轴水平,纵轴垂直,不倾斜)
108
- function getCoverSize(ele) {
109
- let coord_array = getTransform(ele);
110
-
111
- let left = Math.min(coord_array[0], coord_array[2], coord_array[4], coord_array[6]);
112
- let right = Math.max(coord_array[0], coord_array[2], coord_array[4], coord_array[6]);
113
- let top = Math.min(coord_array[1], coord_array[3], coord_array[5], coord_array[7]);
114
- let bottom = Math.max(coord_array[1], coord_array[3], coord_array[5], coord_array[7]);
115
-
116
- return [top, bottom, left, right];
117
- }
118
-
119
- export {
120
- getTransform,
121
- getCoverSize
122
- }
@@ -1,450 +0,0 @@
1
- class Vector2 {
2
- constructor(x, y) {
3
- this.set(x, y);
4
- }
5
-
6
- add(x, y) {
7
- if (x instanceof Vector2) {
8
- this.x += x.x;
9
- this.y += x.y;
10
- } else {
11
- this.x += x || 0;
12
- this.y += y || 0;
13
- }
14
- return this;
15
- }
16
-
17
- // ## angle
18
- // ### angle()
19
- // Returns the angle in radians of this vector
20
- // relative to the x-axis (counter-clockwise)
21
- // in the range 0 to 2 * PI.
22
- // ### angle(radians)
23
- // Rotates this vector to the given angle in radians.
24
- // Returns this vector for chaining.
25
- angle(rad) {
26
- if (rad !== undefined) { return this.set(this.length(), 0).rotate(rad); }
27
- let angle = Math.atan2(this.y, this.x);
28
- if (angle < 0) angle += Math.PI * 2;
29
- return angle;
30
- }
31
-
32
- // ## angleDeg
33
- // ### angleDeg()
34
- // Same as angle() but in degrees.
35
- // ### angleDeg(degrees)
36
- // Same as angle(radians) but in degrees.
37
- angleDeg(deg) {
38
- if (deg !== undefined) { return this.angle(deg / 180 * Math.PI); }
39
- return this.angle() * 180 / Math.PI;
40
- }
41
-
42
- // ## clone()
43
- // ## copy()
44
- // Returns a new identical Vector2.
45
- clone() {
46
- return new Vector2(this.x, this.y);
47
- }
48
-
49
- // ## cross(Vector2)
50
- // ## cross(x, y)
51
- // Returns the cross product of this vector and another.
52
- cross(x, y) {
53
- if (x instanceof Vector2) { return this.x * x.y - this.y * x.x; }
54
- return this.x * y - this.y * x;
55
- }
56
-
57
- // ## distance(Vector2)
58
- // ## distance(x, y)
59
- // Returns the distance between this vector and another.
60
- distance(x, y) {
61
- const distSq = this.distanceSq(x, y);
62
- if (distSq === undefined) { return undefined; }
63
- return Math.sqrt(distSq);
64
- }
65
-
66
- // ## distanceSq(Vector2)
67
- // ## distanceSq(x, y)
68
- // Returns the distance squared of this vector and another.
69
- distanceSq(x, y) {
70
- let dx;
71
- let dy;
72
- if (x instanceof Vector2) {
73
- dx = x.x - this.x;
74
- dy = x.y - this.y;
75
- } else if (y !== undefined) {
76
- dx = x - this.x;
77
- dy = y - this.y;
78
- } else { return undefined; }
79
- return dx * dx + dy * dy;
80
- }
81
-
82
- // ## dot(Vector2)
83
- // ## dot(x, y)
84
- // Returns the dot product of this vector and another.
85
- dot(x, y) {
86
- if (x instanceof Vector2) { return this.x * x.x + this.y * x.y; }
87
- return this.x * x + this.y * y;
88
- }
89
-
90
- // ## equals
91
- // ### equals(Vector2)
92
- // Returns true if this and another vector2 are equal.
93
- // ### equals(Vector2, epsilon)
94
- // Returns true if this and another vector2 are equal within an epsilon.
95
- // ### equals(x, y)
96
- // Returns true if this vector equals given x, y components.
97
- // ### equals(x, y, epsilon)
98
- // Returns true if this vector equals given x, y components within an epsilon.
99
- equals(x, y, epsilon) {
100
- if (x instanceof Vector2) {
101
- y = y || 0;
102
- return Math.abs(this.x - x.x) <= y && Math.abs(this.y - x.y) <= y;
103
- }
104
- if (y !== undefined) {
105
- epsilon = epsilon || 0;
106
- return Math.abs(this.x - x) <= epsilon && Math.abs(this.y - y) <= epsilon;
107
- }
108
- return false;
109
- }
110
-
111
- // ## length()
112
- // Returns the length of this vector.
113
- length() {
114
- return Math.sqrt(this.lengthSq());
115
- }
116
-
117
- // ## lengthSq()
118
- // Returns the length squared of this vector.
119
- lengthSq() {
120
- return this.x * this.x + this.y * this.y;
121
- }
122
-
123
- // ## negate()
124
- // Negates this vector. (Multiplies x and y by -1).
125
- // Returns this vector for chaining.
126
- negate() {
127
- return this.scale(-1);
128
- }
129
-
130
- // ##normalize()
131
- // Normalizes this vector.
132
- // Returns this vector for chaining.
133
- normalize() {
134
- return this.scale(1 / this.length());
135
- }
136
-
137
- // ## rotate(radians)
138
- // Rotates this vector by an angle in degrees counter-clockwise.
139
- // Returns this vector for chaining.
140
- rotate(rad) {
141
- const cos = Math.cos(rad);
142
- const sin = Math.sin(rad);
143
- return this.set(this.x * cos - this.y * sin,
144
- this.x * sin + this.y * cos);
145
- }
146
-
147
- // ## rotateDeg(degrees)
148
- // Same as rotate but in degrees.
149
- rotateDeg(deg) {
150
- return this.rotate(deg / 180 * Math.PI);
151
- }
152
-
153
- // ## scale(scale)
154
- // ## scale(scaleX, scaleY)
155
- // Scales this vector by a scalar.
156
- // Second argument to scale y separate from x is optional.
157
- // Returns this vector for chaining.
158
- scale(scaleX, scaleY) {
159
- this.x *= scaleX;
160
- this.y *= (scaleY || scaleX);
161
- return this;
162
- }
163
-
164
- // ## set(Vector2)
165
- // ## set(x, y)
166
- // Sets this vector to the given values.
167
- // Returns this vector for chaining.
168
- set(x, y) {
169
- if (x instanceof Vector2) {
170
- this.x = x.x;
171
- this.y = x.y;
172
- } else {
173
- this.x = x || 0;
174
- this.y = y || 0;
175
- }
176
- return this;
177
- }
178
-
179
- // ## setPolar(radians, length)
180
- // Set this vector by angle in degrees and magnitude.
181
- // Returns this vector for chaining.
182
- setPolar(rad, length) {
183
- return this.set(length, 0).rotate(rad);
184
- }
185
-
186
- // ## setPolarDeg(degrees, length)
187
- // Same as setPolar but in degrees.
188
- setPolarDeg(deg, length) {
189
- return this.setPolar(deg / 180 * Math.PI, length);
190
- }
191
-
192
- sub(x, y) {
193
- if (y !== undefined) {
194
- this.x -= x;
195
- this.y -= y;
196
- } else {
197
- this.x -= x.x;
198
- this.y -= x.y;
199
- }
200
- return this;
201
- }
202
-
203
- toString() {
204
- return `(${this.x}, ${this.y})`;
205
- }
206
- }
207
-
208
- function isEqual(number1, number2, tolerance) {
209
- tolerance = tolerance || 0;
210
- return Math.abs(number1 - number2) < Math.abs(tolerance);
211
- }
212
-
213
- const mathUtils = {
214
- isEqual,
215
-
216
- isZero(number, tolerance) {
217
- return isEqual(number, 0, tolerance);
218
- },
219
-
220
- /**
221
- * Clamps the passed value to the passed bounds (i.e. if value is smaller than min bound it's set to min bound, if bigger than max bound it's set to max bound).
222
- * @param value
223
- * @param min
224
- * @param max
225
- * @returns {*}
226
- */
227
- clamp(value, min, max) {
228
- if (value > min) {
229
- return value < max ? value : max;
230
- }
231
- return min;
232
- }
233
- };
234
-
235
- const isZero = mathUtils.isZero;
236
- const clamp = mathUtils.clamp;
237
-
238
-
239
- // The value of 1/3
240
- const inv3 = 1.0 / 3.0;
241
- const origin = new Vector2(0, 0);
242
- // tolerance for number comparison
243
- const tolerance = 0.0001;
244
-
245
- // the iterations count after which the result will be 0;
246
- const defaultLoopIterations = 30;
247
-
248
- function isArray(array) {
249
- return Object.prototype.toString.call(array) === '[object Array]';
250
- }
251
-
252
-
253
- function vectorLikeToVectorPolygon(polygon) {
254
- if (!isArray(polygon)) {
255
- throw new Error("Polygon must be an array of points");
256
- }
257
-
258
- switch (typeof polygon[0]) {
259
- case 'number': {
260
- if (polygon.length % 2 !== 0) {
261
- throw new Error("Polygon that contains array of numbers must has even length ([x0, y0, x1, y1, ...., xn, yn])");
262
- }
263
-
264
- const result = [];
265
-
266
- for (let i = 0; i < polygon.length; i += 2) {
267
- result.push(new Vector2(polygon[i], polygon[i + 1]));
268
- }
269
- return result;
270
- }
271
- case 'object': {
272
- return polygon.map(
273
- isArray(polygon[0]) ?
274
- point => new Vector2(point[0], point[1]) :
275
- point => new Vector2(point.x, point.y)
276
- );
277
- }
278
- default:
279
- throw new Error("Passed polygon is not an array of 2D coordinates");
280
- }
281
- }
282
-
283
- function getAreaWeightedCenter(polygon) {
284
- if (polygon.length === 1) {
285
- return new Vector2(polygon[0]);
286
- }
287
-
288
- const ac = new Vector2(0.0, 0.0);
289
-
290
- polygon.forEach(vector => ac.add(vector));
291
- ac.scale(1 / polygon.length);
292
-
293
- const center = new Vector2();
294
- let area = 0.0;
295
-
296
- polygon.forEach((vector, index) => {
297
- const p1 = new Vector2(vector);
298
- const p2 = new Vector2(index + 1 === polygon.length ? polygon[0] : polygon[index + 1]);
299
-
300
-
301
- p1.add(-ac.x, -ac.y);
302
- p2.add(-ac.x, -ac.y);
303
-
304
- const triangleArea = 0.5 * p1.cross(p2);
305
- area += triangleArea;
306
-
307
- center.add(p1.add(p2).scale(inv3).scale(triangleArea));
308
- });
309
-
310
- if (isZero(area, tolerance)) {
311
- // zero area can only happen if all the points are the same point
312
- // in which case just return a copy of the first
313
- return center.set(polygon[0]);
314
- }
315
-
316
- // return the center
317
- return center.scale(1 / area).add(ac);
318
- }
319
-
320
- function getFarthestPointInDirection(polygon, direction) {
321
- let farthestPoint = polygon[0];
322
- let farthestDistance = direction.dot(polygon[0]);
323
- let tempDist = 0;
324
-
325
- for (let i = 1; i < polygon.length; i++) {
326
- tempDist = direction.dot(polygon[i]);
327
- if (tempDist > farthestDistance) {
328
- farthestDistance = tempDist;
329
- farthestPoint = polygon[i];
330
- }
331
- }
332
- return new Vector2(farthestPoint);
333
- }
334
-
335
- function support(polygon1, polygon2, direction) {
336
- const point1 = getFarthestPointInDirection(polygon1, direction);
337
- const point2 = getFarthestPointInDirection(polygon2, direction.negate());
338
- return new Vector2(point1.sub(point2));
339
- }
340
-
341
-
342
- function closestPointOnSegmentToOrigin(vector1, vector2) {
343
- const closest = new Vector2(0.0, 0.0);
344
- // vector from point to the origin
345
- const vector1ToOrigin = new Vector2(vector1).negate();
346
- // vector representing the line
347
- const lineVector1Vector2 = new Vector2(vector2).sub(vector1);
348
-
349
- // get the length squared of the line
350
- const lineV1V2Dot = lineVector1Vector2.dot(lineVector1Vector2);
351
- // if a === b
352
- if (isZero(lineV1V2Dot, tolerance)) {
353
- return closest.set(vector1);
354
- }
355
-
356
- // projection of aToOrigin on lineAB
357
- const v1ToOrigin_V1V2 = vector1ToOrigin.dot(lineVector1Vector2);
358
- // get the position from the first line point to the projection
359
- let t = v1ToOrigin_V1V2 / lineV1V2Dot;
360
- // make sure t is in between 0.0 and 1.0
361
- t = clamp(t, 0.0, 1.0);
362
- return closest.set(lineVector1Vector2.scale(t).add(vector1));
363
- }
364
-
365
-
366
- function distance(polygon1, polygon2) {
367
- polygon1 = vectorLikeToVectorPolygon(polygon1);
368
- polygon2 = vectorLikeToVectorPolygon(polygon2);
369
-
370
- const centerP1 = getAreaWeightedCenter(polygon1);
371
- const centerP2 = getAreaWeightedCenter(polygon2);
372
-
373
-
374
- const direction = centerP2.add(centerP1.negate());
375
-
376
- let a;
377
- let b;
378
- let c;
379
-
380
- a = support(polygon1, polygon2, direction);
381
- b = support(polygon1, polygon2, direction.negate());
382
-
383
- for (let i = 0; i < defaultLoopIterations; i++) {
384
- const p = closestPointOnSegmentToOrigin(a, b);
385
-
386
- if (isZero(p.length(), tolerance)) {
387
- // the origin is on the Minkowski Difference
388
- // I consider this touching/collision
389
- return 0.0;
390
- }
391
-
392
- // p.to(origin) is the new direction
393
- // we normalize here because we need to check the
394
- // projections along this vector later
395
- direction.set(p.negate().normalize());
396
- c = support(polygon1, polygon2, direction);
397
- // is the point we obtained making progress
398
- // towards the goal (to get the closest points
399
- // to the origin)
400
- const dc = c.dot(direction);
401
- // you can use a or b here it doesn't matter
402
- const da = a.dot(direction);
403
-
404
- if (isZero(dc - da, tolerance)) {
405
- return dc;
406
- }
407
-
408
- // if we are still getting closer then only keep
409
- // the points in the simplex that are closest to
410
- // the origin (we already know that c is closer
411
- // than both a and b)
412
- if (a.distanceSq(origin) < b.distanceSq(origin)) {
413
- b = c;
414
- } else {
415
- a = c;
416
- }
417
- }
418
-
419
- return 0.0;
420
- }
421
-
422
- const gjk = {
423
-
424
- /**
425
- * Checks if two convex polygons intersects.
426
- * Polygons must be an arrays with elements of one of next formats:
427
- * 1. [x1, y2, x2, y2, ..., xn, yn]
428
- * 2. [{x, y}, {x, y}, ..., {x, y}]
429
- * 3. [[x, y], [x, y], ..., [x, y]]
430
- * @param {Array.<Object|Number|Array.<Number>>}polygon1
431
- * @param {Array.<Object|Number|Array.<Number>>}polygon2
432
- * @returns {boolean}
433
- */
434
- intersect(polygon1, polygon2) {
435
- return isZero(distance(polygon1, polygon2), tolerance);
436
- },
437
-
438
- /**
439
- * Calculates distance between 2 convex polygons.
440
- * Polygons must be an arrays with elements of one of next formats:
441
- * 1. [x1, y2, x2, y2, ..., xn, yn]
442
- * 2. [{x, y}, {x, y}, ..., {x, y}]
443
- * 3. [[x, y], [x, y], ..., [x, y]]
444
- * @param {Array.<Object|Number|Array.<Number>>}polygon1
445
- * @param {Array.<Object|Number|Array.<Number>>}polygon2
446
- * @returns {number}
447
- */
448
- distance
449
- };
450
- export default gjk;