Bridges between modern JavaScript image manipulation and p5.Image (createImageFromBitmap)
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 24k
- Forks
- 3.8k
- Avg merge
- 3d 16h
- Merged PRs (30d)
- 25
Description
Increasing Access
unsure
Most appropriate sub-area of p5.js?
- Accessibility (Web Accessibility)
- Build tools and processes
- Color
- Core/Environment/Rendering
- Data
- DOM
- Events
- Friendly error system
- Image
- IO (Input/Output)
- Localization
- Math
- Unit Testing
- Typography
- Utilities
- WebGL
- Other (specify if possible)
Feature request details
The ImageBitmap interface (https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap) is a more modern way to use images on canvas than manipulating DOM images. It is also usable in web workers.
The implementation is still quite unequal in browsers, with some browsers not supporting some options (chrome support everything)
https://caniuse.com/createimagebitmap and https://developer.mozilla.org/en-US/docs/Web/API/createImageBitmap#browser_compatibility
But this newer interface makes it very easy to turn a downloaded blob or a canvas into a ImageBitmap, and it's also easy to draw them on a canvas.
There's no way that I could find in the current p5.js library to turn a ImageBitmap (or a blob for that matter) into ap5.Image object.
For a generative template that load and prepares images from a web source for generative artist to then use and remix, I needed to fetch the remote images and store them as ImageBitmap (so it can be natively used without p5 by an artist).
But I also needed to store them as p5.Image in the case p5 library was loaded by the generative artist using the template.
Of course I wanted to avoid fetching images twice, once with Fetch => Blob => ImageBitmap , and once with LoadImage which includes a Fetch.
I also wanted to avoid having to have two completely different path for fetching from the web, depending on the library used by artists.
I stripped down loadImage() code to a simple function that just set the p5.Image, so I can pass it an ImageBitmap and get a p5.Image in return.
function createImageFromBitmap(bitmap) {
const pImg = new p5.Image(1, 1, this);
pImg.width = pImg.canvas.width = bitmap.width;
pImg.height = pImg.canvas.height = bitmap.height;
// Draw the image into the backing canvas of the p5.Image
pImg.drawingContext.drawImage(bitmap, 0, 0);
pImg.modified = true;
return pImg;
}
Using this small function, I can have one single fetch path, retrieving the images, and then chose to store them as ImageBitmaps or p5.Images or both.
(It's missing the standard argument verification and error handling of p5 functions. of course.)
I think it would be interesting for p5.js to have better bridges with this relatively recent image manipulation interface in JavaScript, with ways to convert to and from p5.image objects.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the existing p5.Image and loadImage() implementation, then compare the proposed ImageBitmap-to-p5.Image path with the browser createImageBitmap interface. Define the supported conversion scope, argument validation, error handling, and browser expectations before determining what tests are needed for the new bridge.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- computer-graphics, frontend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100