-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphics.js
142 lines (117 loc) · 4.2 KB
/
Graphics.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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* Created with JetBrains WebStorm.
* User: JasonOwnzBiatch
* Date: 22/06/13
* Time: 4:20 PM
* To change this template use File | Settings | File Templates.
*/
var Canvas = (function(){
"use strict"
//ELEMENTS: FIRST = BLOCK, SECOND = EMPTY SPACE
var colourSet = {
aqua: [[160,192,240], [0,32,48]],
grass: [[23,86,0], [150,239,12]],
black: [[0,0,0], [255,255,255]],
white: [[255,255,255], [0,0,0]]
};
var curColourSet = "aqua";
var GFX_WIDTH = 64,
GFX_HEIGHT = 32,
MAGNIFICATION = 10,
CANVAS_WIDTH = GFX_WIDTH * MAGNIFICATION,
CANVAS_HEIGHT = GFX_HEIGHT * MAGNIFICATION,
BLOCK_WIDTH = MAGNIFICATION,
BLOCK_HEIGHT = MAGNIFICATION;
var filledBlock = undefined,
emptyBlock = undefined;
var cur_canvas = undefined,
cur_context = undefined;
function Canvas(id){
var canvas = document.getElementById(id);
canvas.width = (CANVAS_WIDTH);
canvas.height = (CANVAS_HEIGHT);
//canvas.style.border = "1px solid black";
cur_context = canvas.getContext("2d");
cur_canvas = canvas;
this.resetGraphics();
};
//Private:
//Public:
Canvas.prototype.graphics = new Array();
Canvas.prototype.cacheGraphics = new Array();
Canvas.prototype.resetGraphics = function resetGraphics(){
cur_canvas.width = cur_canvas.width;
this.graphics = new Array(GFX_HEIGHT);
this.cacheGraphics = new Array(GFX_HEIGHT);
for(var i = 0; i < this.graphics.length; i++){
this.graphics[i] = new Array(GFX_WIDTH);
this.cacheGraphics[i] = new Array(GFX_WIDTH);
}
for(var row = 0; row < this.graphics.length; row++){
for(var col = 0; col < this.graphics[row].length; col++){
this.graphics[row][col] = 0;
this.cacheGraphics[row][col] = 3;
}
}
}
Canvas.prototype.renderOnCanvas = function(){
for (var row = 0; row < this.graphics.length; row++) {
for (var col = 0; col < this.graphics[row].length; col++) {
if(this.graphics[row][col] != this.cacheGraphics[row][col]){
var colour = undefined;
var x, y;
x = col * MAGNIFICATION;
y = row * MAGNIFICATION;
if(this.graphics[row][col]){
colour = colourSet.aqua[0];
} else {
colour = colourSet.aqua[1];
}
cur_context.fillStyle = "rgb(" + colour[0] + "," + colour[1] + "," + colour[2] + ")";
cur_context.fillRect(x,y,BLOCK_WIDTH,BLOCK_HEIGHT);
this.cacheGraphics[row][col] = this.graphics[row][col];
}
}
}
};
Canvas.prototype.displayMemory = function(num){
var bin = num.toString(2);
var binLength = bin.length;
var pad = "";
if(binLength < 8){
var leftOver = 8 - binLength;
for(var i = 0; i < leftOver; i++){
pad += "0";
}
}
};
Canvas.prototype.displayText = function(){
var div = document.getElementById("fills");
//empty children nodes
div.innerHTML = "";
for (var row = 0; row < this.graphics.length; row++) {
var str = "";
for (var col = 0; col < this.graphics[row].length; col++) {
str += "" + this.graphics[row][col];
}
var line = document.createElement("div");
line.innerText = str;
div.appendChild(line);
}
};
Canvas.prototype.drawDummy = function(){
var imgData = cur_context.createImageData(10, 10);
for (var i=0;i<imgData.data.length;i+=4)
{
imgData.data[i+0]=255;
imgData.data[i+1]=0;
imgData.data[i+2]=0;
imgData.data[i+3]=255;
}
cur_context.putImageData(imgData, 10, 10);
cur_context.putImageData(imgData, 100, 10);
/*cur_context.fillStyle = "rgb(200,0,0)";
cur_context.fillRect(295,10,5,5);*/
};
return Canvas;
})();