forked from ZulNs/ZulNs.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuboid3d.js
163 lines (147 loc) · 4.53 KB
/
cuboid3d.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var lastTransform,
transformMatrix,
startPointX,
startPointY,
isDragging = false,
isBlocked = false,
isFiredByMouse = false,
touchId,
productToRadians = Math.PI / 560,
cuboid = document.getElementsByClassName('cuboid')[0];
init();
function init() {
var contents = document.querySelectorAll('.cuboid .content'),
option = document.location.search,
rotate;
for (var i = 0; i < contents.length; i++)
addEvent(contents[i], 'mousedown', handleBlockEvent);
addEvent(cuboid, 'mousedown', handleMouseDown);
addEvent(cuboid, 'mousemove', handleMouseMove);
addEvent(document, 'mouseup', handleMouseUp);
if ('touchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0) {
for (var i = 0; i < contents.length; i++)
addEvent(contents[i], 'touchstart', handleBlockEvent);
addEvent(cuboid, 'touchstart', handleTouchStart);
addEvent(cuboid, 'touchmove', handleTouchMove);
addEvent(cuboid, 'touchcancel', handleTouchEnd);
addEvent(document, 'touchend', handleTouchEnd);
}
option = (!!option) ? option.substring(1) : 0;
rotate = (option == 2) ? 245 : (option == 3) ? 205 : -45;
cuboid.style.cssText = addVendorPrefix('transform: rotateY(' + rotate + 'deg);');
}
function addEvent(elm, evt, callback) {
if (!!window.addEventListener)
elm.addEventListener(evt, callback);
else
elm.attachEvent('on' + evt, callback);
}
function addVendorPrefix(property) {
return '-webkit-' + property +
'-moz-' + property +
'-o-' + property +
property;
}
function toArray(str) {
var res = [], arr = str.substring(9, str.length -1).split(',');
for (var i in arr) res.push(parseFloat(arr[i]));
return res;
}
function handleMouseDown(evt) {
if (!isBlocked && !isDragging) {
var e = evt || window.event;
e.preventDefault();
e.stopPropagation();
isFiredByMouse = true;
startDragging(e.pageX, e.pageY);
}
}
function handleMouseMove(evt) {
if (isDragging && isFiredByMouse) {
var e = evt || window.event;
e.preventDefault();
whileDragging(e.pageX, e.pageY);
}
}
function handleMouseUp(evt) {
if (isDragging && isFiredByMouse) {
var e = evt || window.event;
e.preventDefault();
endDragging();
}
isBlocked = false;
}
function handleTouchStart(evt) {
var e = evt || window.event;
if (isDragging && !isBlocked && !isFiredByMouse && e.touches.length == 1) endDragging();
if (!isBlocked && !isDragging) {
var touch = e.changedTouches[0];
e.preventDefault();
//e.stopPropagation();
isFiredByMouse = false;
touchId = touch.identifier;
startDragging(touch.pageX, touch.pageY);
}
}
function handleTouchMove(evt) {
if (isDragging && !isFiredByMouse) {
var e = evt || window.event;
var touches = e.changedTouches;
var touch;
for (var i = 0; i < touches.length; i++) {
touch = touches[i];
if (touch.identifier === touchId) {
e.preventDefault();
whileDragging(touch.pageX, touch.pageY);
break;
}
}
}
}
function handleTouchEnd(evt) {
if (isDragging && !isFiredByMouse) {
var e = evt || window.event;
var touches = e.changedTouches;
var touch;
for (var i = 0; i < touches.length; i++) {
touch = touches[i];
if (touch.identifier === touchId) {
e.preventDefault();
endDragging();
break;
}
}
}
isBlocked = false;
}
function handleBlockEvent(evt) {
if (!isDragging) isBlocked = true;
}
function startDragging(spx, spy) {
startPointX = spx;
startPointY = spy;
isDragging = true;
lastTransform = window.getComputedStyle(cuboid).getPropertyValue('transform');
if (lastTransform === 'none')
lastTransform = 'matrix3d(1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1)';
transformMatrix = toArray(lastTransform);
cuboid.style.cssText = addVendorPrefix('transform: ' + lastTransform + ';');
}
function whileDragging(cpx, cpy) {
var sx, sy, x = 0, y = 0, z = 0, cr = 0.5, rad, css;
if (startPointX != cpx || startPointY != cpy) {
sx = (startPointY - cpy);
sy = (cpx - startPointX);
rad = Math.sqrt(sx * sx + sy * sy) * productToRadians;
x = sx * transformMatrix[0] + sy * transformMatrix[1];
y = sx * transformMatrix[4] + sy * transformMatrix[5];
z = sx * transformMatrix[8] + sy * transformMatrix[9];
css = 'transform: ' + lastTransform + ' rotate3d(' + x + ', ' + y + ', ' + z + ', ' + rad + 'rad);';
cuboid.style.cssText = addVendorPrefix(css);
}
}
function endDragging() {
isDragging = false;
lastTransform = window.getComputedStyle(cuboid).getPropertyValue('transform');
transformMatrix = toArray(lastTransform);
}