"use strict";
// Default values for the min. and max. Y of biomes
const MIN_Y_DEFAULT = -31000
const MAX_Y_DEFAULT = 31000
// Draw a grid line every GRID_STEP units
const GRID_STEP = 10
// Size of the resizing corner
const RESIZE_CORNER = 14;
// Minimum canvas side length (px)
const MIN_CANVAS_SIZE = 100;
// Minimum required distance of canvas from the right page side
const CANVAS_PAGE_MARGIN_RIGHT = 20
// Grid widths. We use lower grid widths
// as the grid becomes more crammed.
// There are 4 levels from 0 to 3.
// Level 3 is the full width, level 1 is the
// smallest with and level 0 renders no grid lines
// when the grid would become too crammed.
const GRID_WIDTH_LEVEL_3 = 2; // full width
const GRID_WIDTH_LEVEL_2 = 1; // reduced width
const GRID_WIDTH_LEVEL_1 = 0.5; // more reduced width
// the grid thesholds are the "grid lines to pixel"
// ratio. The more grid lines therere are per pixel,
// the lower the grid level.
// e.g. grid level 2 triggers if ratio is above
// GRID_THRESHOLD_LEVEL_2. Grid level 3 is used
// if no grid thresholds are triggered.
const GRID_THRESHOLD_LEVEL_2 = 0.08;
const GRID_THRESHOLD_LEVEL_1 = 0.16;
const GRID_THRESHOLD_LEVEL_0 = 0.24; // this will disable grid rendering
// Distance from the point's center in which a point can
// be selected by clicking on it
const POINT_SELECT_DISTANCE = 25
// Distance a point has to be dragged after a single click
// before drag-n-drop starts.
// Normally, the user can drag-n-drop a point
// when clickong and dragging it *after* it has already been
// selected; there's no distance requirement.
// But it is also possible to select the point (by clicking
// on it) and dragging it with the same mouse click. But for
// this method, the mouse had to move the distance below
// from the point coordinates (in pixels) before drag-n-drop
// activates.
const ONE_CLICK_DRAG_DROP_DISTANCE = 30
// Name to display if empty
const FALLBACK_NAME = "(no name)"
// Symbol for storing the biome ID in site objects
// for the Voronoi script
const biomeIDSymbol = Symbol("Biome ID");
// Colors (mostly self-explanatory)
const POINT_COLOR = "#913636";
const POINT_COLOR_SELECTED = "#e19696";
const EDGE_COLOR = "#0f2c2e";
const GRID_COLOR = "#00000040";
const AXIS_COLOR = "#000000";
// Color to be used when the diagram is cleared
const CLEAR_COLOR = "#ecddba";
// list of possible cell colors
// note: These MUST be in "#xxxxxx" format
// for the hexColorToRGBColor function to work.
const CELL_COLORS = [
"#64988e",
"#3d7085",
"#345644",
"#6b7f5c",
"#868750",
"#a7822c",
"#a06e38",
"#ad5f52",
"#692f11",
"#89542f",
"#796e63",
"#a17d5e",
"#5a3f20",
"#836299",
];
const CELL_COLOR_NEUTRAL = "#888888";
const COLOR_BUTTON_BORDER_SELECTED = "#FF0000";
/* Status variables for the diagram calculations */
// Min. and max. mathematically possible values for heat and humidity
let limit_heat_min, limit_heat_max;
let limit_humidity_min, limit_humidity_max;
// Draw area. Slightly larger than the value area to avoid
// ugly edge rendering problems
const DRAW_OFFSET = 10
let draw_heat_min, draw_heat_max;
let draw_humidity_min, draw_humidity_max;
// The point that is considered the middle of heat/humidity;
// mathematically this value is the most probable.
let midpoint_heat;
let midpoint_humidity;
// Biome noise settings
const NOISE_OFFSET_DEFAULT = 50;
const NOISE_SCALE_DEFAULT = 50;
const NOISE_PERSISTENCE_DEFAULT = 0.5;
const NOISE_OCTAVES_DEFAULT = 3;
const NOISE_ABSVALUE_DEFAULT = false;
// Current noise values
let noises = {
heat: {
offset: NOISE_OFFSET_DEFAULT,
scale: NOISE_SCALE_DEFAULT,
octaves: NOISE_OCTAVES_DEFAULT,
persistence: NOISE_PERSISTENCE_DEFAULT,
absvalue: NOISE_ABSVALUE_DEFAULT,
},
humidity: {
offset: NOISE_OFFSET_DEFAULT,
scale: NOISE_SCALE_DEFAULT,
octaves: NOISE_OCTAVES_DEFAULT,
persistence: NOISE_PERSISTENCE_DEFAULT,
absvalue: NOISE_ABSVALUE_DEFAULT,
},
};
function updateAreaVarsFor(noiseType) {
let noise = noises[noiseType];
let is_absolute = noise.absvalue === true
// Calculate min. and max. possible values
// Octaves
let [o_min, o_max] = [0, 0]
for (let o=1; o<=noise.octaves; o++) {
let exp = o-1
// Calculate the two possible extreme values
// with the octave value being either at 1 or -1.
let limit1 = (1 * noise.persistence ** exp)
let limit2
if (!is_absolute) {
limit2 = (-1 * noise.persistence ** exp)
} else {
// If absvalue is set, one of the
// limits is always 0 because we
// can't get lower.
limit2 = 0
}
// To add to the maximum, pick the higher value
if (limit1 > limit2) {
o_max = o_max + limit1
} else {
o_max = o_max + limit2
}
// To add to the minimum, pick the LOWER value
if (limit1 > limit2) {
o_min = o_min + limit2
} else {
o_min = o_min + limit1
}
}
// Add offset and scale to min/max value (final step)
let min_value = noise.offset + noise.scale * o_min
let max_value = noise.offset + noise.scale * o_max
// Bring the 2 values in the correct order
// (min_value might be bigger for negative scale)
if (min_value > max_value) {
[min_value, max_value] = [max_value, min_value]
}
// Update globals
let limit_min = min_value;
let limit_max = max_value;
let draw_min = limit_min - DRAW_OFFSET
let draw_max = limit_max + DRAW_OFFSET
let midpoint = min_value + (max_value - min_value) / 2
if (noiseType === "heat") {
limit_heat_min = limit_min;
limit_heat_max = limit_max;
draw_heat_min = draw_min;
draw_heat_max = draw_max;
midpoint_heat = midpoint;
} else if (noiseType === "humidity") {
limit_humidity_min = limit_min;
limit_humidity_max = limit_max;
draw_humidity_min = draw_min;
draw_humidity_max = draw_max;
midpoint_humidity = midpoint;
} else {
console.log("ERROR! updateAreaVars() called with wrong noise_type!")
}
}
function updateAreaVars() {
updateAreaVarsFor("heat");
updateAreaVarsFor("humidity");
// Update element
rangeDisplay.innerHTML = "heat range: ["+(+limit_heat_min)+", "+(+limit_heat_max)+"]; " +
"humidity range: ["+(+limit_humidity_min)+", "+(+limit_humidity_max)+"]";
}
updateAreaVars();
// If true, point names are shown in diagram
let showNames = true;
// If true, points are shown in diagram
let showPoints = true;
// If true, cells are colorized in diagram
let showCellColors = true;
// If true, show the grid in the diagram
let showGrid = true;
// If true, show the heat/humidity axes
let showAxes = false;
// Set to true if the draw canvas currently shows an error message
let drawError = false;
// The last ID assigned to a biome (0 = none assigned yet).
let lastBiomeID = 0;
let biomePoints = [];
// Add a biome to the biome list
function addBiome(biomeDef) {
biomeDef.id = lastBiomeID;
biomeDef.colorIndex = lastBiomeID % CELL_COLORS.length;
biomePoints.push(biomeDef);
// The biome ID is just a simple ascending number
lastBiomeID++;
}
// Current cursor position on the canvas
let canvas_cursor_x = null;
let canvas_cursor_y = null;
// Add a default biome at the midpoint
addBiome({name: "default", heat:midpoint_heat, humidity:midpoint_humidity, min_y: MIN_Y_DEFAULT, max_y: MAX_Y_DEFAULT})
// Get the Y vale of the widget to set the Y altitude
function getViewY() {
if (!inputViewY) {
return 0;
}
return inputViewY.value;
}
// Returns the biome point by its given ID
// or null if it couldn't be found
function getBiomeByID(id) {
for(let b=0; b0; c-=4) {
context.moveTo(voronoiCanvas.width, voronoiCanvas.height - c);
context.lineTo(voronoiCanvas.width - c, voronoiCanvas.height);
}
context.strokeStyle = "#00000080";
context.closePath();
context.stroke();
}
}
/* Put the name of the given point on the draw context */
function putPointName(context, point) {
let he = point.heat
let hu = point.humidity
if (he < limit_heat_min || he > limit_heat_max || hu < limit_humidity_min || hu > limit_humidity_max) {
return;
}
let [x, y] = biomeCoordsToCanvasPixelCoords(he, hu);
let w = voronoiCanvas.width;
let h = voronoiCanvas.height;
if (x > w/2) {
context.textAlign = "right";
x = x-5;
} else {
context.textAlign = "left";
x = x+5;
}
if (y < h/2) {
context.textBaseline = "top";
} else {
context.textBaseline = "alphabetic";
}
context.font = "120% sans-serif"
let displayName = point.name;
if (displayName === "") {
displayName = FALLBACK_NAME;
}
context.fillText(displayName, x, y);
}
/* Put the given point on the draw context */
function putPoint(context, point) {
const ARROW_SIZE_SIDE = 7;
const ARROW_SIZE_CORNER = 9;
let he = point.heat
let hu = point.humidity
let [x, y] = biomeCoordsToCanvasPixelCoords(he, hu);
let w = voronoiCanvas.width;
let h = voronoiCanvas.height;
let [limit_x_min, limit_y_min] = biomeCoordsToCanvasPixelCoords(limit_heat_min, limit_humidity_min);
let [limit_x_max, limit_y_max] = biomeCoordsToCanvasPixelCoords(limit_heat_max, limit_humidity_max);
// Point is out of bounds: Draw an arrow at the border
if (he < limit_heat_min || he > limit_heat_max || hu < limit_humidity_min || hu > limit_humidity_max) {
context.beginPath();
// bottom left corner
if (he < limit_heat_min && hu < limit_humidity_min) {
context.moveTo(limit_x_min, limit_y_min);
context.lineTo(limit_x_min + ARROW_SIZE_CORNER, limit_y_min);
context.lineTo(limit_x_min, limit_y_min - ARROW_SIZE_CORNER);
context.closePath();
context.fill();
// bottom right corner
} else if (he > limit_heat_max && hu < limit_humidity_min) {
context.moveTo(limit_x_max, limit_y_min);
context.lineTo(limit_x_max - ARROW_SIZE_CORNER, limit_y_min);
context.lineTo(limit_x_max, limit_y_min - ARROW_SIZE_CORNER);
context.closePath();
context.fill();
// top left corner
} else if (he < limit_heat_min && hu > limit_humidity_max) {
context.moveTo(limit_x_min, limit_y_max);
context.lineTo(limit_x_min + ARROW_SIZE_CORNER, limit_y_max);
context.lineTo(limit_x_min, limit_y_max + ARROW_SIZE_CORNER);
context.closePath();
context.fill();
// top right corner
} else if (he > limit_heat_max && hu > limit_humidity_max) {
context.moveTo(limit_x_max, limit_y_max);
context.lineTo(limit_x_max - ARROW_SIZE_CORNER, limit_y_max);
context.lineTo(limit_x_max, limit_y_max + ARROW_SIZE_CORNER);
context.closePath();
context.fill();
// left side
} else if (he < limit_heat_min) {
context.moveTo(limit_x_min, y);
context.lineTo(limit_x_min + ARROW_SIZE_SIDE, y + ARROW_SIZE_SIDE);
context.lineTo(limit_x_min + ARROW_SIZE_SIDE, y - ARROW_SIZE_SIDE);
context.closePath();
context.fill();
// right side
} else if (he > limit_heat_max) {
context.moveTo(limit_x_max, y);
context.lineTo(limit_x_max - ARROW_SIZE_SIDE, y + ARROW_SIZE_SIDE);
context.lineTo(limit_x_max - ARROW_SIZE_SIDE, y - ARROW_SIZE_SIDE);
context.closePath();
context.fill();
// bottom side
} else if (hu < limit_humidity_min) {
context.moveTo(x, limit_y_min);
context.lineTo(x - ARROW_SIZE_SIDE, limit_y_min - ARROW_SIZE_SIDE);
context.lineTo(x + ARROW_SIZE_SIDE, limit_y_min - ARROW_SIZE_SIDE);
context.closePath();
context.fill();
// top side
} else if (hu > limit_humidity_max) {
context.moveTo(x, limit_y_max);
context.lineTo(x - ARROW_SIZE_SIDE, limit_y_max + ARROW_SIZE_SIDE);
context.lineTo(x + ARROW_SIZE_SIDE, limit_y_max + ARROW_SIZE_SIDE);
context.closePath();
context.fill();
}
// Point is in bounds: Draw a dot
} else {
context.beginPath();
context.moveTo(0, 0);
context.arc(x, y, 5, 0, Math.PI * 2);
context.closePath();
context.fill();
}
};
/* Put the grid on the draw context */
function putGrid(context) {
let [limit_x_min, limit_y_min] = biomeCoordsToCanvasPixelCoords(limit_heat_min, limit_humidity_min);
let [limit_x_max, limit_y_max] = biomeCoordsToCanvasPixelCoords(limit_heat_max, limit_humidity_max);
// Calculate the "grid lines pixel ratio" to reduce the
// width of grid lines or even disable rendering them.
// A high ratio means that a LOT of grid lines would render
// on the canvas.
// This code will effectively trigger if the value range
// is very high.
let xGridLinesPerPixel = (limit_heat_max-limit_heat_min) / voronoiCanvas.width / GRID_STEP;
let yGridLinesPerPixel = (limit_humidity_max-limit_humidity_min) / voronoiCanvas.height/ GRID_STEP;
let xWidth = GRID_WIDTH_LEVEL_3;
let yWidth = GRID_WIDTH_LEVEL_3;
if (xGridLinesPerPixel > GRID_THRESHOLD_LEVEL_0) {
xWidth = null;
} else if (xGridLinesPerPixel > GRID_THRESHOLD_LEVEL_1) {
xWidth = GRID_WIDTH_LEVEL_1;
} else if (xGridLinesPerPixel > GRID_THRESHOLD_LEVEL_2) {
xWidth = GRID_WIDTH_LEVEL_2;
}
if (yGridLinesPerPixel > GRID_THRESHOLD_LEVEL_0) {
yWidth = null;
} else if (yGridLinesPerPixel > GRID_THRESHOLD_LEVEL_1) {
yWidth = GRID_WIDTH_LEVEL_1;
} else if (xGridLinesPerPixel > GRID_THRESHOLD_LEVEL_2) {
yWidth = GRID_WIDTH_LEVEL_2;
}
context.strokeStyle = GRID_COLOR;
if (xWidth !== null) {
context.lineWidth = xWidth;
for (let he=0; he<=limit_heat_max; he+=GRID_STEP) {
let [x, _] = biomeCoordsToCanvasPixelCoords(he, 0);
context.beginPath();
context.moveTo(x, limit_y_min);
context.lineTo(x, limit_y_max);
context.stroke();
}
for (let he=-GRID_STEP; he>=limit_heat_min; he-=GRID_STEP) {
let [x, _] = biomeCoordsToCanvasPixelCoords(he, 0);
context.beginPath();
context.moveTo(x, limit_y_min);
context.lineTo(x, limit_y_max);
context.stroke();
}
}
if (yWidth !== null) {
context.lineWidth = yWidth;
for (let hu=0; hu<=limit_humidity_max; hu+=GRID_STEP) {
let [_, y] = biomeCoordsToCanvasPixelCoords(0, hu);
context.beginPath();
context.moveTo(limit_x_min, y);
context.lineTo(limit_x_max, y);
context.stroke();
}
for (let hu=-GRID_STEP; hu>=limit_humidity_min; hu-=GRID_STEP) {
let [_, y] = biomeCoordsToCanvasPixelCoords(0, hu);
context.beginPath();
context.moveTo(limit_x_min, y);
context.lineTo(limit_x_max, y);
context.stroke();
}
}
}
/* Put the labelled heat/humidity axes on the draw context */
function putAxes(context) {
// Size of arrows (px)
const AXIS_ARROW_SIZE = 8;
// Offset that arrows have from the border (px)
const ARROW_OFFSET = 1;
// Minimum distance (px) that axis must have from the border
const AXIS_BORDER_OFFSET = 10;
// Maximum distance (px) from certain borders at which the axis
// labels and ticks will be put on the other sides
const AXIS_LABEL_FLIP_OFFSET = 40;
context.lineWidth = 2;
context.strokeStyle = AXIS_COLOR;
let [x0, y0] = biomeCoordsToCanvasPixelCoords(0, 0);
let tick_heat = (limit_heat_max - limit_heat_min) * (100/175);
let tick_humidity = (limit_humidity_max - limit_humidity_min) * (100/175);
let [tx, ty] = biomeCoordsToCanvasPixelCoords(tick_heat, tick_humidity);
let w = voronoiCanvas.width;
let h = voronoiCanvas.height;
// If axis would go out of bounds, force them to
// be rendered at the side instead.
let other_side_x = false;
let other_side_y = false;
if (x0 <= AXIS_BORDER_OFFSET) {
x0 = AXIS_BORDER_OFFSET;
} else if (x0 >= w - AXIS_BORDER_OFFSET) {
x0 = w - AXIS_BORDER_OFFSET;
}
if (y0 <= AXIS_BORDER_OFFSET) {
y0 = AXIS_BORDER_OFFSET;
} else if (y0 >= h - AXIS_BORDER_OFFSET) {
y0 = h - AXIS_BORDER_OFFSET;
}
// Flip axis labels if coming close to certain sides
if (x0 <= AXIS_LABEL_FLIP_OFFSET) {
other_side_y = true;
}
if (y0 >= h - AXIS_LABEL_FLIP_OFFSET) {
other_side_x = true;
}
// horizontal axis
context.beginPath();
context.moveTo(0, y0);
context.lineTo(w, y0);
// tick
if (other_side_x) {
context.moveTo(tx, y0 - AXIS_ARROW_SIZE);
context.lineTo(tx, y0);
} else {
context.moveTo(tx, y0);
context.lineTo(tx, y0 + AXIS_ARROW_SIZE);
}
// arrow
context.moveTo(w-ARROW_OFFSET, y0);
context.lineTo(w-ARROW_OFFSET - AXIS_ARROW_SIZE, y0 - AXIS_ARROW_SIZE);
context.moveTo(w-ARROW_OFFSET, y0);
context.lineTo(w-ARROW_OFFSET - AXIS_ARROW_SIZE, y0 + AXIS_ARROW_SIZE);
context.stroke();
context.closePath();
// vertical axis
context.beginPath();
context.moveTo(x0, 0);
context.lineTo(x0, h);
// tick
if (other_side_y) {
context.moveTo(x0 + AXIS_ARROW_SIZE, ty);
context.lineTo(x0, ty);
} else {
context.moveTo(x0, ty);
context.lineTo(x0 - AXIS_ARROW_SIZE, ty);
}
// arrow
context.moveTo(x0, ARROW_OFFSET);
context.lineTo(x0 - AXIS_ARROW_SIZE, ARROW_OFFSET+AXIS_ARROW_SIZE);
context.moveTo(x0, ARROW_OFFSET);
context.lineTo(x0 + AXIS_ARROW_SIZE, ARROW_OFFSET+AXIS_ARROW_SIZE);
context.stroke();
context.closePath();
// axis+tick labels
context.fillStyle = "black";
// heat label
context.font = "100% sans-serif";
let lx, ly, ttx, tty;
if (other_side_x) {
context.textBaseline = "bottom";
context.textAlign = "right";
lx = w - AXIS_ARROW_SIZE*2;
ly = y0 - 4;
tty = y0 - AXIS_ARROW_SIZE;
} else {
context.textBaseline = "top";
context.textAlign = "right";
lx = w - AXIS_ARROW_SIZE*2;
ly = y0 + 4;
tty = y0 + AXIS_ARROW_SIZE;
}
context.fillText("heat", lx, ly);
context.textAlign = "center";
context.fillText(Math.round(tick_heat), tx, tty);
// humidity label
context.font = "100% sans-serif";
context.save();
context.rotate(-Math.PI/2);
if (other_side_y) {
context.textBaseline = "top";
context.textAlign = "right";
lx = -AXIS_ARROW_SIZE*2;
ly = x0 + 4;
} else {
context.textBaseline = "bottom";
context.textAlign = "right";
lx = -AXIS_ARROW_SIZE*2;
ly = x0 - 4;
}
context.fillText("humidity", lx, ly);
context.restore();
if (other_side_y) {
context.textAlign = "left";
ttx = x0 + AXIS_ARROW_SIZE-2;
} else {
context.textAlign = "right";
ttx = x0 - AXIS_ARROW_SIZE-2;
}
context.textBaseline = "middle";
context.fillText(Math.round(tick_humidity), ttx, ty);
}
// Cache diagram object for performance boost
let cachedVoronoiDiagram = null;
/* Given the list of biome points, returns a Voronoi diagram object
(which may have been cached). If recalculate is true, a recalculation is forced. */
function getVoronoiDiagram(points, recalculate) {
// Calculate bounding box, defaults to heat/humidity limits ...
let vbbox = {xl: limit_heat_min, xr: limit_heat_max, yb: limit_humidity_max, yt: limit_humidity_min};
const BUFFER_ZONE = 1;
// ... unless a point is out of bounds,
// then we increase the bounding box size
for (let p of points) {
if (p.heat < vbbox.xl) {
vbbox.xl = p.heat - BUFFER_ZONE;
} else if (p.heat > vbbox.xr) {
vbbox.xr = p.heat + BUFFER_ZONE;
}
if (p.humidity < vbbox.yt) {
vbbox.yt = p.humidity - BUFFER_ZONE;
} else if (p.humidity > vbbox.yb) {
vbbox.yb = p.humidity + BUFFER_ZONE;
}
}
if ((cachedVoronoiDiagram === null) || recalculate) {
let sites = []
for (let p of points) {
sites.push(biomePointToVoronoiPoint(p));
}
let voronoi = new Voronoi();
let diagram = null;
if (cachedVoronoiDiagram && recalculate) {
diagram = cachedVoronoiDiagram;
// This should improve performance
voronoi.recycle(diagram);
}
try {
diagram = voronoi.compute(sites, vbbox);
cachedVoronoiDiagram = diagram;
return diagram;
} catch {
cachedVoronoiDiagram = null;
return null;
}
} else {
return cachedVoronoiDiagram;
}
}
/* Returns the context object required to draw on the canvas */
function getDrawContext() {
let canvas = document.getElementById("voronoiCanvas");
if (canvas.getContext) {
return canvas.getContext("2d");
} else {
return null;
}
}
// Clear draw area
function clear(context) {
if (!context) {
context = getDrawContext();
if (!context) {
return false;
}
}
context.fillStyle = CLEAR_COLOR;
context.fillRect(-DRAW_OFFSET, -DRAW_OFFSET, voronoiCanvas.width+DRAW_OFFSET, voronoiCanvas.height+DRAW_OFFSET);
return true;
}
/* Returns all biome points except those whose Y limits fall out of the
given y value */
function getRenderedPoints(y) {
let points = [];
for (let p=0; p= point.min_y && y <= point.max_y) {
points.push(point);
}
}
return points;
}
/* Given a biome ID, returns the matching HTML element from the
biome list widget or null if none */
function getBiomeIDFromHTMLElement(elem) {
let strID = elem.id;
if (strID && strID.startsWith("biome_list_element_")) {
let slice = strID.slice(19);
if (slice) {
return +slice;
}
}
return null;
}
/* Returns both the ID of selected biome and the associated HTML element from
the biome list widget, or null if nothing is selected.
Result is returned in the form [id, htmlElement] or [null, null] */
function getSelectedBiomeIDAndElement() {
if (biomeSelector.selectedIndex === -1) {
return [null, null];
}
let elem = biomeSelector.options[biomeSelector.selectedIndex];
let biomeID = getBiomeIDFromHTMLElement(elem);
if (biomeID !== null) {
return [biomeID, elem];
}
return [null, null];
}
/* Draws the diagram on the voronoiCanvas.
Will (re-)calculate the Voronoi diagram if recalculate is true;
otherwise it may re-use a previous diagram for performance reasons. */
function draw(recalculate) {
let context = getDrawContext();
let w = voronoiCanvas.width;
let h = voronoiCanvas.height;
let y = getViewY();
// shorter function name (for "convert")
let conv = biomeCoordsToCanvasPixelCoords
if (!context) {
if (!voronoiCanvas.hidden) {
voronoiCanvas.hidden = true;
coordinateDisplay.hidden = true;
altitudeDisplay.hidden = true;
rangeDisplay.hidden = true;
configDiv.hidden = true;
errorMessage.innerText = "ERROR: Could not get the canvas context which means this tool won't work for you. Maybe your browser does not support the HTML canvas element properly.";
}
return false;
}
clear(context);
let showDiagramMessage = function(context, text) {
context.textAlign = "center";
context.fillStyle = "black";
context.textBaseline = "middle";
if (voronoiCanvas.width < 300) {
context.font = "100% sans-serif";
} else if (voronoiCanvas.width < 450) {
context.font = "150% sans-serif";
} else {
context.font = "200% sans-serif";
}
context.fillText(text, voronoiCanvas.width/2, voronoiCanvas.height/2);
updateAltitudeText();
}
// Render a special message if the value range is tiny
if ((limit_heat_max - limit_heat_min < 0.01) || (limit_humidity_max - limit_humidity_min < 0.01)) {
showDiagramMessage(context, "Value range is too small.");
drawError = true;
putResizeCorner(context);
return true;
}
let points = getRenderedPoints(y);
// Render a special message if there are no biomes
if (points.length === 0) {
if (biomePoints.length === 0) {
showDiagramMessage(context, "No biomes.");
} else {
showDiagramMessage(context, "No biomes in this Y altitude.");
}
drawError = true;
putResizeCorner(context);
return true;
}
updateAltitudeText();
let voronoiError = function() {
showDiagramMessage(context, "Error in Javascript-Voronoi!");
drawError = true;
putResizeCorner(context);
}
let diagram = getVoronoiDiagram(points, recalculate);
if (!diagram) {
voronoiError();
drawError = true;
return true;
}
drawError = false;
let createHalfedgesPath = function(context, cell) {
context.beginPath();
for (let h=0; h 0) {
if (showGrid) {
putGrid(context);
}
if (showAxes) {
putAxes(context);
}
}
// Render Voronoi cell edges
context.lineWidth = 2.5;
for (let e=0; e";
}
/* Update the text that shows the biome coordinates
of the cursor when it's on the diagram */
function updateCoordinateDisplay(pixelX, pixelY) {
if (pixelX === null || pixelY === null) {
coordinateDisplay.innerHtml = " ";
return;
}
// show coordinates
let [heat, humidity] = canvasPixelCoordsToBiomeCoords(pixelX, pixelY);
if (!drawError) {
let html = "cursor coordinates: heat="+heat+"; humidity="+humidity+"";
coordinateDisplay.innerHTML = html;
} else {
coordinateDisplay.innerHTML = " ";
}
}
/* Updates and changes the cursor type on the diagram
canvas depending on whether we can select, drag or do nothing
at the pointed position */
function updateCanvasCursorStatus(x, y) {
// Show resize cursor at the bottom right corner
if (resizing || (x > voronoiCanvas.width - RESIZE_CORNER && y > voronoiCanvas.height - RESIZE_CORNER)) {
voronoiCanvas.style.cursor = "nwse-resize";
return
}
if (drawError || !showPoints) {
// a special message is shown; use auto cursor
voronoiCanvas.style.cursor = "auto";
return
}
let nearest = getNearestPointFromCanvasPos(x, y, POINT_SELECT_DISTANCE);
if (nearest !== null) {
let [id, elem] = getSelectedBiomeIDAndElement();
if (id !== null && nearest.id === id) {
// This cursor indicates we can grab the point
voronoiCanvas.style.cursor = "grab";
} else {
// This cursor indicates we can select the point
voronoiCanvas.style.cursor = "crosshair";
}
} else {
// Default cursor when a click doesn't to anything
voronoiCanvas.style.cursor = "auto";
}
}
/* Initializes checkbox variables of the view settings */
function checkboxVarsInit() {
showNames = inputCheckboxNames.checked;
showPoints = inputCheckboxPoints.checked;
showCellColors = inputCheckboxCellColors.checked;
showGrid = inputCheckboxGrid.checked;
showAxes = inputCheckboxAxes.checked;
}
/* Collapses/Expands a config section */
function toggleConfigSectionDisplay(headerLink, container) {
if (container.style.display !== "none") {
headerLink.innerText = "▶";
container.style.display = "none";
} else {
headerLink.innerText = "▼";
container.style.display = "block";
}
}
/* Unhide the main content. Used to disable the noscript
handling because the website starts with the main content
container hidden so the noscript version of the page
isn't cluttered. */
function unhideContent() {
mainContentContainer.hidden = false;
/* Also hide the container holding the noscript error
message to avoid spacing issues */
noscriptContainer.hidden = true;
}
function initBiomeColorSelectors() {
for (let c=0; c= 1) {
dragDropState = 2;
} else if ((dragDropState === 0 || dragDropState === 1) && dist > ONE_CLICK_DRAG_DROP_DISTANCE) {
dragDropState = 2;
}
}
// drag-n-drop
if (dragDropState === 2) {
updatePointWhenDragged(dragDropPointID);
}
updateCoordinateDisplay(event.offsetX, event.offsetY);
updateCanvasCursorStatus(event.offsetX, event.offsetY);
canvas_cursor_x = event.offsetX;
canvas_cursor_y = event.offsetY;
draw(false);
}
voronoiCanvas.onmouseenter = function(event) {
updateCoordinateDisplay(event.offsetX, event.offsetY);
updateCanvasCursorStatus(event.offsetX, event.offsetY);
canvas_cursor_x = event.offsetX;
canvas_cursor_y = event.offsetY;
draw(false);
}
voronoiCanvas.onmousedown = function(event) {
// select point by clicking.
// initiate drag-n-drop if already selected.
mouseIsDown = true;
// Resizing the canvas
if (event.offsetX > voronoiCanvas.width - RESIZE_CORNER && event.offsetY > voronoiCanvas.height - RESIZE_CORNER) {
resizing_start_pos_x = event.offsetX;
resizing_start_pos_y = event.offsetY;
resizing_start_size_x = +this.width;
resizing_start_size_y = +this.height;
resizing = true;
return;
}
if (drawError || !showPoints) {
// Points need to be shown for drag-n-drop to work
return;
}
let nearest = getNearestPointFromCanvasPos(event.offsetX, event.offsetY, POINT_SELECT_DISTANCE);
if (nearest !== null) {
let success, alreadySelected
[success, alreadySelected] = selectPoint(nearest);
dragDropPointID = nearest.id;
if (success) {
let [x, y] = biomeCoordsToCanvasPixelCoords(nearest.heat, nearest.humidity);
dragDropStartPos = { x: x, y: y };
}
if (alreadySelected) {
dragDropState = 1;
}
updateCanvasCursorStatus(event.offsetX, event.offsetY);
}
}
voronoiCanvas.onmouseup = function(event) {
// end drag-n-drop
if (dragDropState === 2) {
updatePointWhenDragged(dragDropPointID);
}
mouseIsDown = false;
dragDropStartPos = null;
dragDropPointID = null;
dragDropState = 0;
}
voronoiCanvas.onmouseleave = function() {
// end drag-n-drop
mouseIsDown = false;
dragDropStartPos = null;
dragDropPointID = null;
dragDropState = 0;
canvas_cursor_x = null;
canvas_cursor_y = null;
coordinateDisplay.innerHTML = " ";
draw(false);
}
/* Biome list events */
biomeSelector.onchange = function() {
draw(false);
if (biomeSelector.selectedIndex !== -1) {
let selected = biomeSelector.options[biomeSelector.selectedIndex];
let point = biomePoints[biomeSelector.selectedIndex];
inputHeat.value = point.heat;
inputHumidity.value = point.humidity;
inputMinY.value = point.min_y;
inputMaxY.value = point.max_y;
}
updateWidgetStates();
}
addBiomeButton.onclick = function() {
let he = Math.round(limit_heat_min + Math.random() * (limit_heat_max - limit_heat_min));
let hu = Math.round(limit_humidity_min + Math.random() * (limit_humidity_max - limit_humidity_min));
let newPoint = {
id: lastBiomeID,
name: generateBiomeName(lastBiomeID),
heat: he,
humidity: hu,
min_y: MIN_Y_DEFAULT,
max_y: MAX_Y_DEFAULT,
colorIndex: lastBiomeID % CELL_COLORS.length,
};
biomePoints.push(newPoint);
let num = biomePoints.length
let newElem = document.createElement("option");
newElem.id = "biome_list_element_" + lastBiomeID;
newElem.value = "" + num;
let newElemText = document.createTextNode(newPoint.name);
newElem.append(newElemText);
biomeSelector.append(newElem);
newElem.selected = "selected";
draw(true);
updateWidgetStates();
lastBiomeID++;
}
removeBiomeButton.onclick = function() {
if (biomeSelector.selectedOptions.length === 0) {
return;
}
let firstIndex = null;
for (let o=0; o 0) {
let newIndex = firstIndex-1;
if (newIndex < 0) {
newIndex = 0;
}
biomeSelector.options[newIndex].selected = "selected";
}
draw(true);
updateWidgetStates();
}
/* Biome editing widgets events */
inputHeat.oninput = function() {
onChangeBiomeValueWidget("heat", +this.value);
}
inputHumidity.oninput = function() {
onChangeBiomeValueWidget("humidity", +this.value);
}
inputMinY.oninput = function() {
onChangeBiomeValueWidget("min_y", +this.value);
}
inputMaxY.oninput = function() {
onChangeBiomeValueWidget("max_y", +this.value);
}
inputBiomeName.oninput = function() {
onChangeBiomeValueWidget("name", this.value);
}
/* Diagram view settings events */
inputViewY.oninput = function() {
draw(true);
updateAltitudeText();
}
inputCheckboxNames.onchange = function() {
showNames = this.checked;
draw(false);
}
inputCheckboxPoints.onchange = function() {
showPoints = this.checked;
draw(false);
}
inputCheckboxCellColors.onchange = function() {
showCellColors = this.checked;
draw(false);
}
inputCheckboxGrid.onchange = function() {
showGrid = this.checked;
draw(false);
}
inputCheckboxAxes.onchange = function() {
showAxes = this.checked;
draw(false);
}
/* Noise parameters events */
inputNoiseHeatScale.oninput = function() {
noises.heat.scale = +this.value;
clear();
updateAreaVars();
draw(true);
}
inputNoiseHeatOffset.oninput = function() {
noises.heat.offset = +this.value;
clear();
updateAreaVars();
draw(true);
}
inputNoiseHeatPersistence.oninput = function() {
noises.heat.persistence = +this.value;
clear();
updateAreaVars();
draw(true);
}
inputNoiseHeatOctaves.oninput = function() {
noises.heat.octaves = +this.value;
clear();
updateAreaVars();
draw(true);
}
inputNoiseHumidityScale.oninput = function() {
noises.humidity.scale = +this.value;
clear();
updateAreaVars();
draw(true);
}
inputNoiseHumidityOffset.oninput = function() {
noises.humidity.offset = +this.value;
clear();
updateAreaVars();
draw(true);
}
inputNoiseHumidityPersistence.oninput = function() {
noises.humidity.persistence = +this.value;
clear();
updateAreaVars();
draw(true);
}
inputNoiseHumidityOctaves.oninput = function() {
noises.humidity.octaves = +this.value;
clear();
updateAreaVars();
draw(true);
}
inputNoiseReset.onclick = function() {
noises.heat.offset = NOISE_OFFSET_DEFAULT;
noises.heat.scale = NOISE_SCALE_DEFAULT;
noises.heat.octaves = NOISE_OCTAVES_DEFAULT;
noises.heat.persistence = NOISE_PERSISTENCE_DEFAULT;
noises.heat.absvalue = NOISE_ABSVALUE_DEFAULT;
inputNoiseHeatOffset.value = noises.heat.offset;
inputNoiseHeatScale.value = noises.heat.scale;
inputNoiseHeatOctaves.value = noises.heat.octaves;
inputNoiseHeatPersistence.value = noises.heat.persistence;
noises.humidity.offset = NOISE_OFFSET_DEFAULT;
noises.humidity.scale = NOISE_SCALE_DEFAULT;
noises.humidity.octaves = NOISE_OCTAVES_DEFAULT;
noises.humidity.persistence = NOISE_PERSISTENCE_DEFAULT;
noises.humidity.absvalue = NOISE_ABSVALUE_DEFAULT;
inputNoiseHumidityOffset.value = noises.humidity.offset;
inputNoiseHumidityScale.value = noises.humidity.scale;
inputNoiseHumidityOctaves.value = noises.humidity.octaves;
inputNoiseHumidityPersistence.value = noises.humidity.persistence;
clear();
updateAreaVars();
draw(true);
}
/* Export events */
inputExportLua.onclick = function() {
let str = "";
for (let b=0; b