mirror of
https://github.com/dataarts/dat.gui.git
synced 2024-12-12 04:08:27 +00:00
182 lines
14 KiB
HTML
182 lines
14 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>dat-gui</title>
|
|
|
|
<script src="build/dat-gui.js"></script>
|
|
<link rel="stylesheet" href="docs/style.css">
|
|
|
|
</style>
|
|
|
|
</head>
|
|
<body>
|
|
|
|
<!-- Copied from README.md -->
|
|
<div id="readme">
|
|
<h1 id="dat-gui">dat-gui</h1>
|
|
<p>dat-gui creates an interface that you can use to modify variables with very little code. </p>
|
|
<p><a href="#basic-usage"> Basic Usage </a> •
|
|
<a href="#limits"> Limits </a> •
|
|
<a href="#colors"> Colors </a> •
|
|
<a href="#events"> Events </a> •
|
|
<a href="#folders"> Folders </a> •
|
|
<a href="#saving"> Saving </a></p>
|
|
<h3 id="basic-usage">Basic Usage</h3>
|
|
<p>Download the <a href="todo">minified library</a> and include it in your html.</p>
|
|
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"gui.js"</span>></span><span class="javascript"></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span></code></pre>
|
|
<p>Create controllers by adding objects and their properties. dat-gui chooses a controller based on the variable's initial value.</p>
|
|
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> gui = <span class="hljs-keyword">new</span> Gui();
|
|
gui.<span class="hljs-keyword">add</span>( object, <span class="hljs-string">'numberProperty'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span> ); <span class="hljs-comment">// Slider</span>
|
|
gui.<span class="hljs-keyword">add</span>( object, <span class="hljs-string">'stringProperty'</span> ); <span class="hljs-comment">// Text box</span>
|
|
gui.<span class="hljs-keyword">add</span>( object, <span class="hljs-string">'booleanProperty'</span> ); <span class="hljs-comment">// Check box</span>
|
|
gui.<span class="hljs-keyword">add</span>( object, <span class="hljs-string">'functionProperty'</span> ); <span class="hljs-comment">// Button</span></code></pre>
|
|
<h3 id="limits">Limits</h3>
|
|
<p>You can specify limits on numbers. A number with a min and max value becomes a slider.</p>
|
|
<pre><code class="lang-javascript"><span class="hljs-keyword">gui</span>.<span class="hljs-built_in">add</span>( text, <span class="hljs-string">'growthSpeed'</span>, -<span class="hljs-number">5</span>, <span class="hljs-number">5</span> ); // Min <span class="hljs-built_in">and</span> <span class="hljs-built_in">max</span>
|
|
<span class="hljs-keyword">gui</span>.<span class="hljs-built_in">add</span>( text, <span class="hljs-string">'noiseStrength'</span> ).step( <span class="hljs-number">5</span> ); // Increment amount
|
|
<span class="hljs-keyword">gui</span>.<span class="hljs-built_in">add</span>( text, <span class="hljs-string">'maxSize'</span> ).<span class="hljs-built_in">min</span>( <span class="hljs-number">0</span> ).step( <span class="hljs-number">0.25</span> ); // Mix <span class="hljs-built_in">and</span> <span class="hljs-built_in">match</span></code></pre>
|
|
<p>You can also provide a list of accepted values for a dropdown.</p>
|
|
<pre><code class="lang-javascript"><span class="hljs-comment">// Choose from accepted values</span>
|
|
gui.add( text, <span class="hljs-string">'message'</span>, [ <span class="hljs-string">'pizza'</span>, <span class="hljs-string">'chrome'</span>, <span class="hljs-string">'hooray'</span> ] );
|
|
|
|
<span class="hljs-comment">// Choose from named values</span>
|
|
gui.add( text, <span class="hljs-string">'speed'</span>, { <span class="hljs-string">Stopped:</span> <span class="hljs-number">0</span>, <span class="hljs-string">Slow:</span> <span class="hljs-number">0.1</span>, <span class="hljs-string">Fast:</span> <span class="hljs-number">5</span> } );</code></pre>
|
|
<h3 id="colors">Colors</h3>
|
|
<p>dat-gui has a color selector and understands many different representations of color. The following creates color controllers for color variables of different formats.</p>
|
|
<pre><code class="lang-javascript"><span class="hljs-keyword">text</span>.color0 = <span class="hljs-string">"#ffae23"</span>; <span class="hljs-comment">// CSS string</span>
|
|
<span class="hljs-keyword">text</span>.color1 = [ <span class="hljs-number">0</span>, <span class="hljs-number">128</span>, <span class="hljs-number">255</span> ]; <span class="hljs-comment">// RGB array</span>
|
|
<span class="hljs-keyword">text</span>.color2 = [ <span class="hljs-number">0</span>, <span class="hljs-number">128</span>, <span class="hljs-number">255</span>, <span class="hljs-number">0.3</span> ]; <span class="hljs-comment">// RGB with alpha</span>
|
|
<span class="hljs-keyword">text</span>.color3 = { h: <span class="hljs-number">350</span>, s: <span class="hljs-number">0.9</span>, v: <span class="hljs-number">0.3</span> }; <span class="hljs-comment">// Hue, saturation, value</span>
|
|
|
|
var gui = new Gui();
|
|
|
|
gui.addColor(<span class="hljs-keyword">text</span>, <span class="hljs-string">'color0'</span>);
|
|
gui.addColor(<span class="hljs-keyword">text</span>, <span class="hljs-string">'color1'</span>);
|
|
gui.addColor(<span class="hljs-keyword">text</span>, <span class="hljs-string">'color2'</span>);
|
|
gui.addColor(<span class="hljs-keyword">text</span>, <span class="hljs-string">'color3'</span>);</code></pre>
|
|
<h3 id="events">Events</h3>
|
|
<p>You can listen for events on individual controllers using an event listener syntax.</p>
|
|
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> controller = gui.add(fizzyText, <span class="hljs-string">'maxSize'</span>, <span class="hljs-number">0</span>, <span class="hljs-number">10</span>);
|
|
|
|
controller.onChange(<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(value)</span> </span>{
|
|
<span class="hljs-comment">// Fires on every change, drag, keypress, etc.</span>
|
|
});
|
|
|
|
controller.onFinishChange(<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(value)</span> </span>{
|
|
<span class="hljs-comment">// Fires when a controller loses focus.</span>
|
|
alert(<span class="hljs-string">"The new value is "</span> + value);
|
|
});</code></pre>
|
|
<h3 id="folders">Folders</h3>
|
|
<p>You can nest as many dat-gui as you want. Nested dat-gui act as collapsible folders.</p>
|
|
<pre><code class="lang-javascript">var <span class="hljs-keyword">gui</span> = <span class="hljs-keyword">new</span> Gui();
|
|
|
|
var f1 = <span class="hljs-keyword">gui</span>.addFolder(<span class="hljs-string">'Flow Field'</span>);
|
|
f1.<span class="hljs-built_in">add</span>(text, <span class="hljs-string">'speed'</span>);
|
|
f1.<span class="hljs-built_in">add</span>(text, <span class="hljs-string">'noiseStrength'</span>);
|
|
|
|
var f2 = <span class="hljs-keyword">gui</span>.addFolder(<span class="hljs-string">'Letters'</span>);
|
|
f2.<span class="hljs-built_in">add</span>(text, <span class="hljs-string">'growthSpeed'</span>);
|
|
f2.<span class="hljs-built_in">add</span>(text, <span class="hljs-string">'maxSize'</span>);
|
|
|
|
f2.<span class="hljs-keyword">open</span>();</code></pre>
|
|
<p>The comment method adds a tooltip to a controller.</p>
|
|
<pre><code class="lang-javascript">f2.add(text, <span class="hljs-attribute">'message</span>').comment( <span class="hljs-attribute">'This</span> <span class="hljs-keyword">is</span> the comment.' );</code></pre>
|
|
<h3 id="saving">Saving</h3>
|
|
<p>Add a save menu to the interface by calling <code>gui.remember</code> on all the objects you've added to the Gui.</p>
|
|
<pre><code class="lang-javascript">var fizzyText = new FizzyText();
|
|
var gui = new Gui();
|
|
|
|
gui.remember(fizzyText);
|
|
|
|
// Add controllers <span class="hljs-keyword">...</span></code></pre>
|
|
<p>Click the gear icon to change your save settings. You can either save your dat-gui values to localStorage, or by copying and pasting a JSON object into your source code as follows:</p>
|
|
<pre><code class="lang-javascript">var fizzyText = new FizzyText();
|
|
var gui = new Gui( { load: JSON } );
|
|
|
|
gui.remember( fizzyText );
|
|
|
|
// Add controllers <span class="hljs-keyword">...</span></code></pre>
|
|
<h3 id="presets">Presets</h3>
|
|
<p>The save menu also allows you to save all of your settings as presets. Click Save to modify the current preset, or New to create a new preset from existing settings. Clicking Revert will clear all unsaved changes to the current preset.</p>
|
|
<p>Switch between presets using the dropdown in the save menu. You can specify the default like this:</p>
|
|
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> gui = <span class="hljs-keyword">new</span> Gui({
|
|
load: <span class="hljs-built_in">JSON</span>,
|
|
preset: <span class="hljs-string">'Flow'</span>
|
|
});</code></pre>
|
|
<p>A word of caution about localStorage:</p>
|
|
<p>Paste the JSON save object into your source frequently. Using localStorage to save presets can make you faster, but its easy to lose your settings by clearing browsing data, changing browsers, or even by changing the URL of the page you're working on.</p>
|
|
<h3 id="save-to-disk">Save to Disk</h3>
|
|
<p>dat-gui comes with a node server that saves your settings to disk. This way you don't have to worry about losing your values to local storage or copying and pasting a JSON string.</p>
|
|
<p>First, run the node script:</p>
|
|
<pre><code class="lang-sh"><span class="hljs-variable">$ </span>node gui-server</code></pre>
|
|
<p>Next, instantiate your Gui with a path to a JSON file to store settings. dat-gui will read from this file on load and write to this file on change.</p>
|
|
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> gui = <span class="hljs-keyword">new</span> Gui( { load: <span class="hljs-string">'path/to/file.json'</span> } );</code></pre>
|
|
<h3 id="custom-placement">Custom Placement</h3>
|
|
<p>By default, Gui panels are created with fixed position, and are automatically appended to the body. You can change this behavior by setting the <code>autoPlace</code> parameter to <code>false</code>.</p>
|
|
<pre><code class="lang-javascript"><span class="hljs-reserved">var</span> gui = <span class="hljs-keyword">new</span> Gui( { <span class="hljs-attribute">autoPlace</span>: <span class="hljs-literal">false</span> } );
|
|
|
|
<span class="hljs-reserved">var</span> customContainer = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'my-gui-container'</span>);
|
|
customContainer.appendChild(gui.domElement);</code></pre>
|
|
<p>Since dat-gui is built using <a href="todo">Web Components</a>, you can also use HTML syntax to add controllers to the page.</p>
|
|
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-title">body</span>></span>
|
|
|
|
<span class="hljs-tag"><<span class="hljs-title">dat-gui-number</span> <span class="hljs-attribute">min</span>=<span class="hljs-value">"-2"</span> <span class="hljs-attribute">max</span>=<span class="hljs-value">"2"</span> <span class="hljs-attribute">step</span>=<span class="hljs-value">"1"</span> <span class="hljs-attribute">value</span>=<span class="hljs-value">"0"</span>></span><span class="hljs-tag"></<span class="hljs-title">dat-gui-number</span>></span>
|
|
|
|
<span class="hljs-tag"><<span class="hljs-title">script</span>></span><span class="javascript">
|
|
|
|
<span class="hljs-keyword">var</span> controller = <span class="hljs-built_in">document</span>.querySelector( <span class="hljs-string">'dat-gui-number'</span> );
|
|
controller.onChange( <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">()</span> </span>{
|
|
|
|
<span class="hljs-comment">// react to UI changes ...</span>
|
|
|
|
} );
|
|
|
|
</span><span class="hljs-tag"></<span class="hljs-title">script</span>></span>
|
|
|
|
<span class="hljs-tag"></<span class="hljs-title">body</span>></span></code></pre>
|
|
<h3 id="defining-custom-controllers">Defining Custom Controllers</h3>
|
|
<p>dat-gui uses <a href="todo">Polymer</a> under the hood to define custom elements. A dat-gui controller is just a <a href="todo">Polymer element</a> with two important requirements:</p>
|
|
<ul>
|
|
<li>Controllers must extend <code><dat-gui-base></code>.</li>
|
|
<li>Controllers must be associated with a data type.</li>
|
|
</ul>
|
|
<p>Take for example this (simplified) source for dat-gui's <code><dat-gui-number></code>:</p>
|
|
<pre><code class="lang-javascript">Polymer( <span class="hljs-string">'dat-gui-number'</span>, {
|
|
|
|
// Define element <span class="hljs-keyword">...</span>
|
|
|
|
} );
|
|
|
|
Gui.register( <span class="hljs-string">'dat-gui-number'</span>, <span class="hljs-keyword">function</span>( value ) {
|
|
|
|
<span class="hljs-keyword">return</span> typeof value == <span class="hljs-string">'number'</span>;
|
|
|
|
} );</code></pre>
|
|
<p><code>Gui.register</code> takes an element name and a test function. The test function tells dat-gui to add a <code><dat-gui-number></code> to the panel when the user adds a variable whose type is <code>'number'</code>.</p>
|
|
<p>A test function determines if a controller is appropriate for a given value. This example registers <code><vector-controller></code> for values that have properties <code>x</code>, <code>y</code> and <code>z</code>.</p>
|
|
<pre><code class="lang-javascript">Gui.register( <span class="hljs-string">'vector-controller'</span>, function( <span class="hljs-keyword">value</span> ) {
|
|
|
|
<span class="hljs-keyword">return</span> <span class="hljs-keyword">value</span>.hasOwnProperty( <span class="hljs-string">'x'</span> ) &&
|
|
<span class="hljs-keyword">value</span>.hasOwnProperty( <span class="hljs-string">'y'</span> ) &&
|
|
<span class="hljs-keyword">value</span>.hasOwnProperty( <span class="hljs-string">'z'</span> );
|
|
|
|
} );</code></pre>
|
|
<h3 id="publishing-custom-controllers">Publishing Custom Controllers</h3>
|
|
<p>You should use <a href="todo">Bower</a> and format your plugin all nice and it should have a certain prefix yada yada.</p>
|
|
<p>Installing third-party controllers ... </p>
|
|
<pre><code class="lang-sh"><span class="hljs-variable">$ </span>bower install gui-three</code></pre>
|
|
<p>Include the source for the third-party controllers after dat-gui.</p>
|
|
<pre><code class="lang-html"><span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"gui.js"</span>></span><span class="javascript"></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span>
|
|
<span class="hljs-tag"><<span class="hljs-title">script</span> <span class="hljs-attribute">src</span>=<span class="hljs-value">"gui-three.js"</span>></span><span class="javascript"></span><span class="hljs-tag"></<span class="hljs-title">script</span>></span></code></pre>
|
|
|
|
</div>
|
|
|
|
<script src="docs/main.js"></script>
|
|
<script src="docs/examples.js"></script>
|
|
|
|
</body>
|
|
</html>
|