-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathjquery.interactive_3d.js
228 lines (195 loc) · 8.39 KB
/
jquery.interactive_3d.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
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
/* ===========================================================
* jquery.interactive_3d.js v1.1
* ===========================================================
* Copyright 2013 Pete Rojwongsuriya.
* http://www.thepetedesign.com
*
* Create a 3D interactive object using images as frames
* with one js call
*
* https://github.com/peachananr/interactive_3d
*
* ========================================================== */
!function($){
var defaults = {
frames: 10,
cursor: "move",
speed: 0,
entrance: true,
preloadImages: true,
touchSupport: true,
loading: "Loading..",
autoPlay: false
};
function touchHandler(event) {
var touch = event.changedTouches[0];
var simulatedEvent = document.createEvent("MouseEvent");
simulatedEvent.initMouseEvent({
touchstart: "mousedown",
touchmove: "mousemove",
touchend: "mouseup"
}[event.type], true, true, window, 1,
touch.screenX, touch.screenY,
touch.clientX, touch.clientY, false,
false, false, false, 0, null);
touch.target.dispatchEvent(simulatedEvent);
}
$.fn.preload = function(el) {
$("<div class='images_cache'></div>").hide().appendTo(el);
this.each(function(){
$('<img/>').attr("src", this).appendTo(".images_cache")
});
}
$.fn.drags = function(settings) {
var $el = this;
return $el.css('cursor', settings.cursor).on("mousedown", function(e) {
var $drag = $(this).addClass('draggable'),
cur_pos = e.pageX,
last_position = {};
$drag.parents().on("mousemove", function(e) {
if($('.draggable').length > 0) {
var src = $el.find("img.main-frame").attr("src"),
img_name = src.split('/')[src.split('/').length-1],
cur_frame = img_name.split('_')[1].split('.')[0];
if (typeof(last_position.x) != 'undefined') {
//get the change from last position to this position
var deltaX = last_position.x - e.clientX,
deltaY = last_position.y - e.clientY;
if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX > 0) {
if(cur_frame > 1) {
setTimeout(function() {
var img_name = src.split('/')[src.split('/').length-1]
var directory = src.split('/').slice(0, -1).join("/")
var new_frame = directory + "/" + img_name.split('_')[0] + "_" + (parseInt(cur_frame) - 1) + "." + img_name.split('.')[1]
$el.find("img.main-frame").attr("src", new_frame)
},settings.speed)
} else {
setTimeout(function() {
var img_name = src.split('/')[src.split('/').length-1]
var directory = src.split('/').slice(0, -1).join("/")
var new_frame = directory + "/" + img_name.split('_')[0] + "_" + (parseInt(settings.frames)) + "." + img_name.split('.')[1]
$el.find("img.main-frame").attr("src", new_frame)
},settings.speed)
}
} else if (Math.abs(deltaX) > Math.abs(deltaY) && deltaX < 0) {
if(cur_frame < settings.frames) {
setTimeout(function() {
var img_name = src.split('/')[src.split('/').length-1]
var directory = src.split('/').slice(0, -1).join("/")
var new_frame = directory + "/" + img_name.split('_')[0] + "_" + (parseInt(cur_frame) + 1) + "." + img_name.split('.')[1]
$el.find("img.main-frame").attr("src", new_frame)
},settings.speed)
} else {
setTimeout(function() {
var img_name = src.split('/')[src.split('/').length-1]
var directory = src.split('/').slice(0, -1).join("/")
var new_frame = directory + "/" + img_name.split('_')[0] + "_" + 1 + "." + img_name.split('.')[1]
$el.find("img.main-frame").attr("src", new_frame)
},settings.speed)
}
}
}
last_position = {
x : e.clientX,
y : e.clientY
};
}
$(".draggable").on("mouseup", function() {
$(this).removeClass('draggable')
});
});
e.preventDefault(); // disable selection
}).on("mouseup", function() {
$(this).removeClass('draggable');
});
}
$.fn.interactive_3d = function(options){
var settings = $.extend({}, defaults, options),
el = $(this);
el.find(" > img").addClass("main-frame");
el.drags(settings),
x = 0,
step = 100 / parseInt(settings.frames),
cur_frame = el.find("img.main-frame").attr("src").split('_')[1].split('.')[0];
function animate_3d() {
var src = el.find("img.main-frame").attr("src");
el.find("img.main-frame").css("opacity", (x * step)/100);
if(cur_frame < settings.frames) {
setTimeout(function() {
var img_name = src.split('/')[src.split('/').length-1]
var directory = src.split('/').slice(0, -1).join("/")
var new_frame = directory + "/" + img_name.split('_')[0] + "_" + (parseInt(cur_frame) + 1) + "." + img_name.split('.')[1]
el.find("img.main-frame").attr("src", new_frame)
cur_frame = parseInt(cur_frame) + 1;
},settings.speed)
} else {
setTimeout(function() {
var img_name = src.split('/')[src.split('/').length-1]
var directory = src.split('/').slice(0, -1).join("/")
var new_frame = directory + "/" + img_name.split('_')[0] + "_" + 1 + "." + img_name.split('.')[1]
el.find("img.main-frame").attr("src", new_frame)
cur_frame = 1;
},settings.speed)
}
if (x++ < (settings.frames - 1)) {
if (settings.autoPlay != false) {
setTimeout(animate_3d, 0);
} else {
setTimeout(animate_3d, (x * 1.5));
}
}
}
if (settings.entrance == true && settings.autoPlay == false ) {
if (settings.loading == false && settings.autoPlay == false) animate_3d();
}
if (settings.touchSupport == true) {
document.addEventListener("touchstart", touchHandler, true);
document.addEventListener("touchmove", touchHandler, true);
document.addEventListener("touchend", touchHandler, true);
document.addEventListener("touchcancel", touchHandler, true);
}
if (settings.preloadImages == true) {
var src = el.find("img.main-frame").attr("src");
arr = []
for (var i = 1; i < settings.frames + 1; i++) {
var img_name = src.split('/')[src.split('/').length-1]
var directory = src.split('/').slice(0, -1).join("/")
arr.push(directory + "/" + img_name.split('_')[0] + "_" + i + "." + img_name.split('.')[1])
}
$(arr).preload(el);
if (settings.loading != false) {
var imgs = $(".images_cache > img").not(function() { return this.complete; });
var count = imgs.length;
el.append("<div class='loading_3d'>" + settings.loading + "</div>");
el.find(".main-frame").css("visibility", "hidden");
if (count) {
imgs.load(function() {
count--;
if (!count) {
el.find(".main-frame").css("visibility", "visible");
el.find(".loading_3d").remove();
if (settings.autoPlay == false) animate_3d();
}
});
} else {
el.find(".main-frame").css("visibility", "visible");
el.find(".loading_3d").remove();
if (settings.autoPlay == false) animate_3d();
}
}
}
if (settings.autoPlay != false) {
function intervalTrigger() {
return window.setInterval( function() {
animate_3d();
}, settings.autoPlay );
};
var id = intervalTrigger();
el.mouseenter(function() {
window.clearInterval(id);
}).mouseleave(function() {
id = intervalTrigger();
});
}
}
}(window.jQuery);