This repository has been archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
live.html
150 lines (126 loc) · 4.63 KB
/
live.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
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
143
144
145
146
147
148
149
150
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://code.jquery.com/jquery-3.3.1.js" integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60=" crossorigin="anonymous"></script>
<style>
body {
padding: 0;
margin: 0;
}
#myCanvas {
background-color: rgb(255, 255, 255);
}
</style>
</head>
<body>
<canvas id="myCanvas" width="500" height="500"></canvas>
<script>
var canvas = document.getElementById("myCanvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
document.body.scrollTop = 0; // <-- pull the page back up to the top
document.body.style.overflow = 'hidden'
var ctx = canvas.getContext("2d");
var canvasData = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
// That's how you define the value of a pixel //
function drawPixel(x, y, r, g, b, a) {
var index = (x + y * canvasWidth) * 4;
canvasData.data[index + 0] = r;
canvasData.data[index + 1] = g;
canvasData.data[index + 2] = b;
canvasData.data[index + 3] = a;
}
// That's how you update the canvas, so that your //
// modification are taken in consideration //
function updateCanvas() {
ctx.putImageData(canvasData, 0, 0);
}
var centerX = Math.floor(canvas.width / 2);
var centerY = Math.floor(canvas.height * 0.85);
ctx.beginPath();
ctx.arc(centerX,centerY,20,0,2*Math.PI);
ctx.fillStyle = "black";
ctx.fill();
var queue = [];
var j = 0;
function drawPoints() {
var response = JSON.parse(xhr.responseText);
var angle = response.angle;
var distance = response.distance;
var data = [angle, distance];
for (var i = 0; i < data.length; i++) {
var rad = Math.floor(data[0][i] * (Math.PI / 180));
var h = Math.floor(Math.abs(data[1][i]));
var r = Math.floor(Math.random() * 255);
var g = Math.floor(Math.random() * 255);
var b = Math.floor(Math.random() * 255);
var x = 0;
var y = 0;
if (data[1][i] >= 0) {
x = Math.floor((canvasWidth / 2) + Math.sin(rad) * h);
} else {
x = Math.floor((canvasWidth / 2) - Math.sin(rad) * h);
}
y = Math.floor(canvasHeight - Math.cos(rad) * h);
var arr = [x, y, r, g, b, 255];
console.log(arr);
if (j >= 9) {
var temp = queue.shift();
drawPixel(temp[0], temp[1], 255, 255, 255, 255);
drawPixel(temp[0] + 1, temp[1], 255, 255, 255, 255);
drawPixel(temp[0], temp[1] + 1, 255, 255, 255, 255);
drawPixel(temp[0] + 1, temp[1] + 1, 255, 255, 255, 255);
ctx.beginPath();
ctx.strokeStyle = 'rgb(' + 255 + ',' + 255 + ',' + 255 + ')';
ctx.moveTo(centerX, centerY);
ctx.lineTo(temp[0], temp[1]);
ctx.stroke();
queue.push(arr);
console.log(arr);
console.log(Math.floor(Math.pow(h,2)));
console.log(Math.floor(Math.pow((Math.sin(rad) * h), 2) + Math.pow((Math.cos(rad) * h), 2)));
drawPixel(x, y, r, g, b, 255);
drawPixel(x + 1, y, r, g, b, 255);
drawPixel(x, y + 1, r, g, b, 255);
drawPixel(x + 1, y + 1, r, g, b, 255);
ctx.beginPath();
ctx.strokeStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
ctx.moveTo(centerX, centerY);
ctx.lineTo(x, y);
ctx.stroke();
updateCanvas();
} else {
queue.push(arr);
console.log(arr);
console.log(Math.floor(Math.pow(h,2)));
console.log(Math.floor(Math.pow((Math.sin(rad) * h), 2) + Math.pow((Math.cos(rad) * h), 2)));
drawPixel(x, y, r, g, b, 255);
drawPixel(x + 1, y, r, g, b, 255);
drawPixel(x, y + 1, r, g, b, 255);
drawPixel(x + 1, y + 1, r, g, b, 255);
ctx.strokeStyle = 'rgb(' + r + ',' + g + ',' + b + ')';
ctx.beginPath();
ctx.moveTo(centerX, centerY);
ctx.lineTo(x, y);
ctx.stroke();
j++;
updateCanvas();
}
}
}
function httpGetAsync() {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
drawPoints(xmlHttp.responseText);
}
xmlHttp.open("GET", "https://adriandarian.github.io/blindsight/", true); // true for asynchronous
xmlHttp.send(null);
}
var myTime2 = setInterval(httpGetAsync(), 1000);
</script>
</body>
</html>