-
Notifications
You must be signed in to change notification settings - Fork 0
/
Display-Simulator.html
80 lines (73 loc) · 2.13 KB
/
Display-Simulator.html
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
<!DOCTYPE html>
<html>
<head>
<title>Display Simulator</title>
</head>
<body>
<h1>Display Simulator</h1>
<h3>Choose the <i>rgb.txt</i> file that is generated from simulation</h3>
<input type="file">
Choose resolution:
<select id="res">
<option value="1280x720">1280x720</option>
<option value="800x600">800x600</option>
<option value="640x480">640x480</option>
</select>
<button id="simulate">Render screen</button>
<br />
<br />
<canvas id="canvas" style="border:4px solid #000000;"></canvas>
<script>
var resolution = document.getElementById('res');
document.getElementById("simulate").addEventListener("click", function() {
var reader = new FileReader();
reader.addEventListener('load', function() {
draw(this.result);
});
reader.readAsText(document.querySelector('input').files[0]);
});
function getSelectedOption(sel) {
var opt;
for ( var i = 0, len = sel.options.length; i < len; i++ ) {
opt = sel.options[i];
if ( opt.selected === true ) {
break;
}
}
return opt;
}
function draw(file) {
var opt = getSelectedOption(resolution);
var res = opt.value.split("x");
var canvas = document.getElementById('canvas');
canvas.width = Number(res[0]);
canvas.height = Number(res[1]);
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
var x = 0;
var y = 0;
var r = 0;
var g = 0;
var b = 0;
var lines = file.split('\n');
while (y < Number(res[1])) {
line = lines[y].split(',');
while (x < Number(res[0])) {
r = ((line[x]&0xff0000) >> 16);
g = ((line[x]&0x00ff00) >> 8);
b = (line[x]&0x0000ff);
ctx.fillStyle = "rgb("+r+","+g+","+b+")";
ctx.fillRect(x, y, 1, 1);
x++;
}
x = 0;
y++;
}
}
}
</script>
<footer>
Furkan Çaycı, 2018
</footer>
</body>
</html>