-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimSprite.js
69 lines (49 loc) · 2.15 KB
/
animSprite.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
// ============
// animanimSprite STUFF
// ============
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
// Construct a "animanimSprite" from the given `image`,
//
function animSprite(image, frameWidth, frameHeight, numCols, numRows) {
this.image = image;
//this.frameDuration = frameDuration;
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
this.numCols = numCols || 16;
this.numRows = numRows || 7;
this.currentCol = 0;
this.currentRow = numRows || 7;
this.scale = 1;
}
animSprite.prototype.drawAt = function (ctx, x, y) {
ctx.drawImage(this.image, this.currentCol*this.frameWidth, this.currentRow*this.frameHeight, this.frameWidth, this.frameHeight, x, y, this.frameWidth, this.frameHeight);
};
animSprite.prototype.drawCentredAt = function (ctx, cx, cy, rotation) {
if (rotation === undefined) rotation = 0;
// drawImage expects "top-left" coords, so we offset our destination
// coords accordingly, to draw our animSprite centred at the origin
ctx.drawImage(this.image, this.currentCol*this.frameWidth, this.currentRow*this.frameHeight, this.frameWidth, this.frameHeight, cx - (this.frameWidth/2), cy - (this.frameHeight/2), this.frameWidth, this.frameHeight);
};
animSprite.prototype.drawWrappedCentredAt = function (ctx, cx, cy, rotation) {
// Get "screen width"
var sw = g_canvas.width;
// Draw primary instance
this.drawWrappedVerticalCentredAt(ctx, cx, cy, rotation);
// Left and Right wraps
this.drawWrappedVerticalCentredAt(ctx, cx - sw, cy, rotation);
this.drawWrappedVerticalCentredAt(ctx, cx + sw, cy, rotation);
};
animSprite.prototype.drawWrappedVerticalCentredAt = function (ctx, cx, cy, rotation) {
// Get "screen height"
var sh = g_canvas.height;
// Draw primary instance
this.drawCentredAt(ctx, cx, cy, rotation);
// Top and Bottom wraps
this.drawCentredAt(ctx, cx, cy - sh, rotation);
this.drawCentredAt(ctx, cx, cy + sh, rotation);
};