-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
83 lines (70 loc) · 2.1 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// TODO(sww): zero allocations! (out param?)
function single (box, motion, box2) {
var newX = moveAxis(box, motion, box2, 0)
var newY = moveAxis(box, motion, box2, 1)
return [newX, newY]
}
// TODO(sww): zero allocations! (out param?)
function set (box, motion, getBox, cb) {
var box2 = undefined
var offset = [motion[0], motion[1]]
var winnerX = undefined
var winnerY = undefined
function heresABox (box2) {
if (box2 !== null) {
var leading = motion[0] > 0 ? box[0] + box[2] : box[0]
var newX = moveAxis(box, motion, box2, 0)
// decide winner
if (motion[0] > 0 && newX < offset[0]) {
offset[0] = newX
winnerX = box2
}
else if (motion[0] < 0 && newX > offset[0]) {
offset[0] = newX
winnerX = box2
}
var leading = motion[1] > 0 ? box[1] + box[3] : box[1]
var newY = moveAxis(box, motion, box2, 1)
// decide winner
if (motion[1] > 0 && newY < offset[1]) {
offset[1] = newY
winnerY = box2
}
else if (motion[1] < 0 && newY > offset[1]) {
offset[1] = newY
winnerY = box2
}
}
}
getBox(heresABox)
// winner callbacks for each axis
if (winnerX) {
cb(winnerX, 0, motion[0] > 0 ? 1 : -1)
}
if (winnerY) {
cb(winnerY, 1, motion[1] > 0 ? 1 : -1)
}
return offset
}
function moveAxis (box, motion, box2, axis) {
var leading = motion[axis] > 0 ? box[axis] + box[axis + 2] : box[axis]
var otherAxis = (axis === 0 ? 1 : 0)
// Bail if there's no other-axis intersection
if (box[otherAxis] > box2[otherAxis] + box2[otherAxis + 2]) {
return motion[axis]
}
if (box[otherAxis] + box[otherAxis + 2] < box2[otherAxis]) {
return motion[axis]
}
// Axis hit (right/bottom)
if (leading <= box2[axis] && leading + motion[axis] > box2[axis]) {
return box2[axis] - leading
}
// Axis hit (left/top)
if (leading >= box2[axis] + box2[axis + 2] && leading + motion[axis] < box2[axis] + box2[axis + 2]) {
return (box2[axis] + box2[axis + 2]) - leading
}
return motion[axis]
}
module.exports.single = single
module.exports.set = set