Skip to content

Instantly share code, notes, and snippets.

@lizzybrooks
Created November 3, 2017 20:41
Show Gist options
  • Save lizzybrooks/54045563e4e8321718cc40297db999f9 to your computer and use it in GitHub Desktop.
Save lizzybrooks/54045563e4e8321718cc40297db999f9 to your computer and use it in GitHub Desktop.
How to make a button that is an image in p5.js
// To use an image as a button, replace the createButton() function with createImage() and attach the same functionality to the image as you would to the button.
// You must include the dom library in this sketch.
// This is adapted from the button example in the p5 dom library. https://p5js.org/reference/#/p5/createButton
var button;
function setup() {
createCanvas(100, 100);
background(0);
button = createImg('mod.png');
button.position(19, 19);
button.mousePressed(changeBG);
}
function changeBG() {
var val = random(255);
background(val);
}
@mollthecoder
Copy link

Cool! Thanks for posting!

@ManavNayak
Copy link

How to scale this button to correct size????

@mollthecoder
Copy link

How to scale this button to correct size????

button.width = (Width in pixels)
button.height = (Height in pixels)
@ManavNayak

@Underscorer-tech
Copy link

I am unable to correct the size of the button plz help

@hoanganh100k
Copy link

button.size();

How to scale this button to correct size????

@saleems11
Copy link

// size(width, height), just one of them could be AUTO, or you can set them to numbers as pixels.
// For more info https://p5js.org/reference/#/p5.Element/size
button.size(AUTO, this.height);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment