PixelCompetition/JavaScript

Aus Magrathea Laboratories e.V.
< PixelCompetition
Version vom 28. Dezember 2020, 13:31 Uhr von Civic (Diskussion | Beiträge)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springen Zur Suche springen
 

https://code.visualstudio.com/download

https://nodejs.org/en/download/

https://code.visualstudio.com/docs/editor/portable


Werft alles in ein Verzeichnis und öffnet die Datei nodevars.bat

Letzte Zeile --> %~dp0\code.exe

 
var msg = 'Hello World';
console.log(msg);

var net = require('net');

var host = "box.pixel-competition.de";
var port = 2342;
//host = "localhost";

var client = new net.Socket();
client.connect(port, host, function() {
	console.log('Connected');
	client.write('help');
});

client.on('data', function(data) {
	console.log(""+data);
});

client.on('close', function() {
	console.log('Connection closed');
});

client.write('help\n');

let r = 0;
let g = 0;
let b = 255;

/*
for (var x = 0; x < 100; x++)
    for (var y = 0; y < 100; y++)
    {
        let cmd = `PX ${x} ${y} ${r} ${g} ${b}\n`;
        console.log(cmd)
        client.write(cmd)
    }
*/

// npm install canvas
const { loadImage, createCanvas } = require('canvas');
var imagePath = __dirname + "/Nut.jpg" //DON'T do this in real life!
loadImage(imagePath).then(
    image =>
    {
        console.log(image.width);        
        console.log(image.height);        
        var canvas = createCanvas(image.width, image.height);
        var ctx = canvas.getContext("2d");        
        ctx.drawImage(image,0,0);        
                var data = ctx.getImageData(0,0,image.width,image.height).data;                

                for (var x = 0; x < image.width; x++)
                    for (var y = 0; y < image.height; y++)
                    {
                        var redIndex = y * (image.width * 4) + x * 4;
                        r = data[redIndex];
                        g = data[redIndex+1];
                        b = data[redIndex+2];
                        let cmd = `PX ${x} ${y} ${r} ${g} ${b}\n`;
                        console.log(cmd)
                        client.write(cmd)        
                    }
                }
)