forked from johnkhunter/jblitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.jblitter.js
135 lines (115 loc) · 3.6 KB
/
jquery.jblitter.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
/*!
* jBlitter: Rich animated HTML5 canvas buttons, now with more jQuery
* http://glowfilter.com/blog/jblitter-animated-html5-canvas-buttons/
*
* Copyright (c) 2010 John Kakoulides (http://glowfilter.com)
* Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
* and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*
*
* Built on top of the jQuery library
* http://jquery.com
*
*/
(function($){
/**
* The jBlitter object.
*
* @constructor
* @class jBlitter
* @param options {Object} A set of key/value pairs to set as configuration properties.
*/
$.fn.jBlitter = function(options){
var settings = {
'resource':'test.png',
'speed':33,
'frameWidth':100,
'frameHeight':100,
'reverse':true,
'loop':false,
'callback':null
};
return this.each(function(){
var opts = $.extend(settings,options);
var canvas = this;
var $canvas = $(this);
var link = $canvas.find("a").first().attr("href");
var context = canvas.getContext('2d');
var curCol = 1;
var curRow = 1;
var curFrame = 1;
var numCols = 0;
var numRows = 0;
var totalFrames = 0;
var interval = 0;
var resource = new Image();
resource.onload = function(){
numRows = Math.ceil(resource.height/opts.frameHeight);
numCols = Math.ceil(resource.width/opts.frameWidth);
totalFrames = numRows * numCols;
var updateCanvas = function(){
curRow = Math.ceil(curFrame/numCols);
var xGrid = (curCol-1)*opts.frameWidth;
var yGrid = (curRow-1)*opts.frameHeight;
context.clearRect(0,0,opts.frameWidth,opts.frameHeight);
context.drawImage(resource,xGrid,yGrid,opts.frameWidth,opts.frameHeight,0,0,opts.frameWidth,opts.frameHeight);
};
var drawNextFrame = function(){
curFrame += 1;
curCol += 1;
if(curFrame >= totalFrames){
if(opts.loop){
curFrame = 1;
curCol = 1;
}else{
clearInterval(interval);
}
}
if (curCol > numCols){
curCol = 1;
}
updateCanvas();
};
var drawPrevFrame = function(){
curFrame -= 1;
curCol -= 1;
if (curCol <= 0){
curCol = numCols;
}
if(curFrame <= 1){
curFrame = 1;
curCol = 1;
clearInterval(interval);
}
updateCanvas();
};
$(canvas).bind("mouseover",function(){
curFrame = 0;
curCol = 0;
clearInterval(interval);
interval = setInterval(drawNextFrame,opts.speed);
});
$(canvas).bind("mouseout",function(){
clearInterval(interval);
if(opts.reverse == true){
interval = setInterval(drawPrevFrame,opts.speed);
}else{
curFrame = 0;
curCol = 0;
drawNextFrame();
}
});
$(canvas).bind("click",function(){
if(opts.callback){
opts.callback();
}
if(link){
window.location.href = link;
}
});
updateCanvas();
};
resource.src = opts.resource;
});
};
})(jQuery);