I'm trying to create a library for procedural textures in JavaScript and for some reason I can't really wrap my head around why my zooming function wont work. I tried lots of things but I always get funky results instead of what I originally wanted to achieve. Basically, if I zoom into a noise texture I want it to appear all blocky.
My current implementation:
this.Zoom = function(factor) {
var imageData, curData, newData,
zoomFactor = Math.pow(2, factor);
curData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
newData = ctx.createImageData(curData);
for (var y = 0; y < ctx.canvas.height; y++) {
for (var x = 0; x < ctx.canvas.width; x++) {
var curIndex = y * ctx.canvas.width + x;
var targetIndex = Math.floor(y / zoomFactor + x / zoomFactor) * 4;
newData.data[curIndex] = curData.data[targetIndex];
newData.data[curIndex + 1] = curData.data[targetIndex + 1];
newData.data[curIndex + 2] = curData.data[targetIndex + 2];
newData.data[curIndex + 3] = curData.data[targetIndex + 3];
}
}
ctx.putImageData(newData, 0, 0);
return this;
}
And here's my JSFiddle: http://jsfiddle.net/j18eqgrq/
For some reason I get this weird effect and I can't wrap my head around it. I think I messed up something with the targetIndex.
In your inner for-loop the code should be:
You should take the RGBA into account in the factor 4, and also the zoomed image index should be calculated through
y * image_width + x