archive/gyrio/index.js

95 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-09-30 18:32:26 -04:00
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
2017-09-30 18:46:31 -04:00
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
2017-09-30 20:54:55 -04:00
var frames = [];
2017-09-30 18:32:26 -04:00
function randInt(min,max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
2017-09-30 18:43:11 -04:00
function toBinary(text){
var output = "";
for (var i = 0; i < text.length; i++)
{
var letter = text[i].charCodeAt(0).toString(2);
output += "0".repeat(8-letter.length) + letter;
}
return output;
2017-09-30 18:44:37 -04:00
}
2017-09-30 18:32:26 -04:00
function generateString() {
var final = "";
var length = randInt(50,100);
for(var i = 0; i < length; i++) {
final += randInt(0,1);
}
return final;
}
2017-09-30 20:54:55 -04:00
function generateFrames(frame, string, width, height, rate) {
// Rate is in bits per second
// 30 frames per second
2017-09-30 21:43:39 -04:00
var bitsPerSection = Math.ceil(canvas.width/width);
2017-09-30 20:54:55 -04:00
var shift = canvas.width-rate*width*frame/30;
var bitsToCalc = Math.ceil(rate*width*frame/(width*30));
var output = [];
console.log(shift);
console.log(bitsToCalc);
2017-09-30 21:43:39 -04:00
for(var i = bitsToCalc-bitsPerSection; i < bitsToCalc; i++) {
try {
var x = i*width;
if(parseInt(string[i])) {
output.push([x+shift, -height+canvas.height/2]);
output.push([x+width+shift, -height+canvas.height/2]);
if(string[i-1] === 0) {
output.push([x+width+shift, canvas.height/2]);
}
} else {
output.push([x+shift, canvas.height/2]);
output.push([x+width+shift,canvas.height/2]);
2017-09-30 20:54:55 -04:00
}
2017-09-30 21:43:39 -04:00
} catch(err) {}
2017-09-30 20:54:55 -04:00
}
return output;
2017-09-30 18:46:31 -04:00
}
2017-09-30 22:06:56 -04:00
2017-09-30 21:43:39 -04:00
var string = toBinary(getWebsite("https://www.google.com"));
2017-09-30 18:46:31 -04:00
function drawFrame(frame) {
2017-09-30 20:54:55 -04:00
ctx.fillRect(0,0, canvas.width, canvas.height);
2017-09-30 18:46:31 -04:00
ctx.beginPath();
2017-09-30 22:06:56 -04:00
var frame = generateFrames(frame, string, 100, 100, 10000000000000);
2017-09-30 20:54:55 -04:00
console.log(frame);
ctx.moveTo(0, canvas.height/2);
2017-09-30 18:46:31 -04:00
for(var i = 0; i < frame.length; i++) {
2017-09-30 20:54:55 -04:00
ctx.lineTo(frame[i][0], frame[i][1]);
2017-09-30 18:46:31 -04:00
}
2017-09-30 20:54:55 -04:00
ctx.strokeStyle="#4CAF50";
ctx.stroke();
2017-09-30 18:46:31 -04:00
}
2017-09-30 18:32:52 -04:00
2017-09-30 22:06:56 -04:00
function getWebsite(url) {
var proxy = "https://cors-anywhere.herokuapp.com/";
var xhr = new XMLHttpRequest();
xhr.open("GET", proxy+url, false);
xhr.send();
return xhr.responseText;
2017-09-30 18:30:11 -04:00
}
2017-09-30 18:46:31 -04:00
2017-09-30 20:54:55 -04:00
function animate(n) {
2017-09-30 22:06:56 -04:00
if(!doAnimate) return;
2017-09-30 20:54:55 -04:00
drawFrame(n);
setTimeout(function() { animate(n+1) }, 100/3);
}
2017-09-30 22:06:56 -04:00
ctx.fillStyle = "#000";
ctx.fillRect(0,0, canvas.width, canvas.height);
var doAnimate = true;
2017-09-30 20:54:55 -04:00
animate(0);
2017-09-30 21:43:39 -04:00
2017-09-30 22:06:56 -04:00