Skip to content

Commit e10e988

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

3 files changed

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

0 commit comments

Comments
 (0)