processing / processing/p5.js

Allow `width` and `height` parameters in loadImage to support proper SVG scaling.

Open
#5,779 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Area:Image Enhancement
Dominant language
JavaScript
Stars
24k
Forks
3.8k
Avg merge
3d 16h
Merged PRs (30d)
25

Description

Increasing Access

Allowing to use an SVG image as source for a canvas image leads to crisper images when a user scales the canvas up.

Most appropriate sub-area of p5.js?
  • Image
Feature enhancement details

The method p5.loadImage currently loads the given image in its inherent size. The underlying function drawImage actually allows drawing SVG files smoothly in any size, independent of resolution. It takes width and height arguments to do this. A quick way to make use of this would be to add these arguments to the signature of loadImage and pass them on to the call of drawImage, should they be given. If they are undefined then loadImage can still default to the inherent width and height.

Here is a proof of concept for implementing this:

p5.prototype.loadImage = function(
  path,
  successCallback,
  failureCallback,
  options
) {
  ...
    ...
      } else {
        // Non-GIF Section
        const img = new Image();

        img.onload = () => {
          pImg.width = pImg.canvas.width = options.width
            ? options.width
            : img.width;
          pImg.height = pImg.canvas.height = options.height
            ? options.height
            : img.height;

          // Draw the image into the backing canvas of the p5.Image
          pImg.drawingContext.drawImage(img, 0, 0, pImg.width, pImg.height);
          pImg.modified = true;
          if (typeof successCallback === 'function') {
            successCallback(pImg);
          }
          self._decrementPreload();
        };
        ...
      ...
    ...
  ...
  return pImg;
};

A possible use case would be a presentation with many graphics that should scale from small to large screens. Being able to use SVG files as templates for canvas bitmaps of appropriate size makes scaling presentations much easier, without the performance penalty of actually working with SVG elements.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the p5.loadImage entry point and running the existing image-loading tests. Check how SVG and non-SVG images are currently sized, then verify that optional width and height values produce the requested canvas dimensions while omitted values preserve inherent sizing.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
computer-graphics
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.