Misc
Event handling, importing/exporting a state, and some tips.
Events
Use on() of specific components to listen its changes. Input components will emit change events. The first argument of the event handler is the event object that contains a value.
const pane = new Pane();
pane.addBinding(PARAMS, 'value')
.on('change', (ev) => {
console.log(ev.value.toFixed(2));
if (ev.last) {
console.log('(last)');
}
});If you want to handle global events (for all of components), on() of the pane is for it.
const pane = new Pane();
pane.addBinding(PARAMS, 'boolean');
pane.addBinding(PARAMS, 'color');
pane.addBinding(PARAMS, 'number');
pane.addBinding(PARAMS, 'string');
pane.on('change', (ev) => {
console.log('changed: ' + JSON.stringify(ev.value));
});
Import/Export
Tweakpane can import/export a blade state with exportState().
const pane = new Pane();
// pane.addBinding(PARAMS, ...);
// pane.addBinding(PARAMS, ...);
const state = pane.exportState();
console.log(state);
To import an exported state, pass the state object to importState().
const pane = new Pane();
// ...
const f = pane.addFolder({
title: 'Values',
});
// f.addBinding(PARAMS, ...);
// f.addBinding(PARAMS, ...);
f.importState(state);
Tips
Custom container
If you want to put a pane into the specific element, pass it as container option of the pane.
const pane = new Pane({
container: document.getElementById('someContainer'),
});
Custom label
You can set a label of components by label option.
const pane = new Pane();
pane.addBinding(PARAMS, 'initSpd', {
label: 'Initial speed',
});
pane.addBinding(PARAMS, 'size', {
label: 'Force field\nradius',
});
Refresh manually
By default, Tweakpane doesn't detect changes of bound parameters. Use refresh() to force-update all input/monitor components.
const pane = new Pane();
// pane.addBinding(PARAMS, ...);
// pane.addBinding(PARAMS, ...);
pane.refresh();
Visibility
Toggle hidden property to show/hide components.
const pane = new Pane();
const f = pane.addFolder({
title: 'Advanced',
});
// ...
btn.on('click', () => {
f.hidden = !f.hidden;
});
Disabled
Use disabled property to disable a view temporarily.
const pane = new Pane();
const i = pane.addBinding(PARAMS, 'param', {
disabled: true,
title: 'Advanced',
});
// ...
btn.on('click', () => {
i.disabled = !i.disabled;
});
Disposing
If you want to dispose a pane manually, call dispose() of the pane. You can also dispose each component in the same way.
const pane = new Pane();
const i = pane.addBinding(PARAMS, 'count');
// ...
// Dispose the input
i.dispose();
// Dispose the pane
pane.dispose();
Adding input/monitor at a specific position
Use index option to specify an index.
const pane = new Pane();
pane.addButton({title: 'Run'});
pane.addButton({title: 'Stop'});
pane.addButton({
index: 1,
title: '**Reset**',
});