• Advertisement
Sign in to follow this  

Why won't pixel manipulation work in this Javascript

This topic is 571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

var canvas = document.getElementById('canvas');
var canvasWidth = canvas.width;
var canvasHeight = canvas.height;
var ctx = canvas.getContext('2d');
var k = 0;
for (var h = 0; h < 8; h++) {
	for (var i = 0; i < 5; i++) {
		for (var j = 0; j < 6; j++) {
			ctx.fillStyle = 'rgb(' + Math.floor(255 - 63 * i) + ',' +
				Math.floor(255 - 51 * j) + ',' +
				Math.floor(255 - 17 * h) + ')';
			ctx.fillRect(k, 0, 1, 360);
			k++;
		}
	}
}
var imageData = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
var buf = new ArrayBuffer(imageData.data.length);
var buf8 = new Uint8ClampedArray(buf);
var data = new Uint32Array(buf);
var RELUM = [
	['1', '2', '3', '3', '3', '4', '4', '5', '1', '2', '3', ...],
	['1', '2', '3', '3', '3', '4', '4', '4', '1', '2', '2', ...],
	['1', '2', '2', '3', '3', '4', '4', '4', '1', '2', '2', ...],
	['1', '2', '2', '3', '3', '3', '4', '4', '1', '2', '2', ...],
	['1', '2', '2', '2', '3', '3', '3', '3', '1', '1', '2', ...],
	['1', '1', '2', '2', '2', '2', '3', '3', '1', '1', '2', ...],
	['0', '1', '1', '1', '2', '2', '2', '2', '0', '1', '1', ...],
	['0', '0', '0', '1', '1', '1', '1', '1', '0', '0', '0', ...],
	['1', '2', '3', '3', '3', '4', '4', '5', '1', '2', '3', ...],
	['1', '2', '3', '3', '3', '4', '4', '4', '1', '2', '3', ...],
	['1', '2', '2', '3', '3', '4', '4', '4', '1', '2', '2', ...],
	[?],
];



var fps = 30;
var now;
var then = Date.now();
var interval = 1000 / fps;
var delta;
var myMap = {};
var screen = new Array(172800);

for (var z = -240; z < 240; z++) {
	for (var q = -180; q < 180; q++) {
		if (q < 0) {
			screen[(z + 240) * (q + 180)] = (9 - Math.Round(z * q / 5400 * -1)) * 16 + Math.Round(z * q / 2700 * -1)
		} else {
			screen[(z + 240) * (q + 180)] = (9 - Math.Round(z * q / 5400)) * 16 + Math.Round(z * q / 2700)
		}
	}
}
for (var e = 0; e < 216; e++) {
	insert(e.toString(16), e, e)
}

function draw() {

	requestAnimationFrame(draw);

	now = Date.now();
	delta = now - then;

	if (delta > interval) {

		then = now - (delta % interval);
		for (var j = 1; j < canvasHeight + 1; ++j) {
			for (var sl = 0; sl < canvasWidth; ++sl) {
				var i = sl * j + j;
				var foo = (sl + 240)
				var hold = 2 * i + 1
				var holdb = sl % 480 * j + j
				var mm = imageData.slice(hold, hold)
				var nn = screen.slice(holdb, holdb)
				var m = (2 << 6 | imageData.data[2 * i]);
				value = get(m.toString(16));
				switch (Math.ceil((sl + 1) / 240)) {
					case 3:
						foo = sl % 480;
						break;
				}
				foo = foo * j + foo;
				var r = imageData.data[4 * value];
				var g = imageData.data[4 * value + 1];
				var b = imageData.data[4 * value + 2];
				var a = imageData.data[4 * value + 3];
				//Little eddian

				data[foo] = (a << 24) |
					(b << 16) |
					(g << 8) |
					r;
			}
		}
		imageData.data.set(buf8);

		ctx.putImageData(imageData, 0, 0);
		imageData = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
	}
}

draw();

function insert(k1, k2, v) {
	if (!(k1 in myMap)) { myMap[k1] = {}; }
	myMap[k1][k2] = v;
}

function get(k1, k2) {
	return myMap[k1] && myMap[k1][k2];
}
Moderator edit:
Full paste here Edited by fastcall22
Code tags, pls

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement