/
gfx.coffee
143 lines (116 loc) · 3.49 KB
/
gfx.coffee
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
$ = @jQuery
throw 'jQuery required' unless $
$.support.gfx = 'webkitTransitionEnd' of window
defaults =
duration: 400
queue: true
easing: ''
transformTypes = [
'scale', 'scaleX', 'scaleY', 'scale3d',
'rotate', 'rotateX', 'rotateY', 'rotateZ', 'rotate3d',
'translate', 'translateX', 'translateY', 'translateZ', 'translate3d',
'skew', 'skewX', 'skewY',
'matrix', 'matrix3d', 'perspective'
]
$.fn.queueNext = (callback, type) ->
type or= "fx";
@queue ->
callback.apply(this, arguments)
setTimeout =>
jQuery.dequeue(this, type)
$.fn.transform = (properties) ->
transforms = []
for key, value of properties when key in transformTypes
transforms.push("#{key}(#{value})")
delete properties[key]
if transforms.length
properties['-webkit-transform'] = transforms.join(' ')
$(@).css(properties)
$.fn.gfx = (properties, options) ->
opts = $.extend({}, defaults, options)
unless typeof opts.duration is 'string'
opts.duration += 'ms'
properties['-webkit-transition'] = "all #{opts.duration} #{opts.easing}"
callback = ->
$(@).css('-webkit-transition', '')
opts.complete?.apply(this, arguments)
$(@).dequeue()
@[ if opts.queue is false then 'each' else 'queue' ] ->
$(@).one('webkitTransitionEnd', callback)
$(@).transform(properties)
$.fn.gfxPopIn = (options) ->
$(@).queueNext ->
$(@).transform
'-webkit-transform-origin': '50% 50%'
scale: '.2'
opacity: '0',
display: 'block'
$(@).gfx({
scale: '1'
opacity: '1'
}, options)
$.fn.gfxPopOut = (options) ->
$(@).queueNext ->
$(@).transform
'-webkit-transform-origin': '50% 50%'
scale: '1'
opacity: '1'
$(@).gfx({
scale: '.2'
opacity: '0'
}, options)
$(@).queueNext ->
$(@).transform
display: 'none'
opacity: '1'
scale: '1'
$.fn.gfxFadeIn = (options = {}) ->
options.duration ?= 1000
$(@).queueNext ->
$(@).css
display: 'block'
opacity: '0'
$(@).gfx({opacity: 1}, options);
$.fn.gfxFadeOut = (options = {}) ->
$(@).queueNext ->
$(@).css
opacity: 1
$(@).gfx({opacity: 0}, options);
$(@).queueNext ->
$(@).css
display: 'none'
opacity: 1
$.fn.gfxShake = (options = {}) ->
options.duration ?= 100
options.easing ?= 'ease-out'
distance = options.distance or 20
$(@).gfx({translateX: "-#{distance}px"}, options)
$(@).gfx({translateX: "#{distance}px"}, options)
$(@).gfx({translateX: "-#{distance}px"}, options)
$(@).gfx({translateX: "#{distance}px"}, options)
$(@).queueNext ->
$(@).transform(translateX: 0)
$.fn.gfxBlip = (options = {}) ->
scale = options.scale or '1.15'
$(@).gfx({scale: scale}, options)
$(@).gfx({scale: '1'}, options)
$.fn.gfxExplodeIn = (options = {}) ->
scale = options.scale or '3'
$(@).queueNext ->
$(@).transform(scale: scale, opacity: '0', display: 'block')
$(@).gfx({scale: '1', opacity: '1'}, options)
$.fn.gfxExplodeOut = (options = {}) ->
scale = options.scale or '3'
$(@).queueNext ->
$(@).transform(scale: '1', opacity: '1')
$(@).gfx({scale: scale, opacity: '0'}, options)
$(@).queueNext ->
$(@).transform(scale: '1', opacity: '1', display: 'none')
$.fn.gfxFlipIn = (options = {}) ->
$(@).queueNext ->
$(@).transform(rotateY: '180deg', scale: '.8')
$(@).gfx({rotateY: 0, scale: 1}, options)
$.fn.gfxFlipOut = (options = {}) ->
$(@).queueNext ->
$(@).transform(rotateY: 0, scale: 1)
$(@).gfx({rotateY: '-180deg', scale: '.8'}, options)