Skip to content

Commit 1c99d03

Browse files
enable WebGL acceleration gated behind "gpu" GET parameter
1 parent afe0b10 commit 1c99d03

3 files changed

Lines changed: 69 additions & 52 deletions

File tree

js/browser/graph.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/** Provides graph operations such as initialization, wayfinding and highlighting.*/
22
/*eslint no-unused-vars: ["warn", { "argsIgnorePattern": "^_" }]*/
3+
import { params } from "./params";
34
import { coloredEdgeStyle, showPropertyStyle, style } from "./style";
45
import { colorschemenight } from "./colorschemenight";
56
import { colorschemeday } from "./colorschemeday";
@@ -37,10 +38,20 @@ export class Graph {
3738
const initTimer = timer("graph-init");
3839
this.container = container;
3940
this.container.style.backgroundColor = "black"; // required to show background image
41+
const renderer = {
42+
name: "canvas",
43+
webgl: true,
44+
showFps: true,
45+
webglDebug: true,
46+
};
47+
if (params.gpu) {
48+
log.warn("'gpu' flag detected: Activating experimental WebGL hardware acceleration.");
49+
}
4050
this.cy = cytoscape({
4151
container,
4252
//@ts-expect-error concat type
4353
style: style.style.concat(colorschemenight),
54+
...(params.gpu && { renderer }),
4455
wheelSensitivity: 2,
4556
minZoom: 0.02,
4657
maxZoom: 7,

js/browser/init.ts

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { config } from "../config/config";
2+
import { params } from "./params";
23
import { loadGraphFromSparql } from "../loadGraphFromSparql";
34
import { loadGraphFromJsonFile, loadLayoutFromJsonObject } from "./load";
45
import { Search } from "./search";
@@ -13,64 +14,15 @@ import { Graph } from "./graph";
1314
import type { LayoutJson } from "./save.ts";
1415
import { View } from "./view";
1516

16-
interface Params {
17-
empty: boolean;
18-
benchmark: boolean;
19-
instances: boolean;
20-
virtual: boolean;
21-
class: string;
22-
json: string;
23-
sparql: string;
24-
graph: string;
25-
sub: string;
26-
}
27-
28-
/** A flag is a GET parameter with a boolean value.
29-
Allow setting a flag without a value, for example <https://www.snik.eu/graph?instances>.
30-
In this case the empty string is returned, see <https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/get>.
31-
The alternative is <https://www.snik.eu/graph?instances=true> but all other values are treated as false.
32-
This is needed in case one wants to override a flag that is active by default through the configuration. */
33-
function parseFlag(f: string): boolean {
34-
return f === "" || f === "true";
35-
}
36-
37-
/** Parse browser URL GET parameters. */
38-
function parseParams(): Params {
39-
const url = new URL(window.location.href);
40-
const defaults = {
41-
sparql: config.ontology.sparql.endpoint,
42-
instances: config.ontology.sparql.instances,
43-
};
44-
// TypeScript interfaces don't exist on runtime, keep in sync with Params interface.
45-
const paramKeys = new Set(["empty", "benchmark", "instances", "virtual", "class", "json", "sparql", "graph", "sub"]);
46-
const unknown = new Set(Array.from(url.searchParams.keys())).difference(paramKeys);
47-
if (unknown.size > 0) {
48-
log.warn("Unknown GET parameters: " + Array.from(unknown).join(", "));
49-
}
50-
return Object.assign(defaults, {
51-
empty: parseFlag(url.searchParams.get("empty")),
52-
benchmark: parseFlag(url.searchParams.get("benchmark")),
53-
// load and show instances when loading from endpoint, not only classes
54-
...(url.searchParams.get("instances") && { instances: parseFlag(url.searchParams.get("instances")) }),
55-
virtual: parseFlag(url.searchParams.get("virtual")), // create "virtual triples" to visualize connections like domain-range
56-
class: url.searchParams.get("class"),
57-
json: url.searchParams.get("json"),
58-
...(url.searchParams.get("sparql") && { sparql: url.searchParams.get("sparql") }), // don't overwrite default with null
59-
graph: url.searchParams.get("graph"),
60-
sub: url.searchParams.get("sub"),
61-
});
62-
}
63-
6417
/**
6518
* Apply parameters and load graph.
6619
@param graph - the graph to apply the params to
6720
@param params - parameter object */
68-
async function applyParams(graph: Graph, params: Params): Promise<void> {
21+
async function applyParams(graph: Graph): Promise<void> {
6922
try {
7023
if (params.benchmark) {
7124
addBenchmarkOverlay(graph.cy);
7225
}
73-
7426
if (params.empty) {
7527
log.info(`Parameter "empty" detected. Skip loading and display file load prompt.`);
7628
const loadArea = document.getElementById("loadarea");
@@ -172,8 +124,7 @@ export async function fillInitialGraph(graph: Graph): Promise<void> {
172124

173125
// GET parameters
174126
await progress(async () => {
175-
const params = parseParams();
176-
await applyParams(graph, params);
127+
await applyParams(graph);
177128
new Search(util.getElementById("search") as HTMLFormElement);
178129
initHelp();
179130
});

js/browser/params.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/** Manages browser URL GET parameters.*/
2+
import { config } from "../config/config";
3+
import log from "loglevel";
4+
5+
export interface Params {
6+
empty: boolean;
7+
benchmark: boolean;
8+
instances: boolean;
9+
gpu: boolean;
10+
virtual: boolean;
11+
class: string;
12+
json: string;
13+
sparql: string;
14+
graph: string;
15+
sub: string;
16+
}
17+
18+
/** A flag is a GET parameter with a boolean value.
19+
Allow setting a flag without a value, for example <https://www.snik.eu/graph?instances>.
20+
In this case the empty string is returned, see <https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/get>.
21+
The alternative is <https://www.snik.eu/graph?instances=true> but all other values are treated as false.
22+
This is needed in case one wants to override a flag that is active by default through the configuration. */
23+
function parseFlag(f: string): boolean {
24+
return f === "" || f === "true";
25+
}
26+
27+
/** Parse browser URL GET parameters. */
28+
function parseParams(): Params {
29+
const url = new URL(window.location.href);
30+
const defaults = {
31+
sparql: config.ontology.sparql.endpoint,
32+
instances: config.ontology.sparql.instances,
33+
};
34+
// TypeScript interfaces don't exist on runtime, keep in sync with Params interface.
35+
const paramKeys = new Set(["empty", "benchmark", "instances", "virtual", "class", "json", "sparql", "graph", "sub", "gpu"]);
36+
const unknown = new Set(Array.from(url.searchParams.keys())).difference(paramKeys);
37+
if (unknown.size > 0) {
38+
log.warn("Unknown GET parameters: " + Array.from(unknown).join(", "));
39+
}
40+
return Object.assign(defaults, {
41+
empty: parseFlag(url.searchParams.get("empty")),
42+
benchmark: parseFlag(url.searchParams.get("benchmark")),
43+
// load and show instances when loading from endpoint, not only classes
44+
...(url.searchParams.get("instances") && { instances: parseFlag(url.searchParams.get("instances")) }),
45+
gpu: parseFlag(url.searchParams.get("gpu")),
46+
virtual: parseFlag(url.searchParams.get("virtual")), // create "virtual triples" to visualize connections like domain-range
47+
class: url.searchParams.get("class"),
48+
json: url.searchParams.get("json"),
49+
...(url.searchParams.get("sparql") && { sparql: url.searchParams.get("sparql") }), // don't overwrite default with null
50+
graph: url.searchParams.get("graph"),
51+
sub: url.searchParams.get("sub"),
52+
});
53+
}
54+
55+
export const params = parseParams();

0 commit comments

Comments
 (0)