vislite 0.6.0-alpha.2 → 0.6.0
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/.eslintrc.js +3 -1
- package/CHANGELOG +1 -1
- package/README.md +11 -3
- package/lib/Buffer/index.umd.js +2 -2
- package/lib/Buffer/index.umd.min.js +2 -2
- package/lib/Canvas/index.umd.js +2 -2
- package/lib/Canvas/index.umd.min.js +2 -2
- package/lib/Cardinal/index.umd.js +2 -2
- package/lib/Cardinal/index.umd.min.js +2 -2
- package/lib/Eoap/index.umd.js +2 -2
- package/lib/Eoap/index.umd.min.js +2 -2
- package/lib/Geometry/index.umd.js +2 -2
- package/lib/Geometry/index.umd.min.js +2 -2
- package/lib/Hermite/index.umd.js +2 -2
- package/lib/Hermite/index.umd.min.js +2 -2
- package/lib/Matrix4/index.umd.js +2 -2
- package/lib/Matrix4/index.umd.min.js +2 -2
- package/lib/Mercator/index.umd.js +2 -2
- package/lib/Mercator/index.umd.min.js +2 -2
- package/lib/OralCanvas/index.es.js +2 -2
- package/lib/OralCanvas/index.es.min.js +2 -2
- package/lib/OralWebGL/index.es.js +2 -2
- package/lib/OralWebGL/index.es.min.js +2 -2
- package/lib/SVG/index.umd.js +2 -2
- package/lib/SVG/index.umd.min.js +2 -2
- package/lib/Shader/index.umd.js +2 -2
- package/lib/Shader/index.umd.min.js +2 -2
- package/lib/Texture/index.umd.js +2 -2
- package/lib/Texture/index.umd.min.js +2 -2
- package/lib/TreeLayout/index.umd.js +2 -2
- package/lib/TreeLayout/index.umd.min.js +2 -2
- package/lib/WebGL/index.umd.js +2 -2
- package/lib/WebGL/index.umd.min.js +2 -2
- package/lib/animation/index.umd.js +2 -2
- package/lib/animation/index.umd.min.js +2 -2
- package/lib/formatColor/index.umd.js +2 -2
- package/lib/formatColor/index.umd.min.js +2 -2
- package/lib/getLoopColors/index.umd.js +2 -2
- package/lib/getLoopColors/index.umd.min.js +2 -2
- package/lib/getWebGLContext/index.umd.js +2 -2
- package/lib/getWebGLContext/index.umd.min.js +2 -2
- package/lib/index.umd.js +2 -2
- package/lib/index.umd.min.js +2 -2
- package/lib/move/index.umd.js +2 -2
- package/lib/move/index.umd.min.js +2 -2
- package/lib/resizeObserver/index.umd.js +2 -2
- package/lib/resizeObserver/index.umd.min.js +2 -2
- package/lib/rotate/index.umd.js +2 -2
- package/lib/rotate/index.umd.min.js +2 -2
- package/lib/ruler/index.umd.js +2 -2
- package/lib/ruler/index.umd.min.js +2 -2
- package/lib/scale/index.umd.js +2 -2
- package/lib/scale/index.umd.min.js +2 -2
- package/lib/throttle/index.umd.js +2 -2
- package/lib/throttle/index.umd.min.js +2 -2
- package/lib/viewHandler/index.umd.js +2 -2
- package/lib/viewHandler/index.umd.min.js +2 -2
- package/package.json +1 -1
- package/src/Canvas.ts +70 -70
- package/src/Eoap.ts +66 -66
- package/src/Geometry.ts +82 -82
- package/src/Matrix4/move.ts +12 -12
- package/src/Matrix4/rotate.ts +15 -15
- package/src/Matrix4/transform.ts +64 -64
- package/src/Mercator.ts +27 -27
- package/src/SVG.ts +24 -24
- package/src/WebGL.ts +66 -66
- package/src/animation.ts +98 -98
- package/src/common/assemble.ts +22 -22
- package/src/common/canvas/arc.ts +51 -51
- package/src/common/canvas/config.ts +158 -158
- package/src/common/canvas/gradient.ts +29 -29
- package/src/common/canvas/painter.ts +417 -417
- package/src/common/geometry/prism-horizontal.ts +35 -35
- package/src/common/geometry/prism-vertical.ts +40 -40
- package/src/common/geometry/sphere-fragment.ts +40 -40
- package/src/common/geometry/tool/circle.ts +11 -11
- package/src/common/mergeOption.ts +6 -6
- package/src/common/setStyle.ts +5 -5
- package/src/common/svg/config.ts +186 -186
- package/src/common/svg/gradient.ts +63 -63
- package/src/common/svg/index.ts +356 -356
- package/src/common/svg/tool.ts +44 -44
- package/src/common/tree/toInnerTree.ts +57 -57
- package/src/common/tree/toPlainTree.ts +132 -132
- package/src/common/webgl/buffer.ts +79 -79
- package/src/common/webgl/doDraw.ts +108 -108
- package/src/common/webgl/getColorFactory.ts +26 -26
- package/src/common/webgl/getShader.ts +48 -48
- package/src/common/webgl/getTexture.ts +11 -11
- package/src/common/webgl/getWebGLContext.ts +28 -28
- package/src/common/webgl/index.ts +224 -224
- package/src/common/webgl/shader.ts +75 -75
- package/src/common/webgl/texture.ts +97 -97
- package/src/formatColor.ts +44 -44
- package/src/getLoopColors.ts +42 -42
- package/src/interpolation/Cardinal.ts +110 -110
- package/src/interpolation/Hermite.ts +65 -65
- package/src/move.ts +7 -7
- package/src/resizeObserver.ts +37 -37
- package/src/rotate.ts +7 -7
- package/src/throttle.ts +52 -52
- package/src/viewHandler.ts +158 -158
- package/types/CanvasOption.d.ts +8 -8
|
@@ -1,66 +1,66 @@
|
|
|
1
|
-
import type HermiteType from '../../types/Hermite'
|
|
2
|
-
|
|
3
|
-
class Hermite implements HermiteType {
|
|
4
|
-
readonly name: string = 'Hermite'
|
|
5
|
-
|
|
6
|
-
private __u: number // 张弛系数
|
|
7
|
-
|
|
8
|
-
private __MR: number[]
|
|
9
|
-
private __a: number
|
|
10
|
-
private __b: number
|
|
11
|
-
|
|
12
|
-
constructor(u: number = 0.5) {
|
|
13
|
-
this.__u = u
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* 设置点的位置
|
|
18
|
-
* @param {Number} x1 左边点的位置
|
|
19
|
-
* @param {Number} y1
|
|
20
|
-
* @param {Number} x2 右边点的位置
|
|
21
|
-
* @param {Number} y2
|
|
22
|
-
* @param {Number} s1 两个点的斜率
|
|
23
|
-
* @param {Number} s2
|
|
24
|
-
*/
|
|
25
|
-
setP(x1: number, y1: number, x2: number, y2: number, s1: number, s2: number) {
|
|
26
|
-
if (x1 < x2) {
|
|
27
|
-
// 记录原始尺寸
|
|
28
|
-
this.__a = x1
|
|
29
|
-
this.__b = x2
|
|
30
|
-
const p3 = this.__u * s1,
|
|
31
|
-
p4 = this.__u * s2
|
|
32
|
-
// 缩放到[0,1]定义域
|
|
33
|
-
y1 /= (x2 - x1)
|
|
34
|
-
y2 /= (x2 - x1)
|
|
35
|
-
// MR是提前计算好的多项式通解矩阵
|
|
36
|
-
// 为了加速计算
|
|
37
|
-
// 如上面说的
|
|
38
|
-
// 统一在[0,1]上计算后再通过缩放和移动恢复
|
|
39
|
-
// 避免了动态求解矩阵的麻烦
|
|
40
|
-
this.__MR = [
|
|
41
|
-
2 * y1 - 2 * y2 + p3 + p4,
|
|
42
|
-
3 * y2 - 3 * y1 - 2 * p3 - p4,
|
|
43
|
-
p3,
|
|
44
|
-
y1
|
|
45
|
-
]
|
|
46
|
-
} else throw new Error('The point x-position should be increamented!')
|
|
47
|
-
return this
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 根据x值返回y值
|
|
53
|
-
* @param {Number} x
|
|
54
|
-
*/
|
|
55
|
-
use(x: number) {
|
|
56
|
-
if (this.__MR) {
|
|
57
|
-
const sx = (x - this.__a) / (this.__b - this.__a),
|
|
58
|
-
sx2 = sx * sx,
|
|
59
|
-
sx3 = sx * sx2
|
|
60
|
-
const sResult = sx3 * this.__MR[0] + sx2 * this.__MR[1] + sx * this.__MR[2] + this.__MR[3]
|
|
61
|
-
return sResult * (this.__b - this.__a)
|
|
62
|
-
} else throw new Error('You shoud first set the position!')
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
1
|
+
import type HermiteType from '../../types/Hermite'
|
|
2
|
+
|
|
3
|
+
class Hermite implements HermiteType {
|
|
4
|
+
readonly name: string = 'Hermite'
|
|
5
|
+
|
|
6
|
+
private __u: number // 张弛系数
|
|
7
|
+
|
|
8
|
+
private __MR: number[]
|
|
9
|
+
private __a: number
|
|
10
|
+
private __b: number
|
|
11
|
+
|
|
12
|
+
constructor(u: number = 0.5) {
|
|
13
|
+
this.__u = u
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 设置点的位置
|
|
18
|
+
* @param {Number} x1 左边点的位置
|
|
19
|
+
* @param {Number} y1
|
|
20
|
+
* @param {Number} x2 右边点的位置
|
|
21
|
+
* @param {Number} y2
|
|
22
|
+
* @param {Number} s1 两个点的斜率
|
|
23
|
+
* @param {Number} s2
|
|
24
|
+
*/
|
|
25
|
+
setP(x1: number, y1: number, x2: number, y2: number, s1: number, s2: number) {
|
|
26
|
+
if (x1 < x2) {
|
|
27
|
+
// 记录原始尺寸
|
|
28
|
+
this.__a = x1
|
|
29
|
+
this.__b = x2
|
|
30
|
+
const p3 = this.__u * s1,
|
|
31
|
+
p4 = this.__u * s2
|
|
32
|
+
// 缩放到[0,1]定义域
|
|
33
|
+
y1 /= (x2 - x1)
|
|
34
|
+
y2 /= (x2 - x1)
|
|
35
|
+
// MR是提前计算好的多项式通解矩阵
|
|
36
|
+
// 为了加速计算
|
|
37
|
+
// 如上面说的
|
|
38
|
+
// 统一在[0,1]上计算后再通过缩放和移动恢复
|
|
39
|
+
// 避免了动态求解矩阵的麻烦
|
|
40
|
+
this.__MR = [
|
|
41
|
+
2 * y1 - 2 * y2 + p3 + p4,
|
|
42
|
+
3 * y2 - 3 * y1 - 2 * p3 - p4,
|
|
43
|
+
p3,
|
|
44
|
+
y1
|
|
45
|
+
]
|
|
46
|
+
} else throw new Error('The point x-position should be increamented!')
|
|
47
|
+
return this
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 根据x值返回y值
|
|
53
|
+
* @param {Number} x
|
|
54
|
+
*/
|
|
55
|
+
use(x: number) {
|
|
56
|
+
if (this.__MR) {
|
|
57
|
+
const sx = (x - this.__a) / (this.__b - this.__a),
|
|
58
|
+
sx2 = sx * sx,
|
|
59
|
+
sx3 = sx * sx2
|
|
60
|
+
const sResult = sx3 * this.__MR[0] + sx2 * this.__MR[1] + sx * this.__MR[2] + this.__MR[3]
|
|
61
|
+
return sResult * (this.__b - this.__a)
|
|
62
|
+
} else throw new Error('You shoud first set the position!')
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
66
|
export default Hermite
|
package/src/move.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
// 点(x,y)沿着向量(ax,ay)方向移动距离d
|
|
2
|
-
export default function (ax: number, ay: number, d: number, x: number, y: number): [number, number] {
|
|
3
|
-
const sqrt = Math.sqrt(ax * ax + ay * ay)
|
|
4
|
-
return [
|
|
5
|
-
ax * d / sqrt + x,
|
|
6
|
-
ay * d / sqrt + y
|
|
7
|
-
]
|
|
1
|
+
// 点(x,y)沿着向量(ax,ay)方向移动距离d
|
|
2
|
+
export default function (ax: number, ay: number, d: number, x: number, y: number): [number, number] {
|
|
3
|
+
const sqrt = Math.sqrt(ax * ax + ay * ay)
|
|
4
|
+
return [
|
|
5
|
+
ax * d / sqrt + x,
|
|
6
|
+
ay * d / sqrt + y
|
|
7
|
+
]
|
|
8
8
|
}
|
package/src/resizeObserver.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
let observer: ResizeObserver
|
|
2
|
-
|
|
3
|
-
const attrValueToCallback = {}
|
|
4
|
-
let uniqueid = 0
|
|
5
|
-
|
|
6
|
-
export default function (el: HTMLElement | null, callback: Function) {
|
|
7
|
-
|
|
8
|
-
try {
|
|
9
|
-
|
|
10
|
-
if (!observer) {
|
|
11
|
-
observer = new ResizeObserver(entries => {
|
|
12
|
-
for (const entry of entries) {
|
|
13
|
-
attrValueToCallback[(entry.target as any)._resize_observer_]()
|
|
14
|
-
}
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
uniqueid++
|
|
19
|
-
|
|
20
|
-
(el as any)._resize_observer_ = uniqueid
|
|
21
|
-
attrValueToCallback[uniqueid] = callback
|
|
22
|
-
|
|
23
|
-
observer.observe(el)
|
|
24
|
-
|
|
25
|
-
return function () {
|
|
26
|
-
if (observer) {
|
|
27
|
-
observer.unobserve(el)
|
|
28
|
-
delete attrValueToCallback[(el as any)._resize_observer_]
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
} catch (e) {
|
|
33
|
-
callback()
|
|
34
|
-
|
|
35
|
-
// 如果浏览器不支持此接口
|
|
36
|
-
console.error('ResizeObserver undefined!')
|
|
37
|
-
}
|
|
1
|
+
let observer: ResizeObserver
|
|
2
|
+
|
|
3
|
+
const attrValueToCallback = {}
|
|
4
|
+
let uniqueid = 0
|
|
5
|
+
|
|
6
|
+
export default function (el: HTMLElement | null, callback: Function) {
|
|
7
|
+
|
|
8
|
+
try {
|
|
9
|
+
|
|
10
|
+
if (!observer) {
|
|
11
|
+
observer = new ResizeObserver(entries => {
|
|
12
|
+
for (const entry of entries) {
|
|
13
|
+
attrValueToCallback[(entry.target as any)._resize_observer_]()
|
|
14
|
+
}
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
uniqueid++
|
|
19
|
+
|
|
20
|
+
(el as any)._resize_observer_ = uniqueid
|
|
21
|
+
attrValueToCallback[uniqueid] = callback
|
|
22
|
+
|
|
23
|
+
observer.observe(el)
|
|
24
|
+
|
|
25
|
+
return function () {
|
|
26
|
+
if (observer) {
|
|
27
|
+
observer.unobserve(el)
|
|
28
|
+
delete attrValueToCallback[(el as any)._resize_observer_]
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
} catch (e) {
|
|
33
|
+
callback()
|
|
34
|
+
|
|
35
|
+
// 如果浏览器不支持此接口
|
|
36
|
+
console.error('ResizeObserver undefined!')
|
|
37
|
+
}
|
|
38
38
|
}
|
package/src/rotate.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
// 点(x,y)围绕中心(cx,cy)旋转deg度
|
|
2
|
-
export default function (cx: number, cy: number, deg: number, x: number, y: number): [number, number] {
|
|
3
|
-
const cos = Math.cos(deg), sin = Math.sin(deg)
|
|
4
|
-
return [
|
|
5
|
-
(x - cx) * cos - (y - cy) * sin + cx,
|
|
6
|
-
(x - cx) * sin + (y - cy) * cos + cy
|
|
7
|
-
]
|
|
1
|
+
// 点(x,y)围绕中心(cx,cy)旋转deg度
|
|
2
|
+
export default function (cx: number, cy: number, deg: number, x: number, y: number): [number, number] {
|
|
3
|
+
const cos = Math.cos(deg), sin = Math.sin(deg)
|
|
4
|
+
return [
|
|
5
|
+
(x - cx) * cos - (y - cy) * sin + cx,
|
|
6
|
+
(x - cx) * sin + (y - cy) * cos + cy
|
|
7
|
+
]
|
|
8
8
|
}
|
package/src/throttle.ts
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
import type throttleOptionType from '../types/throttleOption'
|
|
2
|
-
|
|
3
|
-
import mergeOption from './common/mergeOption'
|
|
4
|
-
|
|
5
|
-
export default function (callback: Function, option: throttleOptionType = {}) {
|
|
6
|
-
|
|
7
|
-
// 校对缺省值
|
|
8
|
-
option = mergeOption(option, <throttleOptionType>{
|
|
9
|
-
time: 200,
|
|
10
|
-
keep: false,
|
|
11
|
-
opportunity: "end"
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
let hadInterval = false
|
|
15
|
-
let hadClick = false
|
|
16
|
-
let oneClick = false
|
|
17
|
-
|
|
18
|
-
let arg: any
|
|
19
|
-
return function () {
|
|
20
|
-
const _this = this
|
|
21
|
-
// eslint-disable-next-line prefer-rest-params
|
|
22
|
-
arg = arguments
|
|
23
|
-
|
|
24
|
-
// 如果前置任务都完成了
|
|
25
|
-
if (!hadInterval) {
|
|
26
|
-
if (option.opportunity != 'end') {
|
|
27
|
-
callback.apply(_this, arg)
|
|
28
|
-
}
|
|
29
|
-
hadInterval = true
|
|
30
|
-
|
|
31
|
-
const interval = setInterval(() => {
|
|
32
|
-
if (hadClick) {
|
|
33
|
-
if (!option.keep) {
|
|
34
|
-
callback.apply(_this, arg)
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
if (option.opportunity != 'begin') {
|
|
38
|
-
if (oneClick || option.opportunity == 'end') callback.apply(_this, arg)
|
|
39
|
-
}
|
|
40
|
-
hadInterval = false
|
|
41
|
-
oneClick = false
|
|
42
|
-
clearInterval(interval)
|
|
43
|
-
}
|
|
44
|
-
hadClick = false
|
|
45
|
-
}, option.time)
|
|
46
|
-
} else {
|
|
47
|
-
hadClick = true
|
|
48
|
-
oneClick = true
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
1
|
+
import type throttleOptionType from '../types/throttleOption'
|
|
2
|
+
|
|
3
|
+
import mergeOption from './common/mergeOption'
|
|
4
|
+
|
|
5
|
+
export default function (callback: Function, option: throttleOptionType = {}) {
|
|
6
|
+
|
|
7
|
+
// 校对缺省值
|
|
8
|
+
option = mergeOption(option, <throttleOptionType>{
|
|
9
|
+
time: 200,
|
|
10
|
+
keep: false,
|
|
11
|
+
opportunity: "end"
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
let hadInterval = false
|
|
15
|
+
let hadClick = false
|
|
16
|
+
let oneClick = false
|
|
17
|
+
|
|
18
|
+
let arg: any
|
|
19
|
+
return function () {
|
|
20
|
+
const _this = this
|
|
21
|
+
// eslint-disable-next-line prefer-rest-params
|
|
22
|
+
arg = arguments
|
|
23
|
+
|
|
24
|
+
// 如果前置任务都完成了
|
|
25
|
+
if (!hadInterval) {
|
|
26
|
+
if (option.opportunity != 'end') {
|
|
27
|
+
callback.apply(_this, arg)
|
|
28
|
+
}
|
|
29
|
+
hadInterval = true
|
|
30
|
+
|
|
31
|
+
const interval = setInterval(() => {
|
|
32
|
+
if (hadClick) {
|
|
33
|
+
if (!option.keep) {
|
|
34
|
+
callback.apply(_this, arg)
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
if (option.opportunity != 'begin') {
|
|
38
|
+
if (oneClick || option.opportunity == 'end') callback.apply(_this, arg)
|
|
39
|
+
}
|
|
40
|
+
hadInterval = false
|
|
41
|
+
oneClick = false
|
|
42
|
+
clearInterval(interval)
|
|
43
|
+
}
|
|
44
|
+
hadClick = false
|
|
45
|
+
}, option.time)
|
|
46
|
+
} else {
|
|
47
|
+
hadClick = true
|
|
48
|
+
oneClick = true
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
}
|
|
52
|
+
|
|
53
53
|
}
|
package/src/viewHandler.ts
CHANGED
|
@@ -1,159 +1,159 @@
|
|
|
1
|
-
import type { callbackFun } from '../types/viewHandler'
|
|
2
|
-
|
|
3
|
-
const mousePosition = (el: HTMLElement, event: any) => {
|
|
4
|
-
const bounding = el.getBoundingClientRect()
|
|
5
|
-
return {
|
|
6
|
-
"x": event.clientX - bounding.left,
|
|
7
|
-
"y": event.clientY - bounding.top
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const getKeyString = (event: any) => {
|
|
12
|
-
return {
|
|
13
|
-
37: "left",
|
|
14
|
-
38: "up",
|
|
15
|
-
39: "right",
|
|
16
|
-
40: "down",
|
|
17
|
-
}[event.keyCode || event.which]
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export default function (callback: callbackFun) {
|
|
21
|
-
|
|
22
|
-
const el = document.getElementsByTagName('body')[0]
|
|
23
|
-
|
|
24
|
-
// 键盘控制
|
|
25
|
-
el.addEventListener("keydown", function (event) {
|
|
26
|
-
const keyCode = getKeyString(event)
|
|
27
|
-
|
|
28
|
-
// 视角向上
|
|
29
|
-
if (keyCode == 'up') {
|
|
30
|
-
callback({
|
|
31
|
-
type: "lookUp",
|
|
32
|
-
normal: [],
|
|
33
|
-
value: 0.1,
|
|
34
|
-
event
|
|
35
|
-
})
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// 视角向下
|
|
39
|
-
else if (keyCode == 'down') {
|
|
40
|
-
callback({
|
|
41
|
-
type: "lookDown",
|
|
42
|
-
normal: [],
|
|
43
|
-
value: 0.1,
|
|
44
|
-
event
|
|
45
|
-
})
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// 视角向左
|
|
49
|
-
else if (keyCode == 'left') {
|
|
50
|
-
callback({
|
|
51
|
-
type: "lookLeft",
|
|
52
|
-
normal: [],
|
|
53
|
-
value: 0.1,
|
|
54
|
-
event
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
// 视角向右
|
|
59
|
-
else if (keyCode == 'right') {
|
|
60
|
-
callback({
|
|
61
|
-
type: "lookRight",
|
|
62
|
-
normal: [],
|
|
63
|
-
value: 0.1,
|
|
64
|
-
event
|
|
65
|
-
})
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
})
|
|
69
|
-
|
|
70
|
-
// 鼠标控制
|
|
71
|
-
let mouseP = null;
|
|
72
|
-
const doMove = function (event) {
|
|
73
|
-
|
|
74
|
-
// 单纯移动
|
|
75
|
-
if (mouseP == null) {
|
|
76
|
-
callback({
|
|
77
|
-
type: "hover",
|
|
78
|
-
normal: [],
|
|
79
|
-
value: 0,
|
|
80
|
-
event
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// 按下移动
|
|
85
|
-
else {
|
|
86
|
-
|
|
87
|
-
const tempMouseP = mousePosition(el, event)
|
|
88
|
-
|
|
89
|
-
// 先求解出轨迹向量
|
|
90
|
-
const normal = [tempMouseP.x - mouseP.x, tempMouseP.y - mouseP.y]
|
|
91
|
-
|
|
92
|
-
// 方向向量旋转90deg得到旋转向量
|
|
93
|
-
const rotateNormal = [
|
|
94
|
-
normal[1],
|
|
95
|
-
normal[0],
|
|
96
|
-
0
|
|
97
|
-
]
|
|
98
|
-
|
|
99
|
-
// 非法射线忽略
|
|
100
|
-
if (rotateNormal[0] == 0 && rotateNormal[1] == 0) return
|
|
101
|
-
|
|
102
|
-
callback({
|
|
103
|
-
type: "rotate",
|
|
104
|
-
normal: rotateNormal,
|
|
105
|
-
value: (Math.abs(tempMouseP.x - mouseP.x) + Math.abs(tempMouseP.y - mouseP.y)) * 0.01,
|
|
106
|
-
event
|
|
107
|
-
})
|
|
108
|
-
|
|
109
|
-
mouseP = tempMouseP
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
el.addEventListener("mousedown", (event) => {
|
|
114
|
-
mouseP = mousePosition(el, event)
|
|
115
|
-
})
|
|
116
|
-
el.addEventListener("mouseup", () => {
|
|
117
|
-
mouseP = null
|
|
118
|
-
})
|
|
119
|
-
el.addEventListener("mousemove", (event) => {
|
|
120
|
-
doMove(event)
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
// 手指控制
|
|
124
|
-
el.addEventListener("touchend", () => {
|
|
125
|
-
mouseP = null;
|
|
126
|
-
})
|
|
127
|
-
el.addEventListener("touchstart", (event) => {
|
|
128
|
-
mouseP = mousePosition(el, event)
|
|
129
|
-
})
|
|
130
|
-
el.addEventListener("touchmove", (event) => {
|
|
131
|
-
doMove(event.touches[0])
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
const doScale = function (value: number, event: Event) {
|
|
135
|
-
if (value == 0) return
|
|
136
|
-
|
|
137
|
-
const baseTimes = 0.899
|
|
138
|
-
callback({
|
|
139
|
-
type: "scale",
|
|
140
|
-
value: value < 0 ? baseTimes : 2 - baseTimes,
|
|
141
|
-
normal: [],
|
|
142
|
-
event
|
|
143
|
-
})
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// 滚轮控制
|
|
147
|
-
el.addEventListener("mousewheel", function (event: any) {
|
|
148
|
-
doScale(event.wheelDelta, event)
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
if (window.addEventListener) {
|
|
152
|
-
|
|
153
|
-
// 针对火狐浏览器
|
|
154
|
-
window.addEventListener('DOMMouseScroll', function (event: any) {
|
|
155
|
-
doScale(-1 * event.detail, event)
|
|
156
|
-
}, false)
|
|
157
|
-
}
|
|
158
|
-
|
|
1
|
+
import type { callbackFun } from '../types/viewHandler'
|
|
2
|
+
|
|
3
|
+
const mousePosition = (el: HTMLElement, event: any) => {
|
|
4
|
+
const bounding = el.getBoundingClientRect()
|
|
5
|
+
return {
|
|
6
|
+
"x": event.clientX - bounding.left,
|
|
7
|
+
"y": event.clientY - bounding.top
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const getKeyString = (event: any) => {
|
|
12
|
+
return {
|
|
13
|
+
37: "left",
|
|
14
|
+
38: "up",
|
|
15
|
+
39: "right",
|
|
16
|
+
40: "down",
|
|
17
|
+
}[event.keyCode || event.which]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default function (callback: callbackFun) {
|
|
21
|
+
|
|
22
|
+
const el = document.getElementsByTagName('body')[0]
|
|
23
|
+
|
|
24
|
+
// 键盘控制
|
|
25
|
+
el.addEventListener("keydown", function (event) {
|
|
26
|
+
const keyCode = getKeyString(event)
|
|
27
|
+
|
|
28
|
+
// 视角向上
|
|
29
|
+
if (keyCode == 'up') {
|
|
30
|
+
callback({
|
|
31
|
+
type: "lookUp",
|
|
32
|
+
normal: [],
|
|
33
|
+
value: 0.1,
|
|
34
|
+
event
|
|
35
|
+
})
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 视角向下
|
|
39
|
+
else if (keyCode == 'down') {
|
|
40
|
+
callback({
|
|
41
|
+
type: "lookDown",
|
|
42
|
+
normal: [],
|
|
43
|
+
value: 0.1,
|
|
44
|
+
event
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 视角向左
|
|
49
|
+
else if (keyCode == 'left') {
|
|
50
|
+
callback({
|
|
51
|
+
type: "lookLeft",
|
|
52
|
+
normal: [],
|
|
53
|
+
value: 0.1,
|
|
54
|
+
event
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 视角向右
|
|
59
|
+
else if (keyCode == 'right') {
|
|
60
|
+
callback({
|
|
61
|
+
type: "lookRight",
|
|
62
|
+
normal: [],
|
|
63
|
+
value: 0.1,
|
|
64
|
+
event
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
// 鼠标控制
|
|
71
|
+
let mouseP = null;
|
|
72
|
+
const doMove = function (event) {
|
|
73
|
+
|
|
74
|
+
// 单纯移动
|
|
75
|
+
if (mouseP == null) {
|
|
76
|
+
callback({
|
|
77
|
+
type: "hover",
|
|
78
|
+
normal: [],
|
|
79
|
+
value: 0,
|
|
80
|
+
event
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 按下移动
|
|
85
|
+
else {
|
|
86
|
+
|
|
87
|
+
const tempMouseP = mousePosition(el, event)
|
|
88
|
+
|
|
89
|
+
// 先求解出轨迹向量
|
|
90
|
+
const normal = [tempMouseP.x - mouseP.x, tempMouseP.y - mouseP.y]
|
|
91
|
+
|
|
92
|
+
// 方向向量旋转90deg得到旋转向量
|
|
93
|
+
const rotateNormal = [
|
|
94
|
+
normal[1],
|
|
95
|
+
normal[0],
|
|
96
|
+
0
|
|
97
|
+
]
|
|
98
|
+
|
|
99
|
+
// 非法射线忽略
|
|
100
|
+
if (rotateNormal[0] == 0 && rotateNormal[1] == 0) return
|
|
101
|
+
|
|
102
|
+
callback({
|
|
103
|
+
type: "rotate",
|
|
104
|
+
normal: rotateNormal,
|
|
105
|
+
value: (Math.abs(tempMouseP.x - mouseP.x) + Math.abs(tempMouseP.y - mouseP.y)) * 0.01,
|
|
106
|
+
event
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
mouseP = tempMouseP
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
el.addEventListener("mousedown", (event) => {
|
|
114
|
+
mouseP = mousePosition(el, event)
|
|
115
|
+
})
|
|
116
|
+
el.addEventListener("mouseup", () => {
|
|
117
|
+
mouseP = null
|
|
118
|
+
})
|
|
119
|
+
el.addEventListener("mousemove", (event) => {
|
|
120
|
+
doMove(event)
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
// 手指控制
|
|
124
|
+
el.addEventListener("touchend", () => {
|
|
125
|
+
mouseP = null;
|
|
126
|
+
})
|
|
127
|
+
el.addEventListener("touchstart", (event) => {
|
|
128
|
+
mouseP = mousePosition(el, event)
|
|
129
|
+
})
|
|
130
|
+
el.addEventListener("touchmove", (event) => {
|
|
131
|
+
doMove(event.touches[0])
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
const doScale = function (value: number, event: Event) {
|
|
135
|
+
if (value == 0) return
|
|
136
|
+
|
|
137
|
+
const baseTimes = 0.899
|
|
138
|
+
callback({
|
|
139
|
+
type: "scale",
|
|
140
|
+
value: value < 0 ? baseTimes : 2 - baseTimes,
|
|
141
|
+
normal: [],
|
|
142
|
+
event
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 滚轮控制
|
|
147
|
+
el.addEventListener("mousewheel", function (event: any) {
|
|
148
|
+
doScale(event.wheelDelta, event)
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
if (window.addEventListener) {
|
|
152
|
+
|
|
153
|
+
// 针对火狐浏览器
|
|
154
|
+
window.addEventListener('DOMMouseScroll', function (event: any) {
|
|
155
|
+
doScale(-1 * event.detail, event)
|
|
156
|
+
}, false)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
159
|
}
|