Embed
The Embed Visualisation displays your data in a visualisation of your own design, from a classic chart to an interactive artwork. A custom script determines what is displayed and how the visualisation responds to the user, all while the visualisation remains integrated in nodegoat. The script can be written by hand, or generated by any AI assistant from a single sentence that describes the visualisation.
Develop and embed a custom visualisation script. Provide a URL (https://... or a path starting with /) to a script, or the script code itself.
The script runs in its own environment and manages the visualisation through the NODEGOAT object. It receives the data as a network:
- Nodes: for the Objects and the referenced Objects that are in the data.
- Edges: for the connections made by the Object Descriptions and Sub-Object Descriptions that reference an Object.
- Statements: for the Sub-Objects with their dates and locations.
nodegoat keeps control of zoom, pan, the timeline, and the legends and panels, and passes their state on to the script. In response to the user, the script can pass nodes and statements back to nodegoat to show the data behind them. As the script only uses the data it receives, a script can be reused for any visualisation.
A script has the option to place its visualisation in the world using nodegoat's geographic map, with the levels and layers that are set in the Geographical Visual Settings and the active Frame.
Create a Visualisation
To implement and activate the Embed Visualisation:
- Open the Visual Settings and go to 'Embed'. Click the context to copy it: the context contains the latest description of nodegoat's Embed Visualisation API and a reference script.
- Paste the context in any AI assistant, and describe your visualisation.
- Paste the returned script in the field 'Script', and open the Embed Visualisation to view the result.
Examples
The following are some examples to demonstrate a range of functionalities.
A three-ring pie to show a breakdown of the active selection: values and referenced nodes, their Descriptions from a Type, their Type itself. Have the choice for independent or nested rings, an optional filter to one description, maximum items per ring. Use material styling.
// Rings: the active selection as a pie of three rings: values and referenced nodes, their Descriptions, their Types. Independent or nested rings, an optional filter to one description, maximum items per ring.
const FONT = 'Roboto, "Helvetica Neue", Arial, sans-serif';
const TEXT_PRIMARY = 'rgba(0, 0, 0, 0.87)';
const TEXT_SECONDARY = 'rgba(0, 0, 0, 0.6)';
const TEXT_LIGHT = '#ffffff';
const KEY_OTHER = '~other';
const ANGLE_START = -Math.PI / 2; // 12 o'clock, clockwise
const ANGLE_FULL = Math.PI * 2;
const ANIMATION = 450; // ms, Material standard easing
const REVIEW_MAX = 500; // Ids passed to a review at most
const RINGS = [ // Radii as part of the pie's radius. Inner rings darker, outer rings lighter
{name: 'Values & references', noun: 'items', field: 'v', inner: 0.36, outer: 0.62, shade: 2, offset: 0, font: 13, weight: '500 '},
{name: 'Descriptions', noun: 'Descriptions', field: 'd', inner: 0.635, outer: 0.81, shade: 1, offset: 5, font: 12, weight: ''},
{name: 'Types', noun: 'Types', field: 't', inner: 0.825, outer: 1, shade: 0, offset: 10, font: 12, weight: ''}
];
const COLORS_OTHER = ['#9E9E9E', '#BDBDBD', '#E0E0E0']; // Material Grey 500, 400, 300
const PALETTE = [ // Material Design hues as [300, 500, 700], ordered so neighbours stay apart
['#64B5F6', '#2196F3', '#1976D2'], // Blue
['#F06292', '#E91E63', '#C2185B'], // Pink
['#4DB6AC', '#009688', '#00796B'], // Teal
['#FFD54F', '#FFC107', '#FFA000'], // Amber
['#9575CD', '#673AB7', '#512DA8'], // Deep Purple
['#AED581', '#8BC34A', '#689F38'], // Light Green
['#FF8A65', '#FF5722', '#E64A19'], // Deep Orange
['#4DD0E1', '#00BCD4', '#0097A7'], // Cyan
['#7986CB', '#3F51B5', '#303F9F'], // Indigo
['#DCE775', '#CDDC39', '#AFB42B'], // Lime
['#E57373', '#F44336', '#D32F2F'], // Red
['#81C784', '#4CAF50', '#388E3C'], // Green
['#BA68C8', '#9C27B0', '#7B1FA2'], // Purple
['#FFB74D', '#FF9800', '#F57C00'], // Orange
['#4FC3F7', '#03A9F4', '#0288D1'], // Light Blue
['#A1887F', '#795548', '#5D4037'], // Brown
['#90A4AE', '#607D8B', '#455A64'] // Blue Grey
];
const LEGEND_WIDTH = 280, LEGEND_HEADER = 32, LEGEND_ROW = 24, LEGEND_PADDING = 8;
const elm_canvas = document.body.appendChild(document.createElement('canvas'));
elm_canvas.style.cssText = 'position: absolute; left: 0px; top: 0px;';
const map_nodes = new Map();
const map_statements = new Map();
const map_labels = new Map();
const map_color_index = new Map();
const map_type_colors = new Map();
const map_node_items = new Map();
const map_statement_items = new Map();
const arr_edge_items = [];
const arr_descriptions = {value: [], ref: []};
const map_text_colors = new Map();
let context_color = null;
let map_triples = new Map();
let num_total = 0, num_values = 0, num_refs = 0;
let arr_rings = [[], [], []];
let str_built = 'independent';
let num_anim_start = 0;
let is_queued = false;
let str_summary = '';
let str_filter_values = 'all';
let str_filter_refs = 'all';
let str_layout = 'independent';
let num_limit = 8;
let arr_focus = null;
const arr_legend = {size: null, rows: [], height: LEGEND_PADDING * 2 + RINGS.length * (LEGEND_HEADER + LEGEND_ROW)};
NODEGOAT.setData({values: 'plain'}); // Values as nodegoat displays them, so equal values count together
NODEGOAT.setEnvironment({transition: true}); // nodegoat animates zooms on the script's own map
const getDescriptionOptions = function(str_kind) {
const arr_options = (str_kind == 'value' ?
[{value: 'all', label: 'All value descriptions'}, {value: 'none', label: 'No values'}] :
[{value: 'all', label: 'All relationship descriptions'}, {value: 'none', label: 'No relationships'}]
);
for (const arr_description of arr_descriptions[str_kind]) {
arr_options.push({value: arr_description.key, label: arr_description.name+(arr_description.context ? ' — '+arr_description.context : '')});
}
return arr_options;
};
const getPanelRings = function() {
return [
{type: 'text', id: 'summary'},
{type: 'select', id: 'filter_values', title: 'Count values of', value: str_filter_values, options: getDescriptionOptions('value')},
{type: 'select', id: 'filter_refs', title: 'Count references of', value: str_filter_refs, options: getDescriptionOptions('ref')},
{type: 'select', id: 'layout', title: 'Layout', value: str_layout, options: [{value: 'independent', label: 'Independent rings'}, {value: 'nested', label: 'Nested rings'}]},
{type: 'range', id: 'limit', min: 3, max: 20, step: 1, value: num_limit, title: 'Items per ring'},
{type: 'button', id: 'fit', icon: 'tiles-few', title: 'Fit'}
];
};
const getPanelLegend = function() {
return [{type: 'image', id: 'legend', width: LEGEND_WIDTH, height: arr_legend.height, cursor: 'pointer'}];
};
NODEGOAT.setControls({
keys: ['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', '+', '-'],
panels: {
rings: getPanelRings(),
legend: getPanelLegend()
}
});
NODEGOAT.on('open', function() {
const map_counts = new Map();
const map_names = new Map();
const addCount = function(str_key) {
map_counts.set(str_key, (map_counts.get(str_key) || 0) + 1);
};
const setType = function(arr_node) {
const t = 't:'+arr_node.model_id;
if (!map_labels.has(t)) {
map_labels.set(t, NODEGOAT.getNodeModelName(arr_node) || 'Unknown Type');
map_type_colors.set(t, NODEGOAT.getNodeModelColor(arr_node));
}
return t;
};
const setDescription = function(d, str_name, str_context, str_kind) {
if (map_labels.has(d)) {
return;
}
str_name = (str_name || 'Unnamed description');
map_labels.set(d, str_name);
arr_descriptions[str_kind].push({key: d, name: str_name, context: str_context});
if (!map_names.has(str_name)) {
map_names.set(str_name, []);
}
map_names.get(str_name).push(d);
};
const addValues = function(arr_values, str_prefix, t, getName, str_context, arr_items) {
for (const model_id in (arr_values || {})) {
const d = str_prefix+model_id;
setDescription(d, getName(model_id), str_context, 'value');
for (const value of arr_values[model_id]) {
const str_value = (value === null || value === undefined ? '' : String(value)).trim();
if (!str_value) {
continue;
}
const v = 'v:'+str_value;
if (!map_labels.has(v)) {
map_labels.set(v, str_value);
}
arr_items.push([v, d, t]);
addCount(v);
addCount(d);
addCount(t);
}
}
};
for (const arr_node of NODEGOAT.DATA.nodes) {
map_nodes.set(arr_node.id, arr_node);
}
for (const arr_statement of NODEGOAT.DATA.statements) {
map_statements.set(arr_statement.id, arr_statement);
}
for (const arr_node of NODEGOAT.DATA.nodes) {
const t = setType(arr_node);
const arr_items = [];
addValues(arr_node.values, 'n:', t, NODEGOAT.getNodeValueModelName, map_labels.get(t), arr_items);
map_node_items.set(arr_node.id, arr_items);
}
const getStatementContext = function(arr_statement, t) {
const str_name = NODEGOAT.getStatementModelName(arr_statement);
return (str_name && str_name != '~' ? str_name : map_labels.get(t));
};
for (const arr_statement of NODEGOAT.DATA.statements) {
const arr_node = map_nodes.get(arr_statement.node_id);
if (!arr_node) {
continue;
}
const t = setType(arr_node);
const arr_items = [];
addValues(arr_statement.values, 's:', t, NODEGOAT.getStatementValueModelName, getStatementContext(arr_statement, t), arr_items);
map_statement_items.set(arr_statement.id, arr_items);
}
NODEGOAT.DATA.edges.forEach(function(arr_edge, i) {
const arr_source = map_nodes.get(arr_edge.source);
const arr_target = map_nodes.get(arr_edge.target);
if (!arr_source || !arr_target) {
arr_edge_items[i] = null;
return;
}
const t = setType(arr_source);
const arr_statement = (arr_edge.statement !== null ? map_statements.get(arr_edge.statement) : null);
const d = (arr_edge.statement !== null ? 'es:' : 'e:')+arr_edge.model_id;
setDescription(d, NODEGOAT.getEdgeModelName(arr_edge), (arr_statement ? getStatementContext(arr_statement, t) : map_labels.get(t)), 'ref');
const v = 'r:'+arr_target.id;
if (!map_labels.has(v)) {
map_labels.set(v, '→ '+(arr_target.name || 'Unnamed'));
}
arr_edge_items[i] = [v, d, t];
addCount(v);
addCount(d);
addCount(t);
});
const arr_groups = [[], [], []];
for (const [str_key, num_count] of map_counts) {
arr_groups[getRingOfKey(str_key)].push([str_key, num_count]);
}
for (const arr_group of arr_groups) {
arr_group.sort((arr_a, arr_b) => arr_b[1] - arr_a[1]).forEach((arr_entry, i) => map_color_index.set(arr_entry[0], i));
}
for (const str_kind in arr_descriptions) {
for (const arr_description of arr_descriptions[str_kind]) {
if (map_names.get(arr_description.name).length > 1 && arr_description.context) {
map_labels.set(arr_description.key, arr_description.name+' · '+arr_description.context);
}
}
arr_descriptions[str_kind].sort((arr_a, arr_b) => arr_a.name.localeCompare(arr_b.name) || String(arr_a.context).localeCompare(String(arr_b.context)));
}
NODEGOAT.setControls({panels: {rings: getPanelRings()}});
});
NODEGOAT.on('view', function() {
queue();
});
NODEGOAT.on('draw', function() {
aggregate();
build();
});
NODEGOAT.on('pointer', function(arr_pointer) {
if (!NODEGOAT.ACTIVE || !NODEGOAT.VIEW) {
return;
}
const arr_seg = (arr_pointer.event == 'leave' ? null : getHit(arr_pointer.x, arr_pointer.y));
if (arr_pointer.event == 'click') {
if (arr_seg) {
setReview(arr_seg.triples);
}
return;
}
if (arr_pointer.event == 'up') {
return;
}
setFocusSegment(arr_seg);
});
NODEGOAT.on('key', function(arr_key) {
if (arr_key.event != 'down' || !NODEGOAT.VIEW || arr_key.ctrl || arr_key.alt || arr_key.meta) {
return;
}
const num_step = 50;
if (arr_key.key == 'ArrowLeft' || arr_key.key == 'ArrowRight') {
NODEGOAT.setView({offset_x: NODEGOAT.VIEW.offset_x + (arr_key.key == 'ArrowLeft' ? num_step : -num_step)});
} else if (arr_key.key == 'ArrowUp' || arr_key.key == 'ArrowDown') {
NODEGOAT.setView({offset_y: NODEGOAT.VIEW.offset_y + (arr_key.key == 'ArrowUp' ? num_step : -num_step)});
} else if (arr_key.key == '+' || arr_key.key == '-') {
NODEGOAT.setView({level: NODEGOAT.VIEW.level + (arr_key.key == '+' ? 1 : -1)});
}
});
NODEGOAT.on('control', function(arr_control) {
if (arr_control.id == 'legend') {
if (arr_control.event == 'resize') {
arr_legend.size = arr_control.value;
drawLegendControl();
return;
}
const arr_row = (arr_control.event == 'leave' ? null : getLegendRow(arr_control.value.y));
if (arr_control.event == 'click') {
if (arr_row) {
setReview(getRingKeyTriples(arr_row.ring, arr_row.key));
}
return;
}
const has_changed = (arr_row ?
(!arr_focus || arr_focus.seg || arr_focus.ring !== arr_row.ring || arr_focus.key !== arr_row.key) :
(arr_focus && !arr_focus.seg)
);
if (has_changed) {
arr_focus = (arr_row ? {ring: arr_row.ring, key: arr_row.key} : null);
NODEGOAT.setControlContent('legend', 'legend', undefined, (arr_row ? arr_row.label+' — '+formatNumber(arr_row.count)+' ('+formatPercent(arr_row.count / num_total)+')' : null));
drawLegendControl();
queue();
}
return;
}
if (arr_control.id == 'filter_values' && arr_control.event == 'change') {
str_filter_values = arr_control.value;
if (str_filter_values != 'all' && str_filter_values != 'none') {
str_filter_refs = 'none';
}
setFilters();
} else if (arr_control.id == 'filter_refs' && arr_control.event == 'change') {
str_filter_refs = arr_control.value;
if (str_filter_refs != 'all' && str_filter_refs != 'none') {
str_filter_values = 'none';
}
setFilters();
} else if (arr_control.id == 'layout' && arr_control.event == 'change') {
str_layout = arr_control.value;
build();
} else if (arr_control.id == 'limit') {
const num = Number(arr_control.value);
if (num !== num_limit) {
num_limit = num;
build();
}
} else if (arr_control.id == 'fit') {
NODEGOAT.setView({level: 0, offset_x: 0, offset_y: 0});
}
});
NODEGOAT.on('capture', function(arr_capture) {
const elm_capture = document.createElement('canvas');
elm_capture.width = arr_capture.width;
elm_capture.height = arr_capture.height;
const context = elm_capture.getContext('2d');
context.scale(arr_capture.width / NODEGOAT.VIEW.width, arr_capture.height / NODEGOAT.VIEW.height);
const arr_centre = getCentre();
drawChart(context, arr_centre.x, arr_centre.y, arr_centre.radius);
context.save();
context.translate(16, 16);
context.shadowColor = 'rgba(0, 0, 0, 0.2)';
context.shadowBlur = 8 * context.getTransform().a;
context.shadowOffsetY = 2 * context.getTransform().a;
context.fillStyle = '#ffffff';
fillRoundRect(context, 0, 0, LEGEND_WIDTH, arr_legend.height, 8);
context.restore();
context.save();
context.translate(16, 16);
drawLegend(context, LEGEND_WIDTH, false);
context.restore();
return elm_capture;
});
const getRingOfKey = function(str_key) {
if (str_key[0] == 'v' || str_key[0] == 'r') {
return 0;
}
return (str_key[0] == 't' ? 2 : 1);
};
const setFilters = function() {
NODEGOAT.setControls({panels: {rings: getPanelRings()}});
if (NODEGOAT.ACTIVE) {
aggregate();
build();
}
};
const aggregate = function() {
map_triples = new Map();
num_values = 0;
num_refs = 0;
if (!NODEGOAT.ACTIVE) {
num_total = 0;
return;
}
const add = function(arr_item, node_id, statement_id, target_id) {
const is_ref = (arr_item[1][0] == 'e');
const str_filter = (is_ref ? str_filter_refs : str_filter_values);
if (str_filter == 'none' || (str_filter != 'all' && str_filter != arr_item[1])) {
return;
}
const str_key = arr_item[0]+'\u0000'+arr_item[1]+'\u0000'+arr_item[2];
let arr_triple = map_triples.get(str_key);
if (!arr_triple) {
arr_triple = {v: arr_item[0], d: arr_item[1], t: arr_item[2], count: 0, nodes: new Set(), statements: new Set()};
map_triples.set(str_key, arr_triple);
}
arr_triple.count++;
if (target_id) {
arr_triple.nodes.add(target_id);
}
arr_triple.nodes.add(node_id);
if (statement_id) {
arr_triple.statements.add(statement_id);
}
if (is_ref) {
num_refs++;
} else {
num_values++;
}
};
for (const node_id of NODEGOAT.ACTIVE.nodes) {
for (const arr_item of (map_node_items.get(node_id) || [])) {
add(arr_item, node_id, null, null);
}
}
for (const statement_id of NODEGOAT.ACTIVE.statements) {
const arr_items = map_statement_items.get(statement_id);
if (!arr_items) {
continue;
}
const node_id = map_statements.get(statement_id).node_id;
for (const arr_item of arr_items) {
add(arr_item, node_id, statement_id, null);
}
}
for (const i of NODEGOAT.ACTIVE.edges) {
const arr_item = arr_edge_items[i];
if (arr_item) {
const arr_edge = NODEGOAT.DATA.edges[i];
add(arr_item, arr_edge.source, arr_edge.statement, arr_edge.target);
}
}
num_total = num_values + num_refs;
};
const getKept = function(map_count) {
const arr_keys = Array.from(map_count.keys()).sort((a, b) => map_count.get(b) - map_count.get(a) || (map_color_index.get(a) || 0) - (map_color_index.get(b) || 0));
return new Set(arr_keys.length <= num_limit + 1 ? arr_keys : arr_keys.slice(0, num_limit));
};
const build = function() {
const map_previous = new Map();
for (const arr_segs of arr_rings) {
for (const arr_seg of arr_segs) {
map_previous.set(arr_seg.id, arr_seg);
}
}
const is_nested = (str_layout == 'nested');
const arr_triples = Array.from(map_triples.values());
const arr_root = {path: [], label: '', triples: arr_triples, count: num_total, a0: ANGLE_START, a1: ANGLE_START + ANGLE_FULL};
let arr_parents = [arr_root];
str_built = str_layout;
arr_rings = [];
RINGS.forEach(function(arr_ring, i_ring) {
const str_field = arr_ring.field;
const map_count = new Map();
for (const arr_triple of arr_triples) {
map_count.set(arr_triple[str_field], (map_count.get(arr_triple[str_field]) || 0) + arr_triple.count);
}
const set_kept = getKept(map_count);
const arr_segs = [];
for (const arr_parent of (is_nested ? arr_parents : [arr_root])) {
if (!arr_parent.count) {
continue;
}
const map_groups = new Map();
for (const arr_triple of arr_parent.triples) {
const str_key = (set_kept.has(arr_triple[str_field]) ? arr_triple[str_field] : KEY_OTHER);
let arr_group = map_groups.get(str_key);
if (!arr_group) {
arr_group = {key: str_key, count: 0, triples: [], keys: new Set()};
map_groups.set(str_key, arr_group);
}
arr_group.count += arr_triple.count;
arr_group.triples.push(arr_triple);
arr_group.keys.add(arr_triple[str_field]);
}
const arr_groups = Array.from(map_groups.values()).sort((a, b) => (a.key == KEY_OTHER) - (b.key == KEY_OTHER) || b.count - a.count || (map_color_index.get(a.key) || 0) - (map_color_index.get(b.key) || 0));
const num_span = arr_parent.a1 - arr_parent.a0;
let num_angle = arr_parent.a0;
arr_groups.forEach(function(arr_group, i) {
const num_end = (i == arr_groups.length - 1 ? arr_parent.a1 : num_angle + num_span * arr_group.count / arr_parent.count);
const arr_path = (is_nested ? arr_parent.path.concat(arr_group.key) : [arr_group.key]);
arr_segs.push({
id: i_ring+':'+arr_path.join('\u0000'),
ring: i_ring,
key: arr_group.key,
path: arr_path,
label: (arr_group.key == KEY_OTHER ? 'Other' : map_labels.get(arr_group.key)),
color: getColor(i_ring, arr_group.key),
count: arr_group.count,
triples: arr_group.triples,
merged: arr_group.keys.size,
parent: (is_nested && arr_parent !== arr_root ? arr_parent : null),
a0: num_angle,
a1: num_end
});
num_angle = num_end;
});
}
arr_rings.push(arr_segs);
arr_parents = arr_segs;
});
for (const arr_segs of arr_rings) {
for (const arr_seg of arr_segs) {
const arr_previous = map_previous.get(arr_seg.id);
arr_seg.f0 = arr_seg.c0 = (arr_previous ? arr_previous.c0 : arr_seg.a0);
arr_seg.f1 = arr_seg.c1 = (arr_previous ? arr_previous.c1 : arr_seg.a0);
}
}
num_anim_start = performance.now();
if (arr_focus && arr_focus.seg) {
const str_id = arr_focus.seg.id;
const arr_seg = arr_rings[arr_focus.seg.ring].find(arr_seg => arr_seg.id == str_id);
arr_focus = (arr_seg ? {seg: arr_seg} : null);
if (!arr_seg) {
NODEGOAT.setHover({});
}
}
setSummary();
setLegend();
queue();
};
const setSummary = function() {
let str = formatNumber(num_total)+' counted';
if (num_total) {
str += ': '+formatNumber(num_values)+' value'+(num_values == 1 ? '' : 's')+', '+formatNumber(num_refs)+' reference'+(num_refs == 1 ? '' : 's');
}
if (str !== str_summary) {
str_summary = str;
NODEGOAT.setControlContent('rings', 'summary', str_summary);
}
};
const getRingKeyTriples = function(i_ring, str_key) {
const arr_triples = [];
for (const arr_seg of arr_rings[i_ring]) {
if (arr_seg.key === str_key) {
arr_triples.push(...arr_seg.triples);
}
}
return arr_triples;
};
const setReview = function(arr_triples) {
const set_nodes = new Set(), set_statements = new Set();
for (const arr_triple of arr_triples) {
for (const node_id of arr_triple.nodes) {
if (set_nodes.size >= REVIEW_MAX) {
break;
}
set_nodes.add(node_id);
}
for (const statement_id of arr_triple.statements) {
if (set_statements.size >= REVIEW_MAX) {
break;
}
set_statements.add(statement_id);
}
}
if (set_nodes.size || set_statements.size) {
NODEGOAT.setReview({node_ids: Array.from(set_nodes), statement_ids: Array.from(set_statements)});
}
};
const setFocusSegment = function(arr_seg) {
const arr_current = (arr_focus && arr_focus.seg ? arr_focus.seg : null);
if (arr_seg === arr_current) {
return;
}
if (!arr_seg && arr_focus && !arr_focus.seg) {
return;
}
arr_focus = (arr_seg ? {seg: arr_seg} : null);
NODEGOAT.setHover(arr_seg ? {title: getSegmentTitle(arr_seg), cursor: 'pointer'} : {});
drawLegendControl();
queue();
};
const isFocus = function(arr_seg) {
if (!arr_focus) {
return false;
}
const is_nested = (str_built == 'nested');
if (arr_focus.seg) {
if (!is_nested) {
return (arr_seg === arr_focus.seg);
}
const arr_path = arr_focus.seg.path;
const num_length = Math.min(arr_path.length, arr_seg.path.length);
for (let i = 0; i < num_length; i++) {
if (arr_path[i] !== arr_seg.path[i]) {
return false;
}
}
return true;
}
if (is_nested) {
return (arr_seg.ring >= arr_focus.ring && arr_seg.path[arr_focus.ring] === arr_focus.key);
}
return (arr_seg.ring == arr_focus.ring && arr_seg.key === arr_focus.key);
};
const isFocusRow = function(arr_row) {
if (!arr_focus) {
return false;
}
if (arr_focus.seg) {
return (arr_focus.seg.ring == arr_row.ring && arr_focus.seg.key === arr_row.key);
}
return (arr_focus.ring == arr_row.ring && arr_focus.key === arr_row.key);
};
const getSegmentLabel = function(arr_seg) {
return (arr_seg.key == KEY_OTHER ? 'Other ('+formatNumber(arr_seg.merged)+' '+RINGS[arr_seg.ring].noun+')' : arr_seg.label);
};
const getSegmentTitle = function(arr_seg) {
let str_title = '';
if (arr_seg.parent) {
const arr_labels = [];
for (let arr_parent = arr_seg.parent; arr_parent; arr_parent = arr_parent.parent) {
arr_labels.unshift(arr_parent.label);
}
str_title = arr_labels.join(' › ')+' › '+getSegmentLabel(arr_seg);
} else {
str_title = RINGS[arr_seg.ring].name+': '+getSegmentLabel(arr_seg);
}
str_title += ' — '+formatNumber(arr_seg.count)+' ('+formatPercent(arr_seg.count / num_total)+(arr_seg.parent ? ', '+formatPercent(arr_seg.count / arr_seg.parent.count)+' of '+arr_seg.parent.label : '')+')';
return str_title;
};
const getCentre = function() {
return {
x: NODEGOAT.VIEW.width / 2 + NODEGOAT.VIEW.offset_x,
y: NODEGOAT.VIEW.height / 2 + NODEGOAT.VIEW.offset_y,
radius: Math.max(Math.min(NODEGOAT.VIEW.width, NODEGOAT.VIEW.height) / 2 - 20, 30) * NODEGOAT.VIEW.scale
};
};
const getHit = function(x, y) {
if (!num_total) {
return null;
}
const arr_centre = getCentre();
const dx = x - arr_centre.x, dy = y - arr_centre.y;
const r = Math.sqrt(dx * dx + dy * dy) / arr_centre.radius;
const i_ring = (r < RINGS[0].inner ? -1 : (r < RINGS[1].inner ? 0 : (r < RINGS[2].inner ? 1 : (r <= RINGS[2].outer + 0.02 ? 2 : -1))));
if (i_ring < 0) {
return null;
}
let num_angle = Math.atan2(dy, dx);
while (num_angle < ANGLE_START) {
num_angle += ANGLE_FULL;
}
while (num_angle >= ANGLE_START + ANGLE_FULL) {
num_angle -= ANGLE_FULL;
}
return arr_rings[i_ring].find(arr_seg => num_angle >= arr_seg.a0 && num_angle < arr_seg.a1) || null;
};
const queue = function() {
if (!is_queued) {
is_queued = true;
requestAnimationFrame(render);
}
};
const getContext = function(elm) {
const num_resolution = NODEGOAT.VIEW.resolution;
elm.width = Math.round(NODEGOAT.VIEW.width * num_resolution);
elm.height = Math.round(NODEGOAT.VIEW.height * num_resolution);
elm.style.width = (elm.width / num_resolution)+'px';
elm.style.height = (elm.height / num_resolution)+'px';
const context = elm.getContext('2d');
context.setTransform(num_resolution, 0, 0, num_resolution, 0, 0);
return context;
};
const getEase = function(t) {
return (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2);
};
const render = function() {
is_queued = false;
if (!NODEGOAT.VIEW || !NODEGOAT.ACTIVE) {
return;
}
const t = Math.min((performance.now() - num_anim_start) / ANIMATION, 1);
const num_ease = getEase(t);
for (const arr_segs of arr_rings) {
for (const arr_seg of arr_segs) {
arr_seg.c0 = arr_seg.f0 + (arr_seg.a0 - arr_seg.f0) * num_ease;
arr_seg.c1 = arr_seg.f1 + (arr_seg.a1 - arr_seg.f1) * num_ease;
}
}
const context = getContext(elm_canvas);
const arr_centre = getCentre();
drawChart(context, arr_centre.x, arr_centre.y, arr_centre.radius);
if (t < 1) {
queue();
}
};
const drawChart = function(context, cx, cy, num_radius) {
context.save();
if (!num_total) {
context.fillStyle = '#EEEEEE';
for (const arr_ring of RINGS) {
context.beginPath();
context.arc(cx, cy, arr_ring.outer * num_radius, 0, ANGLE_FULL);
context.arc(cx, cy, arr_ring.inner * num_radius, ANGLE_FULL, 0, true);
context.fill();
}
drawCentre(context, cx, cy, num_radius);
context.restore();
return;
}
const num_pixel = context.getTransform().a;
const arr_focused = [];
for (const arr_segs of arr_rings) {
for (const arr_seg of arr_segs) {
if (arr_seg.c1 - arr_seg.c0 <= 0.0005) {
continue;
}
if (isFocus(arr_seg)) {
arr_focused.push(arr_seg);
continue;
}
context.globalAlpha = (arr_focus ? 0.3 : 1);
drawSegment(context, cx, cy, num_radius, arr_seg, 0, num_pixel);
}
}
context.globalAlpha = 1;
for (const arr_seg of arr_focused) {
drawSegment(context, cx, cy, num_radius, arr_seg, 5, num_pixel);
}
for (const arr_segs of arr_rings) {
for (const arr_seg of arr_segs) {
context.globalAlpha = (arr_focus && !isFocus(arr_seg) ? 0.35 : 1);
drawLabel(context, cx, cy, num_radius, arr_seg);
}
}
context.globalAlpha = 1;
drawCentre(context, cx, cy, num_radius);
context.restore();
};
const drawSegment = function(context, cx, cy, num_radius, arr_seg, num_raise, num_pixel) {
const arr_ring = RINGS[arr_seg.ring];
const r0 = arr_ring.inner * num_radius, r1 = arr_ring.outer * num_radius + num_raise;
context.beginPath();
context.arc(cx, cy, r1, arr_seg.c0, arr_seg.c1);
context.arc(cx, cy, r0, arr_seg.c1, arr_seg.c0, true);
context.closePath();
if (num_raise) {
context.shadowColor = 'rgba(0, 0, 0, 0.3)';
context.shadowBlur = 10 * num_pixel;
context.shadowOffsetY = 3 * num_pixel;
}
context.fillStyle = arr_seg.color;
context.fill();
context.shadowColor = 'transparent';
context.shadowBlur = 0;
context.shadowOffsetY = 0;
context.lineWidth = 1.5;
context.lineJoin = 'round';
context.strokeStyle = '#ffffff';
context.stroke();
};
const drawLabel = function(context, cx, cy, num_radius, arr_seg) {
const arr_ring = RINGS[arr_seg.ring];
const r0 = arr_ring.inner * num_radius, r1 = arr_ring.outer * num_radius;
const num_thickness = r1 - r0, r = (r0 + r1) / 2;
const num_font = arr_ring.font;
if (num_thickness < num_font + 6) {
return;
}
const num_span = arr_seg.c1 - arr_seg.c0;
const num_arc = num_span * r - 12;
const num_chord = Math.sqrt(Math.max(0, 4 * r * (num_thickness - num_font - 6)));
const num_max = Math.min(num_arc, num_chord);
if (num_max < 22) {
return;
}
context.font = arr_ring.weight+num_font+'px '+FONT;
const str_label = fitText(context, arr_seg.label, num_max);
if (!str_label) {
return;
}
const str_percent = formatPercent(arr_seg.count / num_total);
const num_font_small = num_font - 2;
let has_percent = (num_thickness >= num_font * 2 + 14);
if (has_percent) {
context.font = num_font_small+'px '+FONT;
has_percent = (context.measureText(str_percent).width <= num_max);
context.font = arr_ring.weight+num_font+'px '+FONT;
}
const num_mid = (arr_seg.c0 + arr_seg.c1) / 2;
let num_rotate = num_mid + Math.PI / 2;
num_rotate = ((num_rotate + Math.PI) % ANGLE_FULL + ANGLE_FULL) % ANGLE_FULL - Math.PI;
if (num_rotate > Math.PI / 2 || num_rotate < -Math.PI / 2) {
num_rotate += Math.PI;
}
context.save();
context.translate(cx + Math.cos(num_mid) * r, cy + Math.sin(num_mid) * r);
context.rotate(num_rotate);
context.fillStyle = getTextColor(arr_seg.color);
context.textAlign = 'center';
context.textBaseline = 'middle';
if (has_percent) {
context.fillText(str_label, 0, -num_font * 0.5);
context.font = num_font_small+'px '+FONT;
context.globalAlpha *= 0.8;
context.fillText(str_percent, 0, num_font * 0.65);
} else {
context.fillText(str_label, 0, 0);
}
context.restore();
};
const drawCentre = function(context, cx, cy, num_radius) {
const r = RINGS[0].inner * num_radius - 6;
if (r < 22) {
return;
}
let str_caption = 'Counted';
let num_count = num_total;
let str_note = (num_total ? formatNumber(num_values)+' values · '+formatNumber(num_refs)+' references' : 'Nothing in the selection or the filters');
if (arr_focus && num_total) {
const arr_seg = arr_focus.seg;
if (arr_seg) {
str_caption = getSegmentLabel(arr_seg);
num_count = arr_seg.count;
str_note = formatPercent(arr_seg.count / num_total)+(arr_seg.parent ? ' · '+formatPercent(arr_seg.count / arr_seg.parent.count)+' of '+arr_seg.parent.label : ' of all');
} else {
const arr_row = arr_legend.rows.find(arr_row => !arr_row.header && !arr_row.empty && arr_row.ring == arr_focus.ring && arr_row.key === arr_focus.key);
if (arr_row) {
str_caption = arr_row.label;
num_count = arr_row.count;
str_note = formatPercent(arr_row.count / num_total)+' of all';
}
}
}
const num_width = r * 1.6;
const num_font = Math.round(Math.min(Math.max(r * 0.38, 16), 44));
context.textAlign = 'center';
context.textBaseline = 'middle';
context.font = '300 '+num_font+'px '+FONT;
context.fillStyle = TEXT_PRIMARY;
context.fillText(fitText(context, formatNumber(num_count), num_width) || '', cx, cy);
context.font = '500 '+(r > 50 ? 12 : 10)+'px '+FONT;
context.fillStyle = TEXT_SECONDARY;
context.fillText(fitText(context, str_caption, num_width * 0.9) || '', cx, cy - num_font * 0.5 - 12);
context.font = (r > 50 ? 11 : 9)+'px '+FONT;
context.fillText(fitText(context, str_note, num_width * 0.9) || '', cx, cy + num_font * 0.5 + 11);
};
const setLegend = function() {
const arr_rows = [];
let y = LEGEND_PADDING;
RINGS.forEach(function(arr_ring, i_ring) {
arr_rows.push({header: true, ring: i_ring, y: y});
y += LEGEND_HEADER;
const map_rows = new Map();
for (const arr_seg of arr_rings[i_ring]) {
let arr_row = map_rows.get(arr_seg.key);
if (!arr_row) {
arr_row = {ring: i_ring, key: arr_seg.key, label: arr_seg.label, color: arr_seg.color, count: 0, keys: new Set()};
map_rows.set(arr_seg.key, arr_row);
}
arr_row.count += arr_seg.count;
for (const arr_triple of arr_seg.triples) {
arr_row.keys.add(arr_triple[arr_ring.field]);
}
}
const arr_sorted = Array.from(map_rows.values()).sort((a, b) => (a.key == KEY_OTHER) - (b.key == KEY_OTHER) || b.count - a.count);
if (!arr_sorted.length) {
arr_rows.push({empty: true, ring: i_ring, y: y});
y += LEGEND_ROW;
}
for (const arr_row of arr_sorted) {
if (arr_row.key == KEY_OTHER) {
arr_row.label = 'Other ('+formatNumber(arr_row.keys.size)+' '+arr_ring.noun+')';
}
arr_row.y = y;
arr_rows.push(arr_row);
y += LEGEND_ROW;
}
});
arr_legend.rows = arr_rows;
const num_height = y + LEGEND_PADDING;
if (num_height !== arr_legend.height) {
arr_legend.height = num_height;
NODEGOAT.setControls({panels: {legend: getPanelLegend()}});
}
drawLegendControl();
};
const getLegendRow = function(y) {
return arr_legend.rows.find(arr_row => !arr_row.header && !arr_row.empty && y >= arr_row.y && y < arr_row.y + LEGEND_ROW) || null;
};
const drawLegendControl = function() {
if (!arr_legend.size) {
return;
}
const num_width = arr_legend.size.width;
const num_ratio = arr_legend.size.pixel_width / arr_legend.size.width;
const elm_legend = new OffscreenCanvas(Math.max(Math.round(num_width * num_ratio), 1), Math.max(Math.round(arr_legend.height * num_ratio), 1));
const context = elm_legend.getContext('2d');
context.scale(num_ratio, num_ratio);
drawLegend(context, num_width, true);
NODEGOAT.setControlContent('legend', 'legend', elm_legend.transferToImageBitmap());
};
const drawLegend = function(context, num_width, has_focus) {
const x_percent = num_width - 8, x_count = num_width - 54;
context.textBaseline = 'middle';
for (const arr_row of arr_legend.rows) {
if (arr_row.header) {
context.font = '500 11px '+FONT;
context.fillStyle = TEXT_SECONDARY;
context.textAlign = 'left';
context.fillText(RINGS[arr_row.ring].name.toUpperCase(), 8, arr_row.y + LEGEND_HEADER / 2 + 4);
continue;
}
const num_mid = arr_row.y + LEGEND_ROW / 2;
if (arr_row.empty) {
context.font = 'italic 12px '+FONT;
context.fillStyle = 'rgba(0, 0, 0, 0.38)';
context.textAlign = 'left';
context.fillText('Nothing counted', 30, num_mid);
continue;
}
if (has_focus && isFocusRow(arr_row)) {
context.fillStyle = 'rgba(0, 0, 0, 0.06)';
fillRoundRect(context, 2, arr_row.y + 1, num_width - 4, LEGEND_ROW - 2, 4);
}
context.beginPath();
context.arc(16, num_mid, 6, 0, ANGLE_FULL);
context.fillStyle = arr_row.color;
context.fill();
context.font = '12px '+FONT;
context.textAlign = 'right';
context.fillStyle = TEXT_SECONDARY;
context.fillText(formatPercent(arr_row.count / num_total), x_percent, num_mid);
const str_count = formatNumber(arr_row.count);
context.font = '500 12px '+FONT;
context.fillStyle = TEXT_PRIMARY;
context.fillText(str_count, x_count, num_mid);
const num_label = x_count - context.measureText(str_count).width - 12 - 30;
context.font = '13px '+FONT;
context.textAlign = 'left';
context.fillText(fitText(context, arr_row.label, num_label) || '', 30, num_mid);
}
};
const getColor = function(i_ring, str_key) {
if (str_key == KEY_OTHER) {
return COLORS_OTHER[i_ring];
}
if (i_ring == 2 && map_type_colors.get(str_key)) {
return map_type_colors.get(str_key);
}
const i = (map_color_index.get(str_key) || 0);
return PALETTE[(i + RINGS[i_ring].offset) % PALETTE.length][RINGS[i_ring].shade];
};
const getTextColor = function(str_color) {
if (!map_text_colors.has(str_color)) {
context_color = (context_color || new OffscreenCanvas(1, 1).getContext('2d', {willReadFrequently: true}));
context_color.clearRect(0, 0, 1, 1);
context_color.fillStyle = '#000000';
context_color.fillStyle = str_color;
context_color.fillRect(0, 0, 1, 1);
const arr_pixel = context_color.getImageData(0, 0, 1, 1).data;
const getLinear = c => (c /= 255, c <= 0.03928 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4));
const num_luminance = 0.2126 * getLinear(arr_pixel[0]) + 0.7152 * getLinear(arr_pixel[1]) + 0.0722 * getLinear(arr_pixel[2]);
map_text_colors.set(str_color, (num_luminance > 0.179 ? TEXT_PRIMARY : TEXT_LIGHT));
}
return map_text_colors.get(str_color);
};
const fitText = function(context, str, num_max) {
if (context.measureText(str).width <= num_max) {
return str;
}
const arr_chars = Array.from(str);
let num_low = 0, num_high = arr_chars.length - 1;
while (num_low < num_high) {
const num = Math.ceil((num_low + num_high) / 2);
if (context.measureText(arr_chars.slice(0, num).join('').trimEnd()+'…').width <= num_max) {
num_low = num;
} else {
num_high = num - 1;
}
}
return (num_low >= 3 ? arr_chars.slice(0, num_low).join('').trimEnd()+'…' : '');
};
const fillRoundRect = function(context, x, y, num_width, num_height, r) {
context.beginPath();
if (context.roundRect) {
context.roundRect(x, y, num_width, num_height, r);
} else {
context.rect(x, y, num_width, num_height);
}
context.fill();
};
const formatNumber = function(num) {
return num.toLocaleString();
};
const formatPercent = function(num) {
if (!isFinite(num)) {
return '0%';
}
const num_percent = num * 100;
if (num_percent >= 10) {
return Math.round(num_percent)+'%';
}
if (num_percent >= 0.1) {
return num_percent.toFixed(1).replace(/\.0$/, '')+'%';
}
return (num_percent > 0 ? '<0.1%' : '0%');
};Show the Sub-Objects as comets around a globe: the dating sets each comet's orbit and speed, active comets spiral down to gravitate around their location and inactive ones fly back out, and the timeline's range is a halo of mass that slows them all.
// Comet globe: the dating sets each comet's orbit and speed, active comets spiral down to swirl at their location, inactive ones fly back out. The timeline's range is a halo of mass that slows them all.
const URL_LAND = 'https://cdn.jsdelivr.net/npm/world-atlas@2.0.2/land-110m.json'; // Coastlines, null for none
const NUM_GLOBE = 0.58; // Globe radius, part of half the view
const NUM_HALO = 0.5; // Halo width, part of the globe radius
const NUM_LOOP = 7; // Loop radius around a place, px
const NUM_SPIN_LOOP = 3; // Radians per second on a loop
const NUM_SPIN_HALO = 0.3; // Radians per second in orbit
const NUM_FALL = 2.8; // Seconds to fall in or fly out
document.body.style.background = '#000000';
const elm_base = document.body.appendChild(document.createElement('canvas'));
const elm_comets = document.body.appendChild(document.createElement('canvas'));
elm_base.style.cssText = elm_comets.style.cssText = 'position: absolute; left: 0px; top: 0px;';
const ctx_base = elm_base.getContext('2d');
const ctx_comets = elm_comets.getContext('2d');
const map_nodes = new Map();
const map_index = new Map();
let arr_statements = [];
let num_comets = 0;
let arr_vectors = null;
let arr_located = null, arr_loop = null, arr_speed = null, arr_phase = null, arr_time = null, arr_active = null;
let arr_x = null, arr_y = null, arr_alpha = null;
let arr_orbit = null;
let arr_turn = null;
let arr_colors = [];
let arr_lines_graticule = [];
let arr_lines_land = [];
let num_lambda_base = 0, num_phi_base = 0;
let arr_shown = null;
let num_ring_min = 0, num_ring_max = 1;
let num_time_min = 0, num_time_span = 1;
let num_mass = 1;
let num_factor = 1, num_factor_shown = 1;
let num_speed = 1, num_speed_shown = 1;
let num_clock = 0, num_time_last = null;
let num_count_active = 0;
let str_text = '';
let i_hover = -1;
let arr_pointer = null;
NODEGOAT.setControls({panels: {comets: [
{type: 'text', id: 'text'},
{type: 'range', id: 'speed', min: 0, max: 3, step: 0.1, value: num_speed, title: 'Speed'}
]}});
const toVector = function(num_latitude, num_longitude) {
const num_phi = num_latitude * Math.PI / 180, num_lambda = num_longitude * Math.PI / 180;
return [Math.cos(num_phi) * Math.cos(num_lambda), Math.cos(num_phi) * Math.sin(num_lambda), Math.sin(num_phi)];
};
const getLocation = function(arr_geometry) {
let arr_coordinates = (arr_geometry.type == 'GeometryCollection' ? (arr_geometry.geometries[0] ? arr_geometry.geometries[0].coordinates : null) : arr_geometry.coordinates);
while (Array.isArray(arr_coordinates) && Array.isArray(arr_coordinates[0])) {
arr_coordinates = arr_coordinates[0];
}
if (!Array.isArray(arr_coordinates) || !isFinite(arr_coordinates[0]) || !isFinite(arr_coordinates[1])) {
return null;
}
return {latitude: arr_coordinates[1], longitude: arr_coordinates[0]};
};
for (let num_longitude = -180; num_longitude < 180; num_longitude += 30) {
const arr_line = [];
for (let num_latitude = -90; num_latitude <= 90; num_latitude += 3) {
arr_line.push(...toVector(num_latitude, num_longitude));
}
arr_lines_graticule.push(new Float32Array(arr_line));
}
for (let num_latitude = -60; num_latitude <= 60; num_latitude += 30) {
const arr_line = [];
for (let num_longitude = -180; num_longitude <= 180; num_longitude += 3) {
arr_line.push(...toVector(num_latitude, num_longitude));
}
arr_lines_graticule.push(new Float32Array(arr_line));
}
const setLand = function(arr_topology) {
if (!arr_topology || !Array.isArray(arr_topology.arcs)) {
return;
}
const arr_transform = arr_topology.transform;
const arr_lines = [];
for (const arr_arc of arr_topology.arcs) {
let num_x = 0, num_y = 0, arr_line = [], arr_previous = null;
for (const arr_point of arr_arc) {
let num_longitude = arr_point[0], num_latitude = arr_point[1];
if (arr_transform) {
num_x += arr_point[0];
num_y += arr_point[1];
num_longitude = num_x * arr_transform.scale[0] + arr_transform.translate[0];
num_latitude = num_y * arr_transform.scale[1] + arr_transform.translate[1];
}
if (arr_previous && ((Math.abs(arr_previous[0]) > 179.9 && Math.abs(num_longitude) > 179.9) || (arr_previous[1] < -89.9 && num_latitude < -89.9))) {
arr_lines.push(arr_line);
arr_line = [];
}
arr_line.push(...toVector(num_latitude, num_longitude));
arr_previous = [num_longitude, num_latitude];
}
arr_lines.push(arr_line);
}
arr_lines_land = arr_lines.filter(arr_line => arr_line.length >= 6).map(arr_line => new Float32Array(arr_line));
};
if (URL_LAND) {
fetch(URL_LAND).then(response => (response.ok ? response.json() : null)).then(setLand).catch(error => console.warn('No coastlines:', error));
}
NODEGOAT.on('open', function() {
for (const arr_node of NODEGOAT.DATA.nodes) {
map_nodes.set(arr_node.id, arr_node);
}
const arr_list = [];
let num_min = Infinity, num_max = -Infinity;
for (const arr_statement of NODEGOAT.DATA.statements) {
const arr_location = (arr_statement.geometry ? getLocation(arr_statement.geometry) : null);
const num_time = (arr_statement.date && arr_statement.date[0] != null ? NODEGOAT.getDate(arr_statement.date[0]).getTime() : NaN);
if (!arr_location && !isFinite(num_time)) {
continue;
}
if (isFinite(num_time)) {
num_min = Math.min(num_min, num_time);
num_max = Math.max(num_max, num_time);
}
arr_list.push({statement: arr_statement, location: arr_location, time: num_time});
}
num_comets = arr_list.length;
arr_vectors = new Float32Array(num_comets * 9);
arr_located = new Uint8Array(num_comets);
arr_loop = new Float32Array(num_comets);
arr_speed = new Float32Array(num_comets);
arr_phase = new Float32Array(num_comets);
arr_time = new Float64Array(num_comets);
arr_active = new Uint8Array(num_comets);
arr_x = new Float32Array(num_comets);
arr_y = new Float32Array(num_comets);
arr_alpha = new Float32Array(num_comets);
arr_orbit = new Float32Array(num_comets);
arr_turn = new Float32Array(num_comets).fill(NaN);
const num_span = (num_max - num_min) || 1;
let num_sum_x = 0, num_sum_y = 0, num_sum_z = 0;
arr_list.forEach(function(arr_item, i) {
arr_statements[i] = arr_item.statement;
map_index.set(arr_item.statement.id, i);
arr_time[i] = arr_item.time;
arr_speed[i] = (isFinite(arr_item.time) ? 0.1 + 0.9 * (arr_item.time - num_min) / num_span : 0);
arr_phase[i] = i * 2.39996;
arr_colors[i] = NODEGOAT.getStatementColor(arr_item.statement);
if (!arr_item.location) {
return;
}
const num_phi = arr_item.location.latitude * Math.PI / 180, num_lambda = arr_item.location.longitude * Math.PI / 180;
const num_sin_phi = Math.sin(num_phi), num_cos_phi = Math.cos(num_phi), num_sin_lambda = Math.sin(num_lambda), num_cos_lambda = Math.cos(num_lambda);
arr_vectors.set([
num_cos_phi * num_cos_lambda, num_cos_phi * num_sin_lambda, num_sin_phi,
-num_sin_lambda, num_cos_lambda, 0,
-num_sin_phi * num_cos_lambda, -num_sin_phi * num_sin_lambda, num_cos_phi
], i * 9);
arr_located[i] = 1;
arr_loop[i] = (isFinite(arr_item.time) ? 0.55 + 0.9 * ((i * 0.618034) % 1) : 0);
num_sum_x += num_cos_phi * num_cos_lambda;
num_sum_y += num_cos_phi * num_sin_lambda;
num_sum_z += num_sin_phi;
});
if (Math.hypot(num_sum_x, num_sum_y, num_sum_z) > 1e-9) {
num_lambda_base = Math.atan2(num_sum_y, num_sum_x);
num_phi_base = Math.atan2(num_sum_z, Math.hypot(num_sum_x, num_sum_y));
}
arr_shown = null;
});
NODEGOAT.on('draw', function() {
if (!arr_active) {
return;
}
arr_active.fill(0);
num_count_active = 0;
for (const statement_id of NODEGOAT.ACTIVE.statements) {
const i = map_index.get(statement_id);
if (i !== undefined && !arr_active[i]) {
arr_active[i] = 1;
num_count_active++;
}
}
const num_bounds_min = NODEGOAT.getDate(NODEGOAT.ACTIVE.bounds.min).getTime();
const num_bounds_span = NODEGOAT.getDate(NODEGOAT.ACTIVE.bounds.max).getTime() - num_bounds_min;
num_time_min = num_bounds_min;
num_time_span = num_bounds_span || 1;
if (num_bounds_span > 0) {
num_ring_min = clamp((NODEGOAT.getDate(NODEGOAT.ACTIVE.range.min).getTime() - num_bounds_min) / num_bounds_span, 0, 1);
num_ring_max = clamp((NODEGOAT.getDate(NODEGOAT.ACTIVE.range.max).getTime() - num_bounds_min) / num_bounds_span, 0, 1);
} else {
num_ring_min = 0;
num_ring_max = 1;
}
num_mass = Math.max(num_ring_max - num_ring_min, 0);
num_factor = 1 / (0.2 + 0.8 * num_mass);
setText();
});
NODEGOAT.on('control', function(arr_control) {
if (arr_control.id == 'speed') {
num_speed = arr_control.value;
setText();
}
});
const setText = function() {
const str_text_new = num_count_active.toLocaleString()+' of '+num_comets.toLocaleString()+' comets lit · mass '+Math.round(num_mass * 100)+'% · speed ×'+(num_factor * num_speed).toFixed(1);
if (str_text_new !== str_text) {
str_text = str_text_new;
NODEGOAT.setControlContent('comets', 'text', str_text);
}
};
NODEGOAT.on('pointer', function(arr_event) {
if (arr_event.event == 'leave') {
arr_pointer = null;
setHover(-1);
return;
}
if (arr_event.event == 'click') {
const i = getHit(arr_event.x, arr_event.y);
if (i >= 0) {
NODEGOAT.setReview({node_ids: [arr_statements[i].node_id], statement_ids: [arr_statements[i].id]});
return;
}
const arr_point = getPointCoordinates(arr_event.x, arr_event.y);
if (arr_point) {
const num_radius = getRadius();
const arr_target = getTarget();
const num_lambda = arr_target.lambda + getWrapped(arr_point.lambda - arr_target.lambda);
NODEGOAT.setView({offset_x: (num_lambda_base - num_lambda) * num_radius, offset_y: (arr_point.phi - num_phi_base) * num_radius});
}
return;
}
arr_pointer = (arr_event.touch ? null : {x: arr_event.x, y: arr_event.y});
if (arr_pointer) {
setHover(getHit(arr_pointer.x, arr_pointer.y));
}
});
const clamp = function(num, num_min, num_max) {
return Math.min(Math.max(num, num_min), num_max);
};
const getWrapped = function(num_angle) {
return ((num_angle + Math.PI) % (Math.PI * 2) + Math.PI * 2) % (Math.PI * 2) - Math.PI;
};
const getRadius = function() {
return Math.min(NODEGOAT.VIEW.width, NODEGOAT.VIEW.height) / 2 * NUM_GLOBE * NODEGOAT.VIEW.scale;
};
const getTarget = function() {
const num_radius = getRadius();
return {lambda: num_lambda_base - NODEGOAT.VIEW.offset_x / num_radius, phi: clamp(num_phi_base + NODEGOAT.VIEW.offset_y / num_radius, -Math.PI / 2, Math.PI / 2), radius: num_radius};
};
const getMatrix = function(num_lambda, num_phi) {
const num_sin_lambda = Math.sin(num_lambda), num_cos_lambda = Math.cos(num_lambda), num_sin_phi = Math.sin(num_phi), num_cos_phi = Math.cos(num_phi);
return [
-num_sin_lambda, num_cos_lambda, 0,
-num_sin_phi * num_cos_lambda, -num_sin_phi * num_sin_lambda, num_cos_phi,
num_cos_phi * num_cos_lambda, num_cos_phi * num_sin_lambda, num_sin_phi
];
};
const getPointCoordinates = function(x, y) {
if (!arr_shown) {
return null;
}
const m = getMatrix(arr_shown.lambda, arr_shown.phi);
const num_x = (x - NODEGOAT.VIEW.width / 2) / arr_shown.radius, num_y = -(y - NODEGOAT.VIEW.height / 2) / arr_shown.radius;
const num_distance = num_x * num_x + num_y * num_y;
if (num_distance > 1) {
return null;
}
const num_z = Math.sqrt(1 - num_distance);
return {lambda: Math.atan2(num_x * m[1] + num_y * m[4] + num_z * m[7], num_x * m[0] + num_y * m[3] + num_z * m[6]), phi: Math.asin(clamp(num_x * m[2] + num_y * m[5] + num_z * m[8], -1, 1))};
};
const getHit = function(x, y) {
let i_hit = -1, num_best = 64;
for (let i = 0; i < num_comets; i++) {
if (!arr_alpha[i]) {
continue;
}
const num_dx = arr_x[i] - x, num_dy = arr_y[i] - y;
const num_distance = num_dx * num_dx + num_dy * num_dy - (arr_active[i] ? 16 : 0);
if (num_distance < num_best) {
num_best = num_distance;
i_hit = i;
}
}
return i_hit;
};
const getTitle = function(i) {
const arr_statement = arr_statements[i];
const arr_node = map_nodes.get(arr_statement.node_id);
const str_model = NODEGOAT.getStatementModelName(arr_statement);
let str_title = (arr_node ? arr_node.name : '');
if (str_model && str_model != '~') {
str_title += ' · '+str_model;
}
if (arr_statement.date && arr_statement.date[0] != null) {
const num_year_start = NODEGOAT.getDate(arr_statement.date[0]).getFullYear();
const num_year_end = (arr_statement.date[1] != null ? NODEGOAT.getDate(arr_statement.date[1]).getFullYear() : num_year_start);
str_title += ' · '+num_year_start+(num_year_end != num_year_start ? '–'+num_year_end : '');
}
const arr_location = (arr_statement.location_node_id != null ? map_nodes.get(arr_statement.location_node_id) : null);
if (arr_location) {
str_title += ' · at '+arr_location.name;
}
return str_title;
};
const setHover = function(i) {
if (i === i_hover) {
return;
}
i_hover = i;
NODEGOAT.setHover(i >= 0 ? {title: getTitle(i), cursor: 'pointer'} : {});
};
const setCanvas = function(elm_canvas, context) {
const num_resolution = NODEGOAT.VIEW.resolution;
const num_width = Math.round(NODEGOAT.VIEW.width * num_resolution), num_height = Math.round(NODEGOAT.VIEW.height * num_resolution);
if (elm_canvas.width !== num_width || elm_canvas.height !== num_height) {
elm_canvas.width = num_width;
elm_canvas.height = num_height;
elm_canvas.style.width = NODEGOAT.VIEW.width+'px';
elm_canvas.style.height = NODEGOAT.VIEW.height+'px';
}
context.setTransform(num_resolution, 0, 0, num_resolution, 0, 0);
};
const strokeSphere = function(arr_lines, m, num_x, num_y, num_radius) {
ctx_base.beginPath();
for (const arr_line of arr_lines) {
let is_drawing = false;
for (let j = 0; j < arr_line.length; j += 3) {
const x = arr_line[j], y = arr_line[j + 1], z = arr_line[j + 2];
if (m[6] * x + m[7] * y + m[8] * z <= 0) {
is_drawing = false;
continue;
}
const num_screen_x = num_x + num_radius * (m[0] * x + m[1] * y + m[2] * z);
const num_screen_y = num_y - num_radius * (m[3] * x + m[4] * y + m[5] * z);
if (is_drawing) {
ctx_base.lineTo(num_screen_x, num_screen_y);
} else {
ctx_base.moveTo(num_screen_x, num_screen_y);
is_drawing = true;
}
}
}
ctx_base.stroke();
};
const render = function(num_time) {
requestAnimationFrame(render);
const num_dt = (num_time_last === null ? 16 : clamp(num_time - num_time_last, 0, 100));
num_time_last = num_time;
if (!NODEGOAT.VIEW || !NODEGOAT.ACTIVE || !arr_active) {
return;
}
const num_width = NODEGOAT.VIEW.width, num_height = NODEGOAT.VIEW.height;
setCanvas(elm_base, ctx_base);
setCanvas(elm_comets, ctx_comets);
const arr_target = getTarget();
if (arr_shown === null) {
arr_shown = arr_target;
} else {
const num_ease = 1 - Math.exp(-num_dt / 90);
arr_shown.lambda += (arr_target.lambda - arr_shown.lambda) * num_ease;
arr_shown.phi += (arr_target.phi - arr_shown.phi) * num_ease;
arr_shown.radius += (arr_target.radius - arr_shown.radius) * num_ease;
}
num_factor_shown += (num_factor - num_factor_shown) * (1 - Math.exp(-num_dt / 600));
num_speed_shown += (num_speed - num_speed_shown) * (1 - Math.exp(-num_dt / 300));
num_clock += num_dt / 1000 * num_factor_shown * num_speed_shown;
const num_x = num_width / 2, num_y = num_height / 2, num_radius = arr_shown.radius;
const m = getMatrix(arr_shown.lambda, arr_shown.phi);
const num_rate = num_dt / 1000 / NUM_FALL * Math.sqrt(num_factor_shown) * num_speed_shown;
for (let i = 0; i < num_comets; i++) {
const num_r_space = num_radius * (1 + NUM_HALO * (clamp((arr_time[i] - num_time_min) / num_time_span, 0, 1) || 0));
const num_angle_space = arr_phase[i] + num_clock * arr_speed[i] * NUM_SPIN_HALO;
let x = num_x + Math.cos(num_angle_space) * num_r_space;
let y = num_y + Math.sin(num_angle_space) * num_r_space;
let num_alpha = 1;
if (arr_located[i]) {
const num_target = arr_active[i];
let num_orbit = arr_orbit[i];
if (num_orbit != num_target) {
const num_step = num_rate * (0.5 + arr_speed[i]);
num_orbit = (num_target ? Math.min(num_orbit + num_step, 1) : Math.max(num_orbit - num_step, 0));
arr_orbit[i] = num_orbit;
}
if (num_orbit > 0) {
const j = i * 9;
const num_px = arr_vectors[j], num_py = arr_vectors[j + 1], num_pz = arr_vectors[j + 2];
const num_depth = m[6] * num_px + m[7] * num_py + m[8] * num_pz;
const num_angle = arr_phase[i] + num_clock * arr_speed[i] * NUM_SPIN_LOOP;
const num_cos = Math.cos(num_angle) * arr_loop[i] * NUM_LOOP, num_sin = Math.sin(num_angle) * arr_loop[i] * NUM_LOOP;
const num_ex = arr_vectors[j + 3], num_ey = arr_vectors[j + 4], num_nx = arr_vectors[j + 6], num_ny = arr_vectors[j + 7], num_nz = arr_vectors[j + 8];
const num_x_place = num_x + num_radius * (m[0] * num_px + m[1] * num_py + m[2] * num_pz) + num_cos * (m[0] * num_ex + m[1] * num_ey) + num_sin * (m[0] * num_nx + m[1] * num_ny + m[2] * num_nz);
const num_y_place = num_y - (num_radius * (m[3] * num_px + m[4] * num_py + m[5] * num_pz) + num_cos * (m[3] * num_ex + m[4] * num_ey) + num_sin * (m[3] * num_nx + m[4] * num_ny + m[5] * num_nz));
const num_alpha_place = clamp(num_depth * 5, 0, 1);
if (num_orbit >= 1) {
x = num_x_place;
y = num_y_place;
num_alpha = num_alpha_place;
arr_turn[i] = NaN;
} else {
const num_ease = num_orbit * num_orbit * (3 - 2 * num_orbit);
const num_r_place = Math.hypot(num_x_place - num_x, num_y_place - num_y);
const num_turn = getWrapped(Math.atan2(num_y_place - num_y, num_x_place - num_x) - num_angle_space);
if (isNaN(arr_turn[i])) {
arr_turn[i] = (num_target ? (num_turn < 0 ? num_turn + Math.PI * 2 : num_turn) : (num_turn > 0 ? num_turn - Math.PI * 2 : num_turn));
} else {
arr_turn[i] += getWrapped(num_turn - arr_turn[i]);
}
const num_r = num_r_space + (num_r_place - num_r_space) * num_ease;
const num_angle_spiral = num_angle_space + arr_turn[i] * num_ease;
x = num_x + Math.cos(num_angle_spiral) * num_r;
y = num_y + Math.sin(num_angle_spiral) * num_r;
num_alpha = 1 + (num_alpha_place - 1) * num_ease;
}
} else {
arr_turn[i] = NaN;
}
}
if (!num_alpha || x < -10 || x > num_width + 10 || y < -10 || y > num_height + 10) {
arr_alpha[i] = 0;
continue;
}
arr_x[i] = x;
arr_y[i] = y;
arr_alpha[i] = num_alpha;
}
ctx_comets.globalAlpha = 1;
ctx_comets.globalCompositeOperation = 'destination-out';
ctx_comets.fillStyle = 'rgba(0, 0, 0, '+(1 - Math.pow(0.82, num_dt / 16.7))+')';
ctx_comets.fillRect(0, 0, num_width, num_height);
ctx_comets.globalCompositeOperation = 'lighter';
ctx_comets.fillStyle = 'rgba(255, 255, 255, 0.4)';
for (let i = 0; i < num_comets; i++) {
if (arr_alpha[i] && !arr_active[i]) {
ctx_comets.globalAlpha = arr_alpha[i];
ctx_comets.fillRect(arr_x[i] - 0.7, arr_y[i] - 0.7, 1.4, 1.4);
}
}
ctx_comets.globalCompositeOperation = 'source-over';
let str_color = null;
for (let i = 0; i < num_comets; i++) {
if (arr_alpha[i] && arr_active[i]) {
if (arr_colors[i] !== str_color) {
str_color = arr_colors[i];
ctx_comets.fillStyle = str_color;
}
ctx_comets.globalAlpha = arr_alpha[i];
ctx_comets.fillRect(arr_x[i] - 1.3, arr_y[i] - 1.3, 2.6, 2.6);
}
}
ctx_comets.globalAlpha = 1;
ctx_comets.globalCompositeOperation = 'source-over';
ctx_base.fillStyle = '#000000';
ctx_base.fillRect(0, 0, num_width, num_height);
const elm_gradient = ctx_base.createRadialGradient(num_x - num_radius * 0.35, num_y - num_radius * 0.35, num_radius * 0.1, num_x, num_y, num_radius);
elm_gradient.addColorStop(0, '#13284a');
elm_gradient.addColorStop(1, '#040912');
ctx_base.beginPath();
ctx_base.arc(num_x, num_y, num_radius, 0, Math.PI * 2);
ctx_base.fillStyle = elm_gradient;
ctx_base.fill();
ctx_base.lineWidth = 1;
ctx_base.strokeStyle = 'rgba(120, 190, 255, 0.35)';
ctx_base.stroke();
ctx_base.lineWidth = 0.8;
ctx_base.strokeStyle = 'rgba(120, 170, 230, 0.13)';
strokeSphere(arr_lines_graticule, m, num_x, num_y, num_radius);
if (arr_lines_land.length) {
ctx_base.lineWidth = 1;
ctx_base.strokeStyle = 'rgba(170, 210, 255, 0.45)';
strokeSphere(arr_lines_land, m, num_x, num_y, num_radius);
}
const num_ring_inner = num_radius * (1 + NUM_HALO * num_ring_min), num_ring_outer = num_radius * (1 + NUM_HALO * num_ring_max);
ctx_base.beginPath();
ctx_base.arc(num_x, num_y, num_ring_outer, 0, Math.PI * 2);
ctx_base.arc(num_x, num_y, num_ring_inner, 0, Math.PI * 2, true);
ctx_base.fillStyle = 'rgba(80, 180, 255, '+(0.03 + 0.09 * num_mass)+')';
ctx_base.fill();
ctx_base.save();
ctx_base.lineWidth = 1.5;
ctx_base.strokeStyle = 'rgba(200, 230, 255, 0.45)';
ctx_base.shadowColor = 'rgba(80, 180, 255, 1)';
ctx_base.shadowBlur = 16;
for (const num_r of [num_ring_inner, num_ring_outer]) {
ctx_base.beginPath();
ctx_base.arc(num_x, num_y, num_r, 0, Math.PI * 2);
ctx_base.stroke();
}
ctx_base.restore();
if (arr_pointer) {
setHover(getHit(arr_pointer.x, arr_pointer.y));
}
if (i_hover >= 0 && arr_alpha[i_hover]) {
ctx_base.beginPath();
ctx_base.arc(arr_x[i_hover], arr_y[i_hover], 6, 0, Math.PI * 2);
ctx_base.lineWidth = 1.5;
ctx_base.strokeStyle = 'rgba(255, 255, 255, 0.85)';
ctx_base.stroke();
}
};
requestAnimationFrame(render);