-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSprite.js
48 lines (35 loc) · 1.08 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
// ============
// 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;
}
Sprite.prototype.drawAt = function (ctx, x, y) {
ctx.drawImage(this.image,
x, y);
};
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,
-w/2, -h/2);
ctx.restore();
};