forked from scouttyg/3d-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo5.html
233 lines (211 loc) · 7 KB
/
demo5.html
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Demo</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://raw.github.com/camupod/CSSMatrix/browserified/CSSMatrix.js"></script>
<style>
@import url(http://fonts.googleapis.com/css?family=Open+Sans:600);
html, body {
width: 100%;
height: 100%;
margin: 0; padding: 0;
overflow: hidden;
background-color: #282828;
font-family: 'Open Sans', sans-serif;
text-align: center;
}
.page {
margin: -150px auto;
width: 300px;
height: 300px;
top: 50%;
-webkit-perspective: 1000px;
-moz-perspective: 1000px;
perspective: 1000px;
position: relative;
-webkit-transform-style: preserve-3d;
-moz-transform-style: preserve-3d;
transform-style: preserve-3d;
}
.page-layer {
width: 100%;
height: 100%;
position:absolute; top:0; left:0;
color: transparent;
font-size: 50px;
line-height: 300px;
text-align: center;
border: 1px solid transparent;
}
.page-transitions .page-layer {
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
}
.page-layer:first-child, .page-layer:last-child, .page-exploded .page-layer {
background: rgba(255,255,255,0.03);
border-color: rgba(180,180,180,0.2);
color: rgba(255,255,255,0.25);
text-shadow: 1px 1px 5px rgba(0,0,0,0.25);
}
.page-layer:first-child, .page-layer:last-child {
background: rgba(60, 60, 60, 0.7);
}
</style>
</head>
<body>
<div class="page"></div>
<script type="text/javascript">
var Matrix = window.WebKitCSSMatrix || window.MSCSSMatrix || CSSMatrix;
var FORCE_AFFINE = /MSIE\s+9/i.test(navigator.userAgent),
NUM_LAYERS = 100,
LAYER_SPACING = 3;
function toAffineString(m) {
var fix6 = function (val) { return val.toFixed(6); };
return 'matrix(' + [
m.a, m.b,
m.c, m.d,
m.e, m.f
].map(fix6).join(', ') + ')';
}
function Page($el) {
var self = this,
$layers,
exploded = false,
// current state of rotation, relative to original position (0,0,0)
rotation = { x: 0, y: 0, z: 0 },
// identity matrix, used to update rotationMatrix with a new rotation state
identityMatrix = new Matrix(),
// current rotation matrix to be applied to the layers
rotationMatrix = new Matrix();
var createLayers,
updateLayers,
applyTransform;
self.enableTransitions = function () {
$el.addClass('page-transitions');
};
self.disableTransitions = function () {
$el.removeClass('page-transitions');
};
self.toggleExploded = function () {
exploded = !exploded;
if (exploded) $el.addClass('page-exploded');
else $el.removeClass('page-exploded');
updateLayers();
};
self.rotate = function (dx, dy, dz) {
rotation.x += dx || 0;
rotation.y += dy || 0;
rotation.z += dz || 0;
rotation.x = rotation.x % 360;
rotation.y = rotation.y % 360;
rotation.z = rotation.z % 360;
rotationMatrix = identityMatrix.rotate(rotation.x, rotation.y, rotation.z);
updateLayers();
};
createLayers = function () {
var i, layers = '';
for (i = 0; i < NUM_LAYERS; ++i)
layers += '<div class="page-layer">crocodoc</div>';
$layers = $(layers);
$el.html($layers);
};
updateLayers = function () {
var flipped = (Math.abs(rotation.x) > 90 && Math.abs(rotation.x) < 270) ||
(Math.abs(rotation.y) > 90 && Math.abs(rotation.y) < 270);
$.each($layers, function (i) {
var $layer = $(this);
if (FORCE_AFFINE) {
if (flipped) $layer.css('z-index', $layers.length - i);
else $layer.css('z-index', i);
}
applyTransform($layer);
});
};
applyTransform = function ($layer) {
var z = exploded ? $layer.index() * LAYER_SPACING : 0;
var matrix = rotationMatrix.translate(0, 0, z);
$layer.css('transform', FORCE_AFFINE ? toAffineString(matrix) : matrix.toString());
};
createLayers();
updateLayers();
}
var page = new Page($('.page'));
page.enableTransitions();
$(document).one('mousemove', function () {
page.rotate(-10, 30);
setTimeout(function () {
page.toggleExploded();
}, 500);
});
var mousedown = function (ev) {
var last = getCoord(ev);
var mousemove = function (ev) {
var mouse = getCoord(ev);
dx = mouse.x - last.x,
dy = mouse.y - last.y;
last = mouse;
// note: x and y mean different things in mouse coords, vs rotation axes
page.rotate(-dy / 2, dx / 2);
ev.preventDefault();
return false;
};
var mouseup = function (ev) {
$(document.body).off('mousemove touchmove MSPointerMove', mousemove)
.off('mouseup touchend MSPointerUp mouseleave', mouseup);
page.enableTransitions();
ev.preventDefault();
return false;
};
page.disableTransitions();
$(document.body).on('mousemove touchmove MSPointerMove', mousemove)
.on('mouseup touchend MSPointerUp mouseleave', mouseup);
ev.preventDefault();
return false;
};
$(document.body).on('mousedown touchstart MSPointerDown', mousedown);
$('.transition-btn').on('click touchstart', function (ev) {
transition();
return false;
});
$('.explode-btn').on('click touchstart', function (ev) {
page.toggleExploded();
return false;
});
function getCoord(ev, $el) {
ev = ev.originalEvent || ev;
var offset = $el ? $el.offset() : { left: 0, top: 0 },
x = ev.touches && ev.touches[0].pageX ||
ev.pageX ||
ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
y = ev.touches && ev.touches[0].pageY ||
ev.pageY ||
ev.clientY + document.body.scrollTop + document.documentElement.scrollTop;
return {
x: x - offset.left,
y: y - offset.top
};
}
</script>
<style>
.open {
background: url(open_in_new_window.png) 0 0 no-repeat;
width: 32px;
height: 32px;
position: absolute;
display: block;
top: 4px;
right: 4px;
opacity: 0.2;
z-index: 99999;
}
.open:hover {
opacity: 1;
}
</style>
<a class="open" href="" target="_blank" style="display: none" title="Open demo in a new tab"></a>
<script type="text/javascript"> if (window.self !== window.top) { $('.open').show(); }</script>
</body>
</html>