Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listen() only updates display on value change #36

Merged
merged 4 commits into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 0.16.0-dev

29.14kb, 8.31kb gzipped

- Improved performance for GUI's that make heavy use of `listen()`.

# 0.16.0

29.05kb, 8.28kb gzipped
Expand Down
12 changes: 12 additions & 0 deletions examples/listen-stress/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<title>lil-gui - Basic Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/stats.js"></script>
<script type="module" src="listen-stress.js"></script>
</body>
</html>
83 changes: 83 additions & 0 deletions examples/listen-stress/listen-stress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* global Stats */
import GUI from '../../dist/lil-gui.esm.js';

const params = {
count: parseInt( location.search.substring( 1 ) ) || 10,
animateAll: false,
update() {

let diff = params.count - folder.controllers.length;

for ( ; diff < 0; diff++ ) {
folder.controllers.at( -1 ).destroy();
}

for ( ; diff > 0; diff-- ) {
const i = folder.controllers.length % dummyControllers.length;
const controller = dummyControllers[ i ]();
controller.listen();
}

folder.title( params.count + ' Listening Controllers' );

history.replaceState( {}, '', '?' + params.count );

}
};

const dummyControllers = [
() => folder.add( dummyParams, 'options', { Small: 1, Medium: 10, Large: 100 } ),
() => folder.add( dummyParams, 'boolean' ),
() => folder.add( dummyParams, 'string' ),
() => folder.add( dummyParams, 'number', 0, 1 ),
() => folder.addColor( dummyParams, 'color' )
];

const dummyParams = {
options: 10,
boolean: true,
string: 'lil-gui',
number: 0,
color: { r: 1, g: 0, b: 1 }
};

let frame = 0;
function animate() {

stats.end();
stats.begin();

if ( params.animateAll ) {

const time = Date.now() / 1000;

dummyParams.options = [ 1, 10, 100 ][ frame % 3 ];
dummyParams.boolean = frame % 2;
dummyParams.string = time.toString();
dummyParams.number = time % 1;

dummyParams.color.r = Math.sin( time / 2 ) / 2 + 0.5;
dummyParams.color.g = Math.sin( time / 3 ) / 2 + 0.5;
dummyParams.color.b = Math.sin( time / 5 ) / 2 + 0.5;

frame++;

}

requestAnimationFrame( animate );

}

const gui = new GUI();

gui.add( params, 'animateAll' );
gui.add( params, 'count' ).min( 0 );
gui.add( params, 'update' );

const folder = gui.addFolder();

const stats = new Stats();
document.body.appendChild( stats.dom );

params.update();
animate();
15 changes: 14 additions & 1 deletion src/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,21 @@ export default class Controller {
}

_listenCallback() {

this._listenCallbackID = requestAnimationFrame( this._listenCallback );
this.updateDisplay();

// To prevent framerate loss, make sure the value has changed before updating the display.
// Note: save() is used here instead of getValue() only because of ColorController. The !== operator
// won't work for color objects or arrays, but ColorController.save() always returns a string.

const curValue = this.save();

if ( curValue !== this._listenPrevValue ) {
this.updateDisplay();
}

this._listenPrevValue = curValue;

}

/**
Expand Down