function planet(color, dem) {
const height = width / 2;
const context = DOM.context2d(width, height);
const w = context.canvas.width;
const dpr = w / width;
context.drawImage(dem, 0, 0, width, height);
const d = context.getImageData(0, 0, w, height * dpr).data;
context.drawImage(color, 0, 0, width, height);
const c = context.getImageData(0, 0, w, height * dpr).data;
const E = context.getImageData(0, 0, w, height * dpr);
const e = E.data;
const n = c.length / 4;
const stepX = 4;
const stepY = 4 * w;
function update(elevation, azimuth, strength = 0.3) {
const r = elevation * (Math.PI / 180);
const a = -azimuth * (Math.PI / 180);
const cr = Math.cos(r);
const sr = Math.sin(r);
const ca = Math.cos(a);
const sa = Math.sin(a);
const lz = sr;
const lx = cr * ca;
const ly = cr * sa;
for (let j = 0; j < n; j++) {
const i = 4 * j;
const dzx = d[i + stepX] - d[i - stepX];
const dzy = d[i + stepY] - d[i - stepY];
const norm = Math.hypot(1, dzx, dzy);
const dot = (lz + dzx * lx + dzy * ly) / norm;
const factor = 1 + strength * (dot - cr);
e[i + 0] = c[i + 0] * factor;
e[i + 1] = c[i + 1] * factor;
e[i + 2] = c[i + 2] * factor;
}
context.putImageData(E, 0, 0);
}
update(45, 315, 0.5);
return Object.assign(context.canvas, { update });
}