p5.button.js

Download: p5.button.min.js


Hello!
My name is Nicolas Atek and I am a hobbyist programmer.

p5.button.js is a one function library that will help you create buttons for your p5.js sketch.

Although p5.js library got the ability to use dom objects like buttons, sliders, ...
p5.button.js is suited for small interfaces directly rendered in p5 canvas.

Let me show you a simple exemple:


The button() function first check the state of a button then render it according to its state.

Button state properties are:
  • down: continously check the pression on the button.
  • pressed: one time check when the button is pressed.
  • released: one time check when the button is released.
  • clicked: one time check when the button is released inside its frame.
  • on_pressed: let you define a function for when a button is pressed.
  • on_released: let you define a function for when a button is released.
  • on_clicked: let you define a function for when a button is clicked.


Button base properties are:
  • x, y, w, h, text
  • angle: in degrees
  • icon: a p5 image ( smaller than the button size that will replace the text )
  • origin_x, origin_y: center x and y values ( 0: left / top, 1: right / bottom)
    you can also use string format to set relative coordinates ( '-100' )
  • offset_x, offset_y: translation x and y values
As for position, size and text, you may want to modify the style of a button:

Button style properties are:
  • color: CSS color string format ( 'rgb(255,0,0)', 'black', '#0f0', ... )
  • hover_color: CSS color string format ( empty string to disable )
  • pressed_color: CSS color string format ( empty string to disable )
  • corner_radius
  • corners: array of 4 corner radius
  • pattern: a drawingContext pattern object
  • pattern_fixed: true to align the pattern to the object position
  • border_color: CSS color string format
  • border_hover_color: CSS color string format ( empty string to disable )
  • border_thickness
  • shadow: { blur, color, offset_x, offset_y }
  • hover_shadow: { blur, color, offset_x, offset_y }
  • shading: value between 0 ( no shading ) and 1 ( full shading )
  • text_color: CSS color string format
  • text_hover_color: CSS color string format ( empty string to disable )
  • text_shadow: { blur, color, offset_x, offset_y }
  • text_size
  • text_margin
  • text_offset: { x, y }
  • text_align: 'LEFT', 'RIGHT' or 'CENTER'
  • font: browser safe font ( 'Comic Sans MS', 'Arial Black', ... ) or p5 font
  • icon_color: CSS color string format
  • icon_hover_color: CSS color string format ( empty string to disable )


You can also cretate a style object and assign it to any button.style property.

A style object share the exact same style properties of a button.

In combination with the button.style property, you can use the button.exclude propety which is an array containing string names of the properties you want to exclude.

So you can set full theming for all your buttons but keep specific aspects for each one.

There are also style.on_pressed and style.on_released properties which may be useful
to define functions that act for all buttons sharing this style.
*Note that those functions will let you modify button properties where style not overrides.


Hope, you will enjoy this library which will be certainly part of a bigger one.