-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathoutput.js
More file actions
95 lines (77 loc) · 2.36 KB
/
Copy pathoutput.js
File metadata and controls
95 lines (77 loc) · 2.36 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
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
cb.output = {};
(function (exports) {
var defaults = {
rows: 10,
cols: 10,
pixelSize: 10,
borderWidth: 1
};
function PixelGrid(container, opts) {
this.container = container;
this.rows = opts.rows || defaults.rows;
this.cols = opts.cols || defaults.cols;
this.pixelSize = opts.pixelSize || defaults.pixelSize;
this.borderWidth = (opts.borderWidth !== void 0) ? opts.borderWidth : defaults.borderWidth;
this.pixels = [];
for(var i = 0; i<this.rows; i++) {
this.pixels.push([]);
for(var j = 0; j<this.cols; j++) {
this.pixels[i].push(this.black);
}
}
this._draw();
}
PixelGrid.prototype._draw = function () {
var $table = $('<table/>').addClass('pixelGrid');
$table.css('height', (this.rows * this.pixelSize + this.rows * this.borderWidth) + 'px');
$table.css('padding', '0');
$table.css('margin', '0');
$table.css('border-collapse', 'collapse');
this.$pixels = [];
for (var i = 0; i < this.rows; i++) {
var $row = $('<tr/>');
$table.append($row);
this.$pixels.push([]);
for (var j = 0; j < this.cols; j++) {
var $pixel = $('<td/>');
$pixel.css('padding', '0');
$pixel.css('margin', '0');
$pixel.css('width', this.pixelSize + 'px');
$pixel.css('height', this.pixelSize + 'px');
$pixel.css('border', this.borderWidth + 'px solid #c0c0c0');
$row.append($pixel);
this.$pixels[i].push($pixel);
}
}
$(this.container).append($table);
};
PixelGrid.prototype.each = function (fn) {
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
fn(this.$pixels[i][j]);
}
}
};
PixelGrid.prototype.clear = function (color) {
if (color === void 0) {
color = this.black;
}
this.each(function ($e) {
$e.css('background-color', color);
});
for (var i = 0; i < this.rows; i++) {
for (var j = 0; j < this.cols; j++) {
this.pixels[i][j] = color;
}
}
}
PixelGrid.prototype.setPixel = function (col, row, color) {
if (color === void 0) {
color = this.black;
}
this.pixels[row][col] = color;
this.$pixels[row][col].css('background-color', color);
};
PixelGrid.prototype.black = '#000000';
exports.PixelGrid = PixelGrid;
})(cb.output);