-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdimage.js
111 lines (96 loc) · 4.05 KB
/
dimage.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
function dimage() {
insertCSSAnimations();
function insertCSSAnimations() {
var style = document.createElement('style');
style.type = 'text/css';
var keyFrames = '\
@keyframes dimage-spin-right {\
from {\
transform: rotateY(0);\
}\
to {\
transform: rotateY(360deg);\
}\
}\
@keyframes dimage-spin-left {\
from {\
transform: rotateY(360deg);\
}\
to {\
transform: rotateY(0);\
}\
}';
style.innerHTML = keyFrames;
document.getElementsByTagName('head')[0].appendChild(style);
}
function createSide(image, size, transform, transformOrigin = null) {
let side = document.createElement('div');
side.style.background = "url('" + image + "') no-repeat";
side.style.backgroundSize = "100% 100%";
side.style.position = "absolute";
side.style.width = size + "px";
side.style.height = size + "px";
side.style.transform = transform;
if (transformOrigin !== null) {
side.style.transformOrigin = transformOrigin;
}
return side;
}
function readParams(dimage) {
var size = parseFloat(dimage.dataset.size);
var spinDirection = (dimage.dataset.direction !== undefined && dimage.dataset.direction == 'left') ? 'left' : 'right';
var speed = dimage.dataset.speed;
var imageFront = dimage.dataset.image;
var imageRight = dimage.dataset.image;
var imageBack = dimage.dataset.image;
var imageLeft = dimage.dataset.image;
if (dimage.dataset.imageFront !== undefined) {
imageFront = dimage.dataset.imageFront;
} if (dimage.dataset.imageRight !== undefined) {
imageRight = dimage.dataset.imageRight;
} if (dimage.dataset.imageBack !== undefined) {
imageBack = dimage.dataset.imageBack;
} if (dimage.dataset.imageLeft !== undefined) {
imageLeft = dimage.dataset.imageLeft;
}
return { size, speed, spinDirection, imageFront, imageRight, imageBack, imageLeft };
}
function insertDimage(dimage, sides, params) {
let perspectiveWrapper = document.createElement('div');
perspectiveWrapper.style.paddingLeft = params.size / 4 + "px";
perspectiveWrapper.style.paddingTop = params.size / 10 + "px";
perspectiveWrapper.style.perspective = params.size * 4 + "px";
perspectiveWrapper.style.perspectiveOrigin = "50% " + (params.size / 2) + "px";
let cube = document.createElement('div');
cube.style.animation = "dimage-spin-" + params.spinDirection + " " + params.speed + "s infinite linear";
cube.style.position = "relative";
cube.style.width = params.size + "px";
cube.style.transformStyle = "preserve-3d";
sides.forEach(side => cube.appendChild(side));
perspectiveWrapper.appendChild(cube);
//resize dimage to fix the wider and taller third dimension
dimage.style.width = params.size * 1.5 + "px";
dimage.style.height = params.size * 1.3 + "px";
dimage.appendChild(perspectiveWrapper);
}
function initDimage(dimage) {
var params = readParams(dimage);
let sides = [
createSide(params.imageFront, params.size, "translateZ(" + (params.size / 2) + "px)"),
createSide(params.imageRight, params.size, "rotateY(90deg) translateX(" + (params.size / 2) + "px)", "top right"),
createSide(params.imageBack, params.size, "translateZ(-" + (params.size / 2) + "px) rotateY(180deg)"),
createSide(params.imageLeft, params.size, "rotateY(270deg) translateX(-" + (params.size / 2) + "px)", "center left")
];
insertDimage(dimage, sides, params);
}
function initDimages() {
var dimages = document.getElementsByClassName("dimage");
for (i = dimages.length - 1; i >= 0; i--) {
var dimage = dimages[i];
initDimage(dimage);
}
}
return {
initDimages
}
}