-
Notifications
You must be signed in to change notification settings - Fork 11
/
demo1.html
189 lines (173 loc) · 5.75 KB
/
demo1.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
<!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: 100px auto;
width: 250px;
height: 300px;
-webkit-perspective: 4000px;
-moz-perspective: 4000px;
perspective: 4000px;
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;
font-size: 150px;
line-height: 300px;
text-align: center;
background: rgba(255,255,255,0.03);
border: 1px solid 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 {
background: rgba(60, 60, 60,0.9);
}
</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 = 5,
LAYER_SPACING = 75;
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,
// 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.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">'+(i+1)+'</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 matrix = rotationMatrix.translate(0, 0, $layer.index() * LAYER_SPACING);
$layer.css('transform', FORCE_AFFINE ? toAffineString(matrix) : matrix.toString());
};
createLayers();
updateLayers();
};
var page = new Page($('.page'));
page.rotate(10, 25);
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);
ev.preventDefault();
return false;
};
$(document.body).on('mousemove touchmove MSPointerMove', mousemove)
.on('mouseup touchend MSPointerUp mouseleave', mouseup);
ev.preventDefault();
return false;
};
$(document.body).on('mousedown touchstart MSPointerDown', mousedown);
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>