-
Notifications
You must be signed in to change notification settings - Fork 7
/
Sprite.js
executable file
·118 lines (111 loc) · 3.39 KB
/
Sprite.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
/**
* @name jo.Sprite
* @class Sprite loads and draws Images
* @property {number} width The Images' Width
* @property {number} height The Images' Height
*/
define([ './jo', './Point' ], function(jo, Point) {
jo.Sprite = jo.Class.extend({
width : 0,
height : 0,
loadCall : null,
/**
* @constructor
* @param {string} src takes an image URL
* @param {function} loadCall callback function, gets called when image is loaded
* @methodOf jo.web.Sprite
*/
init : function(src, loadCall) {
this.src = src;
this.img = new Image();
this.img.src = src;
this.loadCall = loadCall;
this.img.onload = jo.bind(this.onLoad, this);
//this.joObject ='Sprite';
},
/**
* @private
* @param event
* @methodOf jo.web.Sprite
*/
onLoad : function(event) {
this.width = this.img.width;
this.height = this.img.height;
event.target.name = this.name;
if(this.loadCall) {
this.loadCall(event);
}
},
/**
* @param {object} options an optional set of options, such as angle, pivot and frame <br/>
* angle: an angle in radians <br/>
* pivot: a point or a direction supported directions are:
* <ul> <li>'center'</li>
* <li>'top'</li>
* <li>'bottom'</li>
* <li>'left'</li>
* <li>'right'</li>
* <li>'topleft'</li>
* <li>'topright'</li>
* <li>'bottomleft'</li>
* <li>'bottomright'</li>
* </ul>
* default is topleft <br/>
* frame: {x: {Number}, y: {Number}, width: {Number}, height: {Number}}
* @param {object} position a position object containing x and y
* @param {jo.Surface} surface A Jomoho Surface e.g. screen
* @methodOf jo.web.Sprite
*/
draw : function(options, position, surface) {
surface.ctx.save();
surface.ctx.translate(position.x, position.y);
var srcW = width = this.width,
srcH = height = this.height,
x = 0,
y = 0;
if(typeof options === 'object'){
if(typeof options.angle !== 'undefined'){
surface.ctx.rotate(options.angle);
}
if(typeof options.frame === 'object'){
srcW = width = options.frame.width;
srcH = height = options.frame.height;
x = options.frame.x;
y = options.frame.y;
}
if(typeof options.resize === 'number'){
width *= options.resize;
height *= options.resize;
}
if(typeof options.pivot !== 'undefined'){
if(options.pivot === 'center'){
surface.ctx.translate(-width / 2, -height / 2);
}else if(options.pivot === 'top'){
surface.ctx.translate(-width / 2, 0);
}else if(options.pivot === 'topright'){
surface.ctx.translate(-width, 0);
}else if(options.pivot === 'bottom'){
surface.ctx.translate(-width / 2, -height);
}else if(options.pivot === 'bottomleft'){
surface.ctx.translate(0, -height);
}else if(options.pivot === 'bottomright'){
surface.ctx.translate(-width, -height);
}else if(options.pivot === 'left'){
surface.ctx.translate(0, -height / 2);
}else if(options.pivot === 'right'){
surface.ctx.translate(-width , -height / 2);
}
//custom Point is possible aswell
if(typeof options.pivot.x !== 'undefined' && typeof options.pivot.y !== 'undefined'){
surface.ctx.translate(options.pivot.x, options.pivot.y);
}
}
}
x = ~~ (x+0.5);
y = ~~ (y+0.5);
surface.ctx.drawImage(this.img, x, y, srcW, srcH, 0, 0, width, height);
surface.ctx.restore();
}
});
return jo.Sprite;
});