-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdisplay.js
More file actions
62 lines (52 loc) · 1.45 KB
/
Copy pathdisplay.js
File metadata and controls
62 lines (52 loc) · 1.45 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
// A display of 64x32 pixels scaled to the canvas dimensions.
function Display(canvas) {
this.canvas = canvas
this.context = canvas.getContext("2d")
// Display dimensions
this.width = 64
this.height = 32
// The scale of one display pseudo-pixel
//
// A canvas of 640px width = one pseudo-pixels is 10px width
//
this.xScale = this.canvas.width / this.width
this.yScale = this.canvas.height / this.height
this.clear()
}
// Clear the display. Draw all black.
Display.prototype.clear = function() {
// A 64x32 matrix representing state of each pseudo-pixels on the canvas.
this.pixels = _.times(64, function() {
return _.times(32, _.constant(0))
})
this.context.fillStyle = '#000'
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height)
}
// XOR a pixel at (x,y).
// Returns true if the pixel was turned on.
Display.prototype.xorPixel = function(x, y) {
// Wrap around vertically
if (x > this.width) {
x -= this.width
} else if (x < 0) {
x += this.width
}
// Wrap around horizontally
if (y > this.height) {
y -= this.height
} else if (y < 0) {
y += this.height
}
// Set the pixel state
var active = this.pixels[x][y] ^= 1
if (active) {
// Draw pixel
this.context.fillStyle = '#fff'
} else {
// Erase pixel
this.context.fillStyle = '#000'
}
this.context.fillRect(x * this.xScale, y * this.yScale, this.xScale, this.yScale)
// Return pixel state
return active
}