phetsims / natural-selection

"Natural Selection" is an educational simulation in HTML5, by PhET Interactive Simulations
GNU General Public License v3.0
3 stars 7 forks source link

Canvas 2D warning message in Chrome console #355

Closed Nancy-Salpepi closed 1 year ago

Nancy-Salpepi commented 1 year ago

Test device MacBook Air (M1 chip) and Dell

Operating System macOS13.5 and Win10

Browser Chrome 115

Problem description For https://github.com/phetsims/qa/issues/967, a warning message appears in the console on startup for the PhET brand sim and PhET-iO wrappers.

Visuals

Screenshot 2023-08-02 at 1 51 08 PM
pixelzoom commented 1 year ago

Reproduced in master with macOS 13.4.1 + Chrome 115.

This is a warning, informing us that there's a way to improve performance of how we're using the Canvas2D API.

I don't know where this warning is manifesting. Natural Selection uses scenery Sprite, which is supposed to default to webgl, with canvas fallback. The only sim-specific use of Canvas2D that I can find is in BunnySelectionRectangleSprite.ts;

    // Convert to {HTMLCanvasElement}, as required by Sprite
    let selectionRectangleSpriteImage: SpriteImage;
    selectionRectangle.toCanvas( canvas => {
      const offset = new Vector2( selectionRectangleBounds.width / 2, selectionRectangleBounds.height - SELECTION_RECTANGLE_DILATION );
      selectionRectangleSpriteImage = new SpriteImage( canvas, offset, { pickable: false } );
    } );

@jonathanolson Is there something that should be done here that might improve performance? Something in the sim or scenery that should be using willReadFrequently? Or should we just ignore this warning?

jonathanolson commented 1 year ago

We were reading a scratch Canvas multiple times (it was reused) in order to support the hitTestPixels feature (used at least for the bunnies). The Canvas's only use is for that purpose, so I've added the flag so that it should be CPU-side and won't have the warning.

pixelzoom commented 1 year ago

Back to @Nancy-Salpepi to verify in master. You should no longer see this warning in Chrome. Close if OK.

Nancy-Salpepi commented 1 year ago

I no longer see the warning in Chrome. Closing.