Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance decrease after forEachLayerAtPixel function calling #10597

Closed
Shekenix opened this issue Jan 31, 2020 · 4 comments
Closed

Performance decrease after forEachLayerAtPixel function calling #10597

Shekenix opened this issue Jan 31, 2020 · 4 comments
Labels

Comments

@Shekenix
Copy link

Shekenix commented Jan 31, 2020

Description
Perormance decreasing when clicking on ImageLayer and getting color by forEachLayerAtPixel function.
The problem mainly appears in full-screen mode in.
Tested on Chrome.

To Reproduce

  1. See my testing video to check how to reproduce issue (look on CPU - procesor usage before and after calling forEachLayerAtPixel function. Chrome on the list of tasks) https://radar-opadow.pl/foreachlayeratpixel-issue.mp4
  2. Go to https://codesandbox.io/s/angry-shtern-b8mm1 to check source code
  3. Click on fullscreen page mode in codesandbox.io or go to https://b8mm1.csb.app to reproduce issue

This example only contains one ImageLayer, but my original application has many ImageLayers and the problem is more serious.

Thank you, in advance, for your support and reply.
Best regards.

@stale
Copy link

stale bot commented Apr 2, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Apr 2, 2020
@Shekenix
Copy link
Author

Shekenix commented Apr 3, 2020

I found solution for this issue getting advanced research.

Find the following file:

ol/renderer/canvas/Layer.js

In the function CanvasLayerRenderer.prototype.getDataAtPixel you get image data by whole context. This solution decrease performance due to scanning of all pixels. To achieve a better performance, first you should crop canvas to 1px image (drawing new 1px image) and finally get that pixel using getDataImage.

I have checked this solution and received much better performance result.

This is my working example:

 CanvasLayerRenderer.prototype.getDataAtPixel = function (pixel, frameState, hitTolerance) {
        var renderPixel = applyTransform(this.inversePixelTransform, pixel.slice());
        var context = this.context;
        var data;
        try {
            var x = Math.round(renderPixel[0]);
            var y = Math.round(renderPixel[1]);
            var newCanvas = document.createElement("canvas");
            var newContext = newCanvas.getContext('2d');
            newCanvas.width = 1;
            newCanvas.height = 1;
            newContext.clearRect(0, 0, 1, 1);
            newContext.drawImage(context.canvas, x, y, 1, 1, 0, 0, 1, 1);
            data = newContext.getImageData(0, 0, 1, 1).data;
            //data = context.getImageData(Math.round(renderPixel[0]), Math.round(renderPixel[1]), 1, 1).data;
        }
        catch (err) {
            if (err.name === 'SecurityError') {
                // tainted canvas, we assume there is data at the given pixel (although there might not be)
                return new Uint8Array();
            }
            return data;
        }
        if (data[3] === 0) {
            return null;
        }
        return data;
    };

Check this solution and please, improve this function.

Regards.

@stale stale bot removed the stale label Apr 3, 2020
@stale
Copy link

stale bot commented Jun 4, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Jun 4, 2020
@stale stale bot closed this as completed Jun 11, 2020
@Shekenix
Copy link
Author

Shekenix commented Jul 3, 2020

Why didn't you improve this feature. It could help many people?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant