So the challenge that I've created for myself is as such.
I have a source photo:
That I am mapping color values and creating a pixelated representation of it using divs
Here's the result:
The code that I'm accomplishing this with is:
'use strict';
var imageSource = 'images/unicorn.jpg';
var img = new Image();
img.src = imageSource;
var canvas = $('<canvas/>')[0];
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
var context = canvas.getContext('2d');
console.log('img height: ' + img.height);
console.log('img width: ' + img.width);
var pixelDensity = 70;
var timerStart = new Date();
for (var i = pixelDensity/2; i < img.height; i += (img.height/pixelDensity) ) {
$('.container').append($('<div class="row">'));
for(var j = pixelDensity/2; j < img.width; j += img.height/pixelDensity) {
var value = context.getImageData(j, i, 1, 1).data;
var colorValue = 'rgb(' + value[0] + ', ' + value[1] + ', ' + value[2] + ')';
$('.row:last').append($('<div class="block">').css({'background-color' : colorValue}));
}
}
var timerStop = new Date();
console.log(timerStop - timerStart + ' ms');
The pixelDensity variable controls how close together the color samples are. The smaller the number, the fewer the samples, taking less time to produce the result. As you increase the number, the samples go up and the function slows down considerably.
I'm curious to know what is making this thing take such a long time. I've looked at slightly similar projects - most notably Jscii - which process the image data much faster, but I can't figure out what the difference is that's offering the added performance.
Thanks for your help!


for(var j = pixelDensity/2; j < img.width; j += img.height/pixelDensity)becomesfor( var j = pixelDensity/2, jmax = img.width, jinc = img.height/pixelDensity; j < jmax; j += jinc )