-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples.html
137 lines (110 loc) · 4.89 KB
/
examples.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!doctype html>
<html lang="en">
<head>
<title>lil-gui-inout Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="params"></div>
<script type="module">
import GUI from './lil-gui/lil-gui.esm.js';
import { Filter, useFilter } from './inout/lil-gui-inout.esm.js';
const gui = new GUI();
const params = {
boolean: true,
number1: 0.5,
number2: 0,
useFilter: 0,
string1: 'Hello World',
color: 'rgb(255,0,0)',
colorhsv: { h: 120, s: 1, v: 1 },
rrggbbaa: '#ff0000aa',
};
// negate boolean
const f_boolean = new Filter( params, 'boolean', v => !v, v => !v );
gui.add( f_boolean, 'boolean' ).name( 'negate boolean' );
// double number
const f_number1 = new Filter( params, 'number1', v => 2 * v, v => v / 2 );
gui.add( f_number1, 'number1', 0, 2 )
.name( 'double number' );
// with .listen()
gui.add( new Filter( params, 'number2', v => v + 1, v => v - 1 ), 'number2' )
.name( ".listen()" ).listen();
gui.add( params, 'number2' ).listen();
// useFilter helper
gui.add( params, 'useFilter', 0, 10, 0.1 ).name( 'x' ).listen();
const uf1 = useFilter( x => x + 10, x => x - 10, gui )
// everything from this point on is the same as lil-gui
.add( params, 'useFilter', 10, 20 )
.name( 'x + 10' ).listen();
// Extend GUI class with useFilter to omit 3rd argument
GUI.prototype.useFilter = useFilter;
gui.useFilter( x => 100 - 5 * x, x => 20 - x / 5 )
.add( params, 'useFilter', 50, 100 )
.name( '100 - 5 * x' ).listen();
gui.addFolder( 'aFolder' ).useFilter( x => x * x * x * x + 10, x => ( x - 10 ) ** ( 1 / 4 ) )
.add( params, 'useFilter', 10, 10010, 0.01 )
.name( 'x^4 + 10' ).listen();
// control string with options
gui.useFilter( s => s.substring( 6 ), s => 'Hello ' + s )
.add( params, 'string1', [ 'World', 'Squirrel' ] )
.name( 'string w/ choices' )
// or another text input
gui.useFilter( s => s.substring( 6 ), s => 'Hello ' + s )
.add( params, 'string1' ).listen()
.name( 'or another text' );
function hexToRgb( hex ) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( hex );
return `rgb(${ parseInt( result[ 1 ], 16 ) },${ parseInt( result[ 2 ], 16 ) },${ parseInt( result[ 3 ], 16 ) })`;
}
// support rgb string output
gui.useFilter( v => v, hexToRgb )
.addColor( params, 'color' )
.name( 'rgb-string' )
.listen()
.onChange( ( v ) => { console.log( 'oc', v ); } )
.onFinishChange( ( v ) => { console.log( 'ofc', v ); } )
function rgbString2hsvObject( rgb ) {
let a = rgb.slice( 1 ).match( /.{1,2}/g ).map( x => parseInt( x, 16 ) ).map( x => x / 255 );
let r = a[ 0 ], g = a[ 1 ], b = a[ 2 ];
let v = Math.max( r, g, b ), n = v - Math.min( r, g, b );
let h = n && ( ( v == r ) ? ( g - b ) / n : ( ( v == g ) ? 2 + ( b - r ) / n : 4 + ( r - g ) / n ) );
let hsv = { h: 60 * ( h < 0 ? h + 6 : h ), s: v && n / v, v: v };
return hsv;
}
function hsvObj2rgbStringect( hsv ) {
let { h, s, v } = hsv;
let f = ( n, k = ( n + h / 60 ) % 6 ) => v - v * s * Math.max( Math.min( k, 4 - k, 1 ), 0 );
let rgb = [ Math.round( 255 * f( 5 ) ), Math.round( 255 * f( 3 ) ), Math.round( 255 * f( 1 ) ) ];
let str = `rgb(${ rgb.join( ',' ) })`;
return str;
}
// support hsv object
gui.useFilter( hsvObj2rgbStringect, rgbString2hsvObject )
.addColor( params, 'colorhsv' )
.name( 'hsv object' )
.listen();
// support #rrggbbaa string
gui.useFilter( x => `#${ x.slice( 1, -2 ) }`, x => x + params.rrggbbaa.slice( -2 ) )
.addColor( params, 'rrggbbaa' );
// and its alpha
gui.useFilter(
x => parseInt( x.slice( -2 ), 16 ),
x => params.rrggbbaa.slice( 0, -2 ) + x.toString( 16 ).padStart( 2, 0 )
)
.add( params, 'rrggbbaa', 0, 255 ).step( 1 )
.name( 'rrggbbaa alpha' );
// --------------------------------------------------
function paramsString( params ) {
return '<pre>' + JSON.stringify( params, null, 2 ) + '</pre>';
}
function printParams() {
document.getElementById( 'params' ).innerHTML = paramsString( params );
}
gui.onChange( printParams );
printParams();
window.params = params;
</script>
</body>
</html>