2023-07-29 02:06:34 +00:00
|
|
|
import { GUI } from 'lil-gui';
|
|
|
|
|
2023-09-02 07:11:08 +00:00
|
|
|
|
|
|
|
const DEFAULTS = {
|
|
|
|
thickness: 0.25,
|
|
|
|
nodesize: 1.25,
|
|
|
|
linkopacity: 0.5,
|
2023-09-06 08:03:44 +00:00
|
|
|
link2opacity: 0.5,
|
2023-11-02 07:40:37 +00:00
|
|
|
shape: '120-cell',
|
|
|
|
option: 'none',
|
2023-10-27 01:45:46 +00:00
|
|
|
visibility: 5,
|
2023-09-06 08:03:44 +00:00
|
|
|
inscribed: false,
|
|
|
|
inscribe_all: false,
|
2023-09-02 07:11:08 +00:00
|
|
|
color: 0x3293a9,
|
|
|
|
background: 0xd4d4d4,
|
2023-11-02 07:40:37 +00:00
|
|
|
hyperplane: 1.5,
|
2023-11-02 22:55:44 +00:00
|
|
|
zoom: 1,
|
2023-09-02 07:41:00 +00:00
|
|
|
rotation: 'rigid',
|
2023-09-02 07:11:08 +00:00
|
|
|
dtheta: 0,
|
|
|
|
dpsi: 0,
|
|
|
|
}
|
2023-07-29 02:06:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-07-29 06:23:08 +00:00
|
|
|
class FourDGUI {
|
|
|
|
|
2023-10-31 23:22:26 +00:00
|
|
|
constructor(shapes, changeShape, setColor, setBackground, setLinkOpacity, setVisibility) {
|
2023-07-29 06:23:08 +00:00
|
|
|
this.gui = new GUI();
|
2023-10-31 23:47:46 +00:00
|
|
|
const SHAPE_NAMES = shapes.map((s) => s.name);
|
|
|
|
|
2023-07-29 06:23:08 +00:00
|
|
|
this.parseLinkParams();
|
2023-07-29 06:44:18 +00:00
|
|
|
const guiObj = this;
|
2023-07-29 06:23:08 +00:00
|
|
|
this.params = {
|
2023-09-02 07:11:08 +00:00
|
|
|
shape: this.link['shape'],
|
2023-10-31 23:47:46 +00:00
|
|
|
option: this.link['option'],
|
2023-09-06 08:03:44 +00:00
|
|
|
inscribed: this.link['inscribed'],
|
|
|
|
inscribe_all: this.link['inscribe_all'],
|
2023-09-02 07:11:08 +00:00
|
|
|
thickness: this.link['thickness'],
|
|
|
|
linkopacity: this.link['linkopacity'],
|
2023-09-06 08:03:44 +00:00
|
|
|
link2opacity: this.link['linkopacity'],
|
2023-09-02 07:11:08 +00:00
|
|
|
nodesize: this.link['nodesize'],
|
2023-10-27 01:45:46 +00:00
|
|
|
depth: this.link['depth'],
|
2023-09-02 07:11:08 +00:00
|
|
|
color: this.link['color'],
|
|
|
|
background: this.link['background'],
|
|
|
|
hyperplane: this.link['hyperplane'],
|
2023-11-02 22:55:44 +00:00
|
|
|
zoom: this.link['zoom'],
|
2023-09-02 07:41:00 +00:00
|
|
|
rotation: this.link['rotation'],
|
2023-07-29 06:23:08 +00:00
|
|
|
damping: false,
|
2023-09-02 07:11:08 +00:00
|
|
|
dtheta: this.link['dtheta'],
|
|
|
|
dpsi: this.link['dpsi'],
|
2023-07-29 06:44:18 +00:00
|
|
|
"copy link": function () { guiObj.copyUrl() }
|
2023-07-29 06:23:08 +00:00
|
|
|
};
|
2023-10-27 06:32:44 +00:00
|
|
|
let options_ctrl;
|
2023-10-31 23:22:26 +00:00
|
|
|
this.gui.add(this.params, 'shape', SHAPE_NAMES).onChange((shape) => {
|
|
|
|
const options = this.getShapeOptions(shapes, shape);
|
|
|
|
options_ctrl = options_ctrl.options(options).onChange((option) => {
|
|
|
|
setVisibility(option)
|
|
|
|
});
|
2023-11-01 00:57:46 +00:00
|
|
|
options_ctrl.setValue(options[0])
|
2023-10-31 23:22:26 +00:00
|
|
|
changeShape(shape)
|
|
|
|
});
|
|
|
|
const options = this.getShapeOptions(shapes, this.params['shape']);
|
2023-11-02 22:55:44 +00:00
|
|
|
options_ctrl = this.gui.add(this.params, 'option').options(options).onChange((option) => {
|
2023-10-31 23:22:26 +00:00
|
|
|
setVisibility(option)
|
2023-10-27 06:32:44 +00:00
|
|
|
});
|
2023-11-02 22:55:44 +00:00
|
|
|
this.gui.add(this.params, 'hyperplane', 1.4, 2.0);
|
|
|
|
this.gui.add(this.params, 'zoom', 0.1, 2.0);
|
2023-09-02 07:11:08 +00:00
|
|
|
this.gui.add(this.params, 'thickness', 0.1, 2);
|
2023-09-06 08:03:44 +00:00
|
|
|
this.gui.add(this.params, 'linkopacity', 0, 1).onChange(
|
|
|
|
(v) => setLinkOpacity(v, true)
|
|
|
|
);
|
|
|
|
this.gui.add(this.params, 'link2opacity', 0, 1).onChange(
|
|
|
|
(v) => setLinkOpacity(v, false)
|
|
|
|
);
|
2023-09-02 07:11:08 +00:00
|
|
|
this.gui.add(this.params, 'nodesize', 0.1, 4);
|
2023-07-29 06:23:08 +00:00
|
|
|
this.gui.addColor(this.params, 'color').onChange(setColor);
|
|
|
|
this.gui.addColor(this.params, 'background').onChange(setBackground);
|
2023-09-02 07:41:00 +00:00
|
|
|
this.gui.add(this.params, 'rotation', [ 'rigid', 'tumbling', 'inside-out', 'axisymmetrical' ]);
|
2023-07-29 06:23:08 +00:00
|
|
|
this.gui.add(this.params, 'damping');
|
|
|
|
this.gui.add(this.params, 'copy link');
|
2023-07-29 06:52:00 +00:00
|
|
|
|
2023-10-31 23:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getShapeOptions(shapes, shape) {
|
|
|
|
const spec = shapes.filter((s) => s.name === shape);
|
|
|
|
if( spec && spec[0].options ) {
|
|
|
|
return spec[0].options.map((o) => o.name);
|
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2023-07-29 06:23:08 +00:00
|
|
|
|
|
|
|
|
2023-09-02 07:11:08 +00:00
|
|
|
numParam(param, parser) {
|
2023-07-29 06:23:08 +00:00
|
|
|
const value = this.urlParams.get(param);
|
|
|
|
if( value ) {
|
|
|
|
const n = parser(value);
|
|
|
|
if( n !== NaN ) {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
}
|
2023-09-02 07:11:08 +00:00
|
|
|
return DEFAULTS[param];
|
2023-07-29 06:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 06:44:18 +00:00
|
|
|
stringToHex(cstr) {
|
2023-07-29 06:23:08 +00:00
|
|
|
return parseInt('0x' + cstr.substr(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
hexToString(hex) {
|
|
|
|
return '#' + hex.toString(16);
|
|
|
|
}
|
2023-07-29 02:06:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2023-07-29 06:23:08 +00:00
|
|
|
parseLinkParams() {
|
|
|
|
this.linkUrl = new URL(window.location.toLocaleString());
|
|
|
|
this.link = {};
|
2023-09-06 08:03:44 +00:00
|
|
|
const guiObj = this;
|
2023-07-29 02:06:34 +00:00
|
|
|
|
2023-07-29 06:23:08 +00:00
|
|
|
this.urlParams = this.linkUrl.searchParams;
|
2023-10-31 23:47:46 +00:00
|
|
|
for( const param of [ "shape", "rotation", "option" ]) {
|
2023-07-29 06:23:08 +00:00
|
|
|
const value = this.urlParams.get(param);
|
|
|
|
if( value ) {
|
|
|
|
this.link[param] = value;
|
2023-09-02 07:11:08 +00:00
|
|
|
} else {
|
|
|
|
this.link[param] = DEFAULTS[param];
|
2023-07-29 06:23:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-06 08:03:44 +00:00
|
|
|
for( const param of [ "inscribed", "inscribe_all"] ) {
|
|
|
|
this.link[param] = ( this.urlParams.get(param) === 'y' );
|
|
|
|
}
|
2023-09-02 07:11:08 +00:00
|
|
|
this.link['hyperplane'] = this.numParam('hyperplane', parseFloat);
|
2023-11-02 22:55:44 +00:00
|
|
|
this.link['zoom'] = this.numParam('zoom', parseFloat);
|
2023-09-02 07:11:08 +00:00
|
|
|
this.link['thickness'] = this.numParam('thickness', parseFloat);
|
|
|
|
this.link['linkopacity'] = this.numParam('linkopacity', parseFloat);
|
2023-09-06 08:03:44 +00:00
|
|
|
this.link['link2opacity'] = this.numParam('link2opacity', parseFloat);
|
2023-09-02 07:11:08 +00:00
|
|
|
this.link['nodesize'] = this.numParam('nodesize', parseFloat);
|
|
|
|
this.link['color'] = this.numParam('color', (s) => guiObj.stringToHex(s));
|
|
|
|
this.link['background'] = this.numParam('background', (s) => guiObj.stringToHex(s));
|
|
|
|
this.link['dpsi'] = this.numParam('dpsi', parseFloat);
|
|
|
|
this.link['dtheta'] = this.numParam('dtheta', parseFloat);
|
2023-07-29 02:06:34 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 06:23:08 +00:00
|
|
|
|
|
|
|
copyUrl() {
|
2023-07-29 06:44:18 +00:00
|
|
|
const url = new URL(this.linkUrl.origin + this.linkUrl.pathname);
|
2023-07-29 06:23:08 +00:00
|
|
|
url.searchParams.append("shape", this.params.shape);
|
2023-10-31 23:47:46 +00:00
|
|
|
url.searchParams.append("option", this.params.option);
|
2023-09-06 08:03:44 +00:00
|
|
|
url.searchParams.append("inscribed", this.params.inscribed ? 'y': 'n');
|
|
|
|
url.searchParams.append("inscribe_all", this.params.inscribe_all ? 'y': 'n');
|
2023-07-29 06:23:08 +00:00
|
|
|
url.searchParams.append("thickness", this.params.thickness.toString());
|
2023-08-04 07:11:41 +00:00
|
|
|
url.searchParams.append("nodesize", this.params.nodesize.toString());
|
2023-09-06 08:03:44 +00:00
|
|
|
url.searchParams.append("linkopacity", this.params.thickness.toString());
|
|
|
|
url.searchParams.append("link2opacity", this.params.nodesize.toString());
|
2023-07-29 06:44:18 +00:00
|
|
|
url.searchParams.append("color", this.hexToString(this.params.color));
|
|
|
|
url.searchParams.append("background", this.hexToString(this.params.background));
|
2023-07-29 06:23:08 +00:00
|
|
|
url.searchParams.append("hyperplane", this.params.hyperplane.toString());
|
2023-11-02 22:55:44 +00:00
|
|
|
url.searchParams.append("zoom", this.params.zoom.toString());
|
2023-09-02 07:41:00 +00:00
|
|
|
url.searchParams.append("rotation", this.params.rotation);
|
2023-07-29 06:44:18 +00:00
|
|
|
url.searchParams.append("dtheta", this.params.dtheta.toString());
|
|
|
|
url.searchParams.append("dpsi", this.params.dpsi.toString());
|
2023-07-29 06:23:08 +00:00
|
|
|
this.copyTextToClipboard(url);
|
2023-07-29 02:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-29 06:23:08 +00:00
|
|
|
copyTextToClipboard(text) {
|
|
|
|
if (!navigator.clipboard) {
|
|
|
|
this.fallbackCopyTextToClipboard(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
navigator.clipboard.writeText(text).then(function() {
|
|
|
|
console.log('Async: Copying to clipboard was successful!');
|
|
|
|
}, function(err) {
|
|
|
|
console.error('Async: Could not copy text: ', err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fallbackCopyTextToClipboard(text) {
|
|
|
|
var textArea = document.createElement("textarea");
|
|
|
|
textArea.value = text;
|
|
|
|
|
|
|
|
// Avoid scrolling to bottom
|
|
|
|
textArea.style.top = "0";
|
|
|
|
textArea.style.left = "0";
|
|
|
|
textArea.style.position = "fixed";
|
|
|
|
|
|
|
|
document.body.appendChild(textArea);
|
|
|
|
textArea.focus();
|
|
|
|
textArea.select();
|
|
|
|
|
|
|
|
try {
|
|
|
|
var successful = document.execCommand('copy');
|
|
|
|
var msg = successful ? 'successful' : 'unsuccessful';
|
|
|
|
console.log('Fallback: Copying text command was ' + msg);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Fallback: Oops, unable to copy', err);
|
|
|
|
}
|
|
|
|
|
|
|
|
document.body.removeChild(textArea);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-02 07:11:08 +00:00
|
|
|
export { FourDGUI, DEFAULTS };
|