A friendly guide on how to integrate p5.js sketches into a website
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Why is it useful?
- p5.js is a JavaScript library, created for beginners and people exploring creative code and digital interaction.
- So, it's basically JS, but EASIER!
- The good news is that our cools sketches don't need to be confined to a canvas in the online editorโฆ
- โฆ it can be seamlessly incorporated into our websites, just like any JS script!
- Here's a short tutorial on how to achieve this and bring more interaction to your webpages using 2 different methods!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
This is the online p5.js editor
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
1 - The < iframe > method
An iframe is basically a window into a nested page within a page, you just need to insert a URL (in this case, your p5.js sketch) as the source of the iframe tag in your HTML file.
Example
Styling (could go directly to the css file)
Results
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
pros
- Easy and straightforward
- Good for a blog post
- Good for multiple sketches
cons
- The editor interface will still be visible
- not very responsive
- manually set the size of the iframe in pixels (although there are some advanced hacks to bypass that, as shown below:)
2 - The local file method
Working within the web editor is convenient, but limited. In order to add our sketch to our own pages, we have to go through a few steps.
Firstly, we need to create another .js file in our website folder to contain our sketch.
After that, we need to include a link to this script to our main HTML file, and then connect it to the p5.js library.
For now, I recommend using the CDN, which just involves pasting the following line inside the < head >
Example
Styling (could go directly to the css file)
Results
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
https://kevinworkman.com/
pros
- More styling and positioning options
- Great for using the p5.js sketch as the background
- good for seamless integration
cons
- Lenghtier process
- Requires a lot of CSS to fine-tune
Useful p5.js tips and tricks
Resizing Canvas on window resize
Advanced (maintains aspect ratio):
Modifying the DOM
When combining both p5.js and HTML, we could create interactions that modify the DOM elements. Example: clicking generates a new < p > tag with text.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
https://p5js.org/examples/dom-input-and-button.html
Thank you!
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ