-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-shutter.js
executable file
·233 lines (167 loc) · 6.03 KB
/
jquery-shutter.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
228
229
230
231
232
233
$.fn.tzShutter = function (options) {
// Checking for canvas support. Works in all modern browsers:
var supportsCanvas = 'getContext' in document.createElement('canvas');
// Providing default values:
options = $.extend({
openCallback: function () { },
closeCallback: function () { },
loadCompleteCallback: function () { },
hideWhenOpened: true,
imgSrc: 'jquery.shutter/shutter.png'
}, options);
var element = this;
if (!supportsCanvas) {
// If there is no support for canvas, bind the
// callack functions straight away and exit:
element.bind('shutterOpen', options.openCallback)
.bind('shutterClose', options.closeCallback);
options.loadCompleteCallback();
return element;
}
Meteor.setTimeout(function () {
var frames = {num: 15, height: 1000, width: 1000},
slices = {num: 8, width: 416, height: 500, startDeg: 30},
animation = {
width: element.width(),
height: element.height(),
offsetTop: (frames.height - element.height()) / 2
},
// This will calculate the rotate difference between the
// slices of the shutter. (2*Math.PI equals 360 degrees in radians):
rotateStep = 2 * Math.PI / slices.num;
// Calculating the offset
slices.angleStep = ((90 - slices.startDeg) / frames.num) * Math.PI / 180;
// The shutter slice image:
var img = new Image();
// Defining the callback before setting the source of the image:
img.onload = function () {
if (window.console && console.time) {
console.time("Generating Frames");
}
// The film div holds 15 canvas elements (or frames).
var film = $('<div>', {
className: 'film',
css: {
height: frames.num * frames.height,
width: frames.width,
marginLeft: -frames.width / 2, // Centering horizontally
top: -animation.offsetTop
}
});
// The animation holder hides the film with overflow:hidden,
// exposing only one frame at a time.
var animationHolder = $('<div>', {
className: 'shutterAnimationHolder',
css: {
width: animation.width,
height: animation.height
}
});
for (var z = 0; z < frames.num; z++) {
// Creating 15 canvas elements.
var canvas = document.createElement('canvas'),
c = canvas.getContext("2d");
canvas.width = frames.width;
canvas.height = frames.height;
c.translate(frames.width / 2, frames.height / 2);
for (var i = 0; i < slices.num; i++) {
// For each canvas, generate the different
// states of the shutter by drawing the shutter
// slices with a different rotation difference.
// Rotating the canvas with the step, so we can
// paint the different slices of the shutter.
c.rotate(-rotateStep);
// Saving the current rotation settings, so we can easily revert
// back to them after applying an additional rotation to the slice.
c.save();
// Moving the origin point (around which we are rotating
// the canvas) to the bottom-center of the shutter slice.
c.translate(0, frames.height / 2);
// This rotation determines how widely the shutter is opened.
c.rotate((frames.num - 1 - z) * slices.angleStep);
// An additional offset, applied to the last five frames,
// so we get a smoother animation:
var offset = 0;
if ((frames.num - 1 - z) < 5) {
offset = (frames.num - 1 - z) * 5;
}
// Drawing the shutter image
c.drawImage(img, -slices.width / 2, -(frames.height / 2 + offset));
// Reverting back to the saved settings above.
c.restore();
}
// Adding the canvas (or frame) to the film div.
film.append(canvas);
}
// Appending the film to the animation holder.
animationHolder.append(film);
if (options.hideWhenOpened) {
animationHolder.hide();
}
element.css('position', 'relative').append(animationHolder);
var animating = false;
// Binding custom open and close events, which trigger
// the shutter animations.
element.bind('shutterClose', function () {
if (animating) {
return false;
}
animating = true;
var count = 0;
var close = function () {
(function animate() {
if (count >= frames.num) {
animating = false;
// Calling the user provided callback.
options.closeCallback.call(element);
return false;
}
film.css('top', -frames.height * count - animation.offsetTop);
count++;
Meteor.setTimeout(animate, 20);
})();
};
if (options.hideWhenOpened) {
animationHolder.fadeIn(60, close);
}
else {
close();
}
});
element.bind('shutterOpen', function () {
if (animating) {
return false;
}
animating = true;
var count = frames.num - 1;
(function animate() {
if (count < 0) {
var hide = function () {
animating = false;
// Calling the user supplied callback:
options.openCallback.call(element);
};
if (options.hideWhenOpened) {
animationHolder.fadeOut(60, hide);
}
else {
hide();
}
return false;
}
film.css('top', -frames.height * count - animation.offsetTop);
count--;
Meteor.setTimeout(animate, 20);
})();
});
// Writing the timing information if the
// firebug/web development console is opened:
if (window.console && console.timeEnd) {
console.timeEnd("Generating Frames");
}
options.loadCompleteCallback();
};
img.src = options.imgSrc;
}, 0);
return element;
};