Skip to content

Performance improvement for tint #3610

Closed
@Ajayneethikannan

Description

@Ajayneethikannan

Nature of issue?

  • Found a bug
  • Existing feature enhancement
  • New feature request

Most appropriate sub-area of p5.js?

  • Core/Environment/Rendering

Which platform were you using when you encountered this?

  • Mobile/Tablet (touch devices)
  • Desktop/Laptop
  • Others (specify if possible) Device independent

Details about the bug:

  • p5.js version: 0.7.3
  • Web browser and version: Browser independent
  • Operating System: OS independent
  • Steps to reproduce this:

https://editor.p5js.org/TheSkepticSniper/sketches/go2nvAoAF
This sketch can be used for checking the performance of tint.
(Uncomment the tint line to see the frame rate drop)

Feature enhancement details:

Currently , when the user sets a tint to draw images , the sketch suffers a huge reduction in performance ,as the function _getTintedCanvas runs every time any image is drawn using the p5.Renderer2d.prototype.image function , manipulating every pixel of the image's canvas and returning a new tinted canvas ( here I think we can make a small speed improvement by using globalCompositeOperation = 'multiply' to create the tinted image, instead of manually multiplying every pixel with the tint ).

When many images are present , or high resolution images are used , this can lead to performance issues.
So curently, the safest place to use tint is only in setup , as the canvas is only drawn once and the tint calculations are done once.

New feature details:

Some ideas which can be implemented are:

1.Caching the previous tinted canvas:

For every p5.Image object, if we are drawing with a specific tint for that image,
we can store the last tinted image canvas, returned by

cnv = this._getTintedImageCanvas(img);

along with the p5.Image instance, as

img._prevTintedImage = cnv;
img._prevTint = tint;

where img refers to a p5.Image instance
In case the img hasn't been modified (which we can check) or the tint hasn't changed, we can reuse this canvas. Or we can calculated the new tinted canvas and store it here.
This method can be used when the image is not modified frequently, or the tint for the particular image is not changed frequently.

(But this method cannot be applied for video elements )

2.Ability to create new tinted image :

if we can add a functionality like

let tImage = img.tint(12, 121, 12);

we can draw the tinted images as new images, without calling _getTintedCanvas every time.

Would love to hear more from the community if this feature should be considered ,
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions