-
Notifications
You must be signed in to change notification settings - Fork 73
/
tilt.js
60 lines (48 loc) · 1.66 KB
/
tilt.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
/*
Base off Tim Holman's tile tilt: http://tholman.com/experiments/css3/metro-tiles/
*/
function addTileTilt()
{
var tile = this;
var mouse = {x: 0, y: 0, down: false};
var maxRotation = 16;
var setRotation = function(){
var width = tile.offsetWidth;
var height = tile.offsetHeight;
//Rotations as percentages
var yRotation = (mouse.x - (width / 2)) / (width / 2);
var xRotation = (mouse.y - (height / 2)) / (height / 2);
tile.style.webkitTransform = "rotateX(" + -xRotation * maxRotation + "deg)" +
" rotateY(" + yRotation * maxRotation + "deg)";
tile.style.mozTransform = "rotateX(" + -xRotation * maxRotation + "deg)" +
" rotateY(" + yRotation * maxRotation + "deg)";
tile.style.transform = "rotateX(" + -xRotation * maxRotation + "deg)" +
" rotateY(" + yRotation * maxRotation + "deg)";
}
var MouseDown = function(e){
mouse.x = e.offsetX;
mouse.y = e.offsetY;
mouse.down = true;
setRotation();
}
var MouseUp = function(e){
mouse.down = false;
tile.style.webkitTransform = "rotateX(0deg)" +
" rotateY(0deg)";
tile.style.mozTransform = "rotateX(0deg)" +
" rotateY(0deg)";
tile.style.transform = "rotateX(0deg)" +
" rotateY(0deg)";
}
var MouseMove = function(e){
mouse.x = e.offsetX;
mouse.y = e.offsetY;
if (mouse.down == true){
setRotation();
}
}
tile.addEventListener('mousemove', MouseMove, false);
tile.addEventListener('mousedown', MouseDown, false);
tile.addEventListener('mouseup', MouseUp, false);
tile.addEventListener('mouseout', MouseUp, false);
}