img test
by AaronLayton
JavaScript
;// Create an image block
var imgWidth = 2,
imgHeight = 2,
pixelSize = 2,
permutationLog = [],
availableValues = [0,1];
var tmpImage = [1,1,1,1,1,
1,0,0,0,1,
1,0,1,0,1,
1,0,0,0,1,
1,1,1,1,1];
function createImg( pixelArray ){
var imgContainer = $("<div>").css({
backgroundColor: '#eee',
float: 'left',
padding: pixelSize,
margin: pixelSize,
overflow: 'hidden',
width: (pixelSize * imgWidth)
});
// loop through array
for (var i = 0; i < pixelArray.length; i++){
var pixel = $("<div>").css({
backgroundColor: (pixelArray[i]==1) ? '#000' : '#fff',
float: 'left',
height: pixelSize ,
width: pixelSize
});
$(imgContainer).append(pixel);
}
$("body").append(imgContainer);
}
function getNewPermutation(){
var thisPermutation = [];
// Create permutation
for (var y = 0; y < imgHeight; y++){
for (var x = 0; x < imgWidth; x++){
thisPermutation.push( availableValues [Math.floor(Math.random()*availableValues .length)] );
}
}
return thisPermutation;
}
function checkIfExists(permutation){
var tmp = $.grep(permutationLog, function(n, i){
return n.toString() == permutation.toString();
});
if (tmp.toString() == [].toString()){
return false;
}else{
return true;
}
}
while(permutationLog.length < 100){
var thisPermutation = getNewPermutation();
var ifExists = checkIfExists(thisPermutation);
if (!ifExists){
permutationLog.push( thisPermutation );
createImg( thisPermutation );
}
}