function getTerrain(factor, mapa, terrain) {
let imageWidth = mapa.projection.width * factor;
let imageHeight = mapa.projection.height * factor;
var image = new Image();
image.onload = onload;
image.crossOrigin = "anonymous";
image.src = terrain.src;
console.log(image.src);
let width = mapa.projection.width;
let height = mapa.projection.height;
const projection = mapa.projection.projection;
const context = DOM.context2d(imageWidth, imageHeight, 1);
const clipped = DOM.context2d(width, height, 1);
clipped.canvas.width = width;
clipped.canvas.height = height;
context.canvas.width = width * factor;
context.canvas.height = height * factor;
context.clearRect(0, 0, width, height);
const path = d3.geoPath(projection, context);
function onload() {
var dx = +width * factor;
var dy = +height * factor;
context.drawImage(image, 0, 0, dx, dy);
var sourceData = context.getImageData(0, 0, dx, dy).data;
var target = context.createImageData(width, height);
var targetData = target.data;
for (var y = 0, i = -1; y < height; ++y) {
for (var x = 0; x < width; ++x) {
var p = projection.invert([x, y]),
p0 = p[0],
p1 = p[1];
if (p0 > 180 || p0 < -180 || p1 > 90 || p1 < -90) {
i += 4;
continue;
}
var q =
(((((90 - p1) / 180) * dy) | 0) * dx +
((((180 + p0) / 360) * dx) | 0)) <<
2;
targetData[++i] = sourceData[q];
targetData[++i] = sourceData[++q];
targetData[++i] = sourceData[++q];
targetData[++i] = 255;
}
}
context.clearRect(0, 0, width, height);
context.putImageData(target, 0, 0);
clipped.putImageData(target, 0, 0);
}
return clipped.canvas;
}