Git Product home page Git Product logo

p5.gui's Introduction

p5.gui

screenshot of p5.gui

p5.gui magically generates a graphical user interface (sliders, color selector, etc) for each of your variables. Behind the scenes it uses other libraries such as Quicksettings (and in the future also DAT.GUI) to do all the hard work.

You currently need to include both p5.gui.js and quicksettings.js in your p5.js sketch.

Usage

Explore the examples for how to use it ...

Add Global Variables

Create your variables

let myNumber = 100;
let myColor = color(255, 0, 0);
let myChoice = ['one', 'two', 'three'];

Create a new GUI with a label

var gui = createGui('My awesome GUI');

Add gui elements for your variables:

gui.addGlobals('myColor', 'myNumber', 'myChoice');

p5.gui inspects the type of your variables and magically displays the corresponding GUI elements.

An example can be found here.

Use Magic Variables to control individual sliders

Once you have created a variable called myNumber you can control the details of the slider like this:

let myNumber = 100;
let myNumberMin = 0;
let myNumberMax = 1000;
let myNumberStep = 10;
gui.addGlobals('myNumber');

p5.gui will magically pick up variables ending in Min, Max and Step to control the appearance of the slider.

See here for an example.

Use sliderRange() to control slider creation

If you want explicitly control the range of a couple of sliders you can also use the sliderRange(min, max, step) command.

This will set the range for all future calls to p5.gui.

let a = 100;
let b = 120;
let c = 120;
sliderRange(0, 1000, 10);
gui.addGlobals('a', 'b', 'c');

See here and here for an example.

Pass params as objects

If you want to keep all your parameters in a single place, you can wrap them into an object like this:

let params = {
	myNumber: 100,
	myColor: [255, 0, 0],
	myChoice: ['one', 'two', 'three'];
};

gui.addObject(params);

Slider Magic works just as with global variables:

let params = {
	myNumber: 100,
	myNumbeMin: 0,
	myNumbeMax: 1000,
	myNumbeStep: 10
};

See here for an example.

Pass your sketch in instance mode

If you want to run your processing sketch in instance mode, you need to pass your sketch to the createGui function. Here's a simple example:

let sketch = function(p) {

	let div;

	let params = {
		r: 500
	};

	p.setup = function() {
		div = p.canvas.parentElement;
		p.createCanvas(div.clientWidth, div.clientHeight);
		gui = p.createGui(this);
		gui.addObject(params);
	};

	p.draw = function() {
		p.background(220);
		p.ellipse(p.width/2, p.height/2, params.r, params.r);
	};

	p.windowResized = function() {
		p.resizeCanvas(div.clientWidth, div.clientHeight);
	};

}

new p5(sketch, 'sketch1');
new p5(sketch, 'sketch2');
new p5(sketch, 'sketch3');
new p5(sketch, 'sketch4');

You can find this example here.

One sketch, many guis

You can just create several guis, and position them individually:

let gui1 = p.createGui('My 1st GUI');
gui1.moveTo(50, 50);
gui1.addGlobals('a', 'b', 'c');

let gui2 = p.createGui('My 2nd GUI');
gui2.moveTo(windowWidth - 50, 50);
gui2.addGlobals('e', 'f', 'g');

See here for an example.

Many sketches, many guis

When using Instance Mode (see above) you can can easily create several sketches, or versions of a single sketch.

See here for an example.

Color Modes

You can use the colorMode() function to change the default color mode used to interpret colors when creating the GUI.

Examples

Links

Licensing

logo of p5.gui

p5.gui is licensed under the MIT License.

This repo also includes code from other libraries:

p5.gui's People

Contributors

audiocommander avatar craftoid avatar gregberger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

p5.gui's Issues

gui opens but does not show elements

for some reason after i createGui with a just one regular float variable the GUI header appears but the element doesn't show. attaching a screenshot.

Screenshot 2019-08-31 at 7 17 25 AM

Detect mouseover

Is there any way to detect if the mouse is over the interface?
Would be handy for example when coding a painting/brush with settings using p5.gui.
mousePressed doesn't seem to work on gui object.

Adding CDN script tag to README.md?

Will that adding the CDN script url or script tag to README will make people get started sooner or more convenient to start using the library?

e.g.

https://cdn.rawgit.com/bitcraftlab/p5.gui/master/libraries/p5.gui.js
https://cdn.rawgit.com/bit101/quicksettings/master/quicksettings.js

or

<script src="https://cdn.rawgit.com/bitcraftlab/p5.gui/master/libraries/p5.gui.js"></script>
<script src="https://cdn.rawgit.com/bit101/quicksettings/master/quicksettings.js"></script>

then people could add those code into some more simpler IDE, like editor.p5js.org or OpenProcessing?

add setPosition to Quicksettings interface

It would be nice to have methods to set the position of GUI panels. I checked quicksettings.js and it has such a method available.

Would you agree this would make a nice addition?
If so I could try to find some time to implement and create a PR.

ReferenceError: QuickSettings is not defined

Hello, im having a problem with quicksettings.

I keep getting this error:

ReferenceError: QuickSettings is not defined
at p5.gui.js:132
at p5.gui.js:207

code:
gui = createGui("stats"); gui.addGlobals("count", "generation"); noLoop();

When creating only the gui, everything works fine. But when adding globals the error comes up.

Add button element to the GUI interface

It would be quite useful to have a button element that can be included in the GUI panel interface. There's a couple use cases where a button in the GUI would make more sense than a checkbox. The dat.gui basic example has a good example of the feature labeled "explode".

Creating a button in the GUI

Hey,

I've tried everything I can think of to create a button in the GUI. I see that quicksettings does support buttons, so is there a way to add one to the gui?

Thanks!

Can I add to functions to the library? To add addButton()

Hey all,

Is there a way to add custom code to the library? I've added the following code to QSGui() and that works perfectly. Now, I don't want to hard code it in the library itself. Is there a way to to append to the library?

       this.addButton = function(name, callback) {
            qs.addButton( name, callback );
        };

Or even to add in this line in the new version?

Example code to position a gui

39DEEEA5-B39C-48A1-B81B-2CE257B14F91
I am using p5.gui with QuickSettings in many of my OpenProcessing sketches. If you are interested, look for sketches 743514 and 743509, in which I use p5.GUI sliders to set hue, saturation and brightness for fullscreen gradients. It is a great timesaver. I agree with the idea that it would be nice to have a button element included. Up until now I have used checkboxes as buttons, but I am looking forward to using a user solution posted in Issues. I reread the p5.GUI information page several times to find out how to position a GUI, and tried the code suggested there:

let gui1 = p.createGui('My 1st GUI');
gui1.moveTo(50, 50);
gui1.addGlobals('a', 'b', 'c');

It would not work. The console error was:
TypeError: gui.moveTo is not a function. (In 'gui.moveTo(50, 50)', 'gui.moveTo' is undefined)

Then I happened to notice a different positioning method shown in one of the examples.

gui = createGui('p5.GUI').setPosition(width - 250, 120);

That one works perfectly, so I suggest using it, and error-checking the other method.

Color picker button doesn't work in Chrome

Maybe I'm doing something wrong with p5.gui library in this example:

https://editor.p5js.org/Tingler/sketches/qOs-EMeM-

The button to pick a color doesn't seem to work in Chrome desktop version 84.0.4147.125 (Official Build) (64-bit).
Works in current Firefox Developer Edition and both mobile Firefox and mobile Chrome.

Same happens in Chrome for me even with the PacMan CodePen provided in documentation:
https://codepen.io/bitcraftlab/pen/GNKmGg

I asked on p5 discord but others report same issue.

how do i service "change in gui" events?

Could some kind soul help me with this? In a p5.js.gui I would like to tell the gui there is a specific callback to service changes in any of the controls. Is this possible? How do I do it? It would be similar to the following call on Quicksettings:

qs.setGlobalChangeHandler(callback);

However, I don't seem to have access to this after I create a gui with

let gui = createGui('p5.gui');
gui.addObject(glob.ui);
gui.setPosition(20, 60);

I would like to do something like

gui.setGlobalChangeHandler(callback);

Cannot add gui in my program

I am trying to add GUI in my program for some sliders and color options. I tried with the similar code in all sample examples provided. But still, I am unable to add GUI. Please look at my code and try to give me solution ASAP if possible. A big thanks in advance.

My JavaScript code is given below.

`
// GLobal variables

let angle = 0;

let fabric;
let wood;
let floor;

var rotateXAxis = 0;
var rotateYAxis = 0;
var rotateZAxis = 0;

`
// set slider range with magic variables
var rotateXAxisMin = 0;
var rotateXAxisMax = 500;

// set angle range and step with magic variables
var rotateYAxisMin = 0;
var rotateYAxisMax = 500;

// set radius range and step with magic variables
var rotateZAxisMin = 0;
var rotateZAxisMax = 500;
//var radiusStep = 0.1;
`
var visible = true;
var gui;

function setup() {
createCanvas(1520, 640, WEBGL);

sliderRange(0, 90, 1);
gui = createGui('Function Rotate');
gui.addGlobals('myColor', 'myAngle');

noLoop();

}

function preload() {
fabric = loadImage('texture/royal_blue.jpg');
wood = loadImage('texture/wood.jpg');
floor = loadImage('texture/floor2.jpg');
}

function draw() {

clear();

background(50);

var t = translateSlider.value(); var r = rotateSlider.value(); var sc = scaleSlider.value(); var sh = sHearSlider.value();
//noStroke(); ..... I think its useful

fill(0, 102, 0);

//let camX = map(0, 0, width, -100, 0);
//let camY = map(0, 0, height, -100, 0);
//camera(0, 500, 300, 0, 1, 0, 0, 1, 0);

let camX = map(mouseX, 0, width, -1000, 0);
let camY = map(mouseY, 0, height, -1000, 0);
camera(0, -800, 500  , 0, 0, 0, 0, -1, 0);

//orbitControl();

//ambientLight(227, 232, 239);

//var dirX = (mouseX / width - 0.5) * 2;
//var dirY = (mouseY / height - 0.5) * 2;
ambientLight(200, 200, 200, 0, 100, 0);

translate(0, 0, -300);
texture(floor); 
//ambientMaterial(125);
plane(2000, 1500);

texture(fabric);
translate(0, 0, 300);
box(400, 250, 30);          //base green

//fill(77, 38, 0);
texture(wood);

translate(0, 125, 0);		//side long top 
box(425, 25, 50);			

translate(0, -250, 0);		//side long bottom
box(425, 25, 50);			

translate(200, 125, 0);		//side short right
box(25, 250, 50);

translate(-400, 0, 0);		//side short left
box(25, 250, 50);

translate(50, 80, -140);	//leg left bottom
box(30, 30, 250);

translate(0, -170, 0);		//leg left top
box(30, 30, 250);

translate(300, 0, 0);		//leg right top
box(30, 30, 250);

translate(0, 180, 0);		//leg left bottom
box(30, 30, 250);



//Snooker Balls

translate(-50, -100, 170);
specularMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(15, 0, 0);
specularMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(35, 0, 0);
specularMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(-10, 20, 0);
specularMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(-15, 10, 0);
specularMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(-15, 30, 0);
specularMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(15, 27, 0);
ambientMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(-15, 20, 0);
specularMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(-20, -20, 0);
specularMaterial(255, 0, 0);
sphere(8, 20, 20);

translate(-30, -20, 0);
specularMaterial(0, 11, 119);
sphere(8, 20, 20);

translate(-50, -40, 0);
specularMaterial(239, 4, 212);
sphere(8, 20, 20);

translate(-50, -30, 0);
specularMaterial(0, 0, 0);
sphere(8, 20, 20);

translate(-80, -30, 0);
specularMaterial(255, 255, 255);
sphere(8, 20, 20);

//Holes

translate(120, -70, -10);
specularMaterial(0, 0, 0);
sphere(12, 20, 20);

translate(0, 225, 0);
specularMaterial(0, 0, 0);
sphere(12, 20, 20);

translate(182, 0, 0);
specularMaterial(0, 0, 0);
sphere(12, 20, 20);

translate(0, -220, 0);
specularMaterial(0, 0, 0);
sphere(12, 20, 20);

translate(-360, 0, 0);
specularMaterial(0, 0, 0);
sphere(12, 20, 20);

translate(0, 220, 0);
specularMaterial(0, 0, 0);
sphere(12, 20, 20);

}

function keyPressed() {
if (keyCode === LEFT_ARROW) {
value = 255;
}
if (keyCode === RIGHT_ARROW) {
value = 0;
}
if (keyCode === UP_ARROW) {
value = 0;
}
if (keyCode === DOWN_ARROW) {
value = 0;
}
}

function windowResized() {
resizeCanvas(1520, 640);
}`

Negative values don't work

If I use a params object like this:

{
  val: -1,
  valMin: -5,
  valMax: 0,
  valStep: 0.1
}

The interface doesn't work properly. The minimum value for the slider works, but not the maximum. The slider goes all the way up to 100 (the default behavior, I believe), not to 0 as I specified.

I might suggest a refactor which internally makes all sliders go from 0 to 1, then implements a remapping function which takes those values and remaps them to the user-specified range. This would solve the issue I mentioned above, but also handle situations where the user provided "min" is greater than the "max". Though not technically correct in a mathematical sense, a user might want bigger values to be on the left side of the slider, and the library might as well support that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.