-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathdemo4.html
291 lines (267 loc) · 9.28 KB
/
demo4.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<!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>
<script src="jquery.xdr.js"></script>
<script src="innersvg.js"></script>
<script src="importNode.js"></script>
<script src="splitSVG.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: 70px auto;
width: 0; height: 0;
-webkit-perspective: 3000px;
-moz-perspective: 3000px;
perspective: 3000px;
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;
text-align: center;
border: 1px solid transparent;
}
.crocodoc-page-svg {
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.page-transitions .page-layer {
-webkit-transition: all 1s;
-moz-transition: all 1s;
transition: all 1s;
}
.page-layer:first-child, .page-exploded .page-layer {
background: rgba(255,255,255,0.02);
border-color: rgba(180,180,180,0.1);
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 assetsLocation = 'http://s3.amazonaws.com/crocodoc-preview/4bd37a25-8cf4-4935-b69e-4c4c2274e551/',
svgSrc = 'page-1.svg?v='+Math.random(); // hack for chrome/s3 bug
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, AFFINE) {
var self = this,
$layers,
affine = AFFINE || FORCE_AFFINE,
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"></div>';
$layers = $(layers);
$el.html($layers);
};
updateLayers = function () {
// hack for chrome on mac...
if (affine) $el.css('transform', 'translateZ('+(Math.random()*0.001)+'px)')
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 (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', affine ? toAffineString(matrix) : matrix.toString());
};
createLayers();
updateLayers();
}
var page = new Page($('.page'));
page.enableTransitions();
insertStylesheet(assetsLocation + 'css/stylesheet.css');
loadSVG(svgSrc, assetsLocation, function (err, $svg) {
$('.page').css({
width: $svg.attr('width'),
height: $svg.attr('height')
});
$('.page').css({
width: $('.page').width() * 0.7,
height: $('.page').height() * 0.7
});
var $splits = splitSVG($svg, NUM_LAYERS);
$splits.each(function (i) {
$('.page-layer').eq(i).html(this);
});
});
$(document.body).one('mousemove', function () {
page.rotate(-10, 30);
setTimeout(function () {
page.toggleExploded();
}, 1000);
});
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);
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
};
}
function loadSVG(src, assetsLocation, callback) {
$.ajax({
url: assetsLocation + src,
contentType: 'image/svg+xml',
success: function (response, s, xhr) {
var newNode,
sourceNode = response.documentElement ||
// Hack for FF
new DOMParser().parseFromString(response,'text/xml').documentElement;
// replace the relative paths to images embedded in the svg
// with absolute paths so they work properly when the svg is embedded inline
$(sourceNode).find('image').each(function (i, im){
$(im).attr('xlink:href', assetsLocation + $(im).attr('xlink:href'));
});
// importNode shim for IE 9 https://gist.github.com/camupod/5165619
newNode = importNode(sourceNode, true);
callback(null, $(newNode));
},
error: function () {
callback('Error loading SVG ('+ assetsLocation + src +')');
},
complete: function (xhr, status) {
if (status != 'success') {
callback(status);
}
}
});
}
function insertStylesheet(src) {
if (document.createStyleSheet) { // IE
document.createStyleSheet(src);
return;
}
var ss = document.createElement("link");
ss.type = "text/css";
ss.rel = "stylesheet";
ss.href = src;
document.getElementsByTagName("head")[0].appendChild(ss);
}
</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>