Olsen Noise, Updated Code
Olsen Noise, with the same highly optimized code as Java version. With Multi-color switch.
HTML
<canvas id="canvas" width="500" height="500">
</canvas>
<fieldset>
<legend>Height Map Properties</legend>
<input type="text" name="iterations" id="iterations">
<label for="iterations">
Iterations(7)
</label>
<label>
<input type="checkbox" id="colors" />Rainbow</label>
</fieldset>
<button type="button" value="Update" id="update">Update</button>
<br> Drag noise around. Enter a new iterations value between 0-7 or so. Hit Update.
<br> (Values greater than 7, fail the shift and are not considered valid. You can do that, but don't consider it demo.)
<br> See <a href="http://godsnotwheregodsnot.blogspot.com/2014/08/3d-olsen-noise.html">my blog post on Olsen Noise</a> for better details.
JavaScript
var SCALE_FACTOR = 2;
//The scale factor is kind of arbitrary, but the code is only consistent for 2 currently. Gives noise for other scale but not location proper.
var BLUR_EDGE = 2; //extra pixels are needed for the blur (3 - 1).
var buildbuffer = BLUR_EDGE + SCALE_FACTOR;
canvas = document.getElementById('canvas');
ctx = canvas.getContext("2d");
var stride = canvas.width + buildbuffer;
var colorvalues = new Array(stride * (canvas.height + buildbuffer));
var iterations = 7;
var xpos = 0;
var ypos = 0;
var singlecolor = true;
/**
* Function adds all the required ints into the ints array.
* Note that the scanline should not actually equal the width.
* It should be larger as per the getRequiredDim function.
*
* @param iterations Number of iterations to perform.
* @param ints pixel array to be used to insert values. (Pass by reference)
* @param stride distance in the array to the next requestedY value.
* @param x requested X location.
* @param y requested Y location.
* @param width width of the image.
* @param height height of the image.
*/
function fieldOlsenNoise(iterations, ints, stride, x, y, width, height) {
olsennoise(ints, stride, x, y, width, height, iterations); //Calls the main routine.
//applyMask(ints, stride, width, height, 0xFF000000);
}
function applyMask(pixels, stride, width, height, mask) {
var index;
index = 0;
for (var k = 0, n = height - 1; k <= n; k++, index += stride) {
for (var j = 0, m = width - 1; j <= m; j++) {
pixels[index + j] |= mask;
}
}
}
/**
* Converts a dimension into the dimension required by the algorithm.
* Due to the blurring, to get valid data the array must be slightly larger.
* Due to the interpixel location at lowest levels it needs to be bigger by
* the max value that can be. (SCALE_FACTOR)
*
* @param dim
* @return
*/
function getRequiredDim(dim) {
return dim + BLUR_EDGE + SCALE_FACTOR;
}
//Function inserts the values into the...