A while back I had to generate an image with the normals of a spheroid.

Normal maps are used to store normalized vector information in a pixel. Very useful to calculate lighting effects in 3D engines.

A normalized vector is a vector of length == 1.

## How it’s done?

<canvas id="canvas" width="256" height="256"></canvas>


And define some basic helper functions

function distance(xa, ya, xb, yb) {
return Math.sqrt((xa - xb) * (xa - xb) + (ya - yb) * (ya - yb));
}

function normalizeVector(vector) {
// a normalized vector is a vector of length 1.0
// we get the length of 1 by dividing the whole
// vector by its magnitude

// the magnitude of a vector V is the distance from (0,0,0) to V

var length = Math.sqrt(
vector * vector +
vector * vector +
vector * vector
)

if (length != 0.0) {
vector = (vector / length)
vector = (vector / length)
vector = (vector / length)
}

return vector
}


Then define some helpers to access the canvas to draw to

const element = document.getElementById('canvas');
const canvas = element.getContext('2d');
const width = element.width;
const height = element.height;
const radius = Math.min(width, height) / 2.0;

const picture = canvas.createImageData(width, height);
var pointer = 0;


The main loop

// for each pixel
for (var x = 0; x < width; x++) {
for (var y = 0; y < height; y++) {

var altitude = Math.sqrt(
);

// altitude is NaN when distance > radius

var v = normalizeVector([
altitude
])

// transform the vector space (-1,1) to pixel space [0,255]
// being {0.5, 0.5} => {0, 0}
picture.data[pointer++] = (v * 128) + 128;
picture.data[pointer++] = (v * 128) + 128;
picture.data[pointer++] = (v * 128) + 128;
picture.data[pointer++] = 255;
}
}



Finally, write the image to the canvas

canvas.putImageData(picture, 0, 0);