-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.js
More file actions
63 lines (49 loc) · 1.49 KB
/
Sprite.js
File metadata and controls
63 lines (49 loc) · 1.49 KB
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
// ============
// SPRITE STUFF
// ============
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
// Construct a "sprite" from the given `image`,
//
/*
function Sprite(image) {
this.image = image;
this.width = image.width;
this.height = image.height;
this.scale = 1;
}
*/
function Sprite(image, sx, sy, width, height) {
this.sx = sx;
this.sy = sy;
this.width = width;
this.height = height;
this.image = image;
this.scale = 1;
}
Sprite.prototype.drawAt = function (ctx, x, y) {
ctx.drawImage(this.image,
this.sx, this.sy, this.width, this.height,
x, y, this.width*this.scale, this.height*this.scale);
};
Sprite.prototype.drawCentredAt = function (ctx, cx, cy, rotation) {
if (rotation === undefined) rotation = 0;
var w = this.width,
h = this.height;
ctx.save();
ctx.translate(cx, cy);
ctx.rotate(rotation);
ctx.scale(this.scale, this.scale);
// drawImage expects "top-left" coords, so we offset our destination
// coords accordingly, to draw our sprite centred at the origin
ctx.drawImage(this.image,
this.sx, this.sy, this.width, this.height,
-w/2, -h/2, this.width, this.height);
//ctx.drawImage(this.image,
// -w/2, -h/2);
ctx.restore();
};