Skip to content

Ignore brightness of pixels outside the viewport#119

Open
DanActions wants to merge 2 commits into
kennethcachia:masterfrom
DanActions:fix-onscroll-viewport-check
Open

Ignore brightness of pixels outside the viewport#119
DanActions wants to merge 2 commits into
kennethcachia:masterfrom
DanActions:fix-onscroll-viewport-check

Conversation

@DanActions

Copy link
Copy Markdown

Fixes #57 by ignoring pixels outside of the viewport when calculating brightness.

I think the issue is in calculatePixelBrightness(). The following lines get the dimensions of the target image and fetch those pixels from the canvas:

var dims = target.getBoundingClientRect();
...
data = context.getImageData(dims.left, dims.top, dims.width, dims.height).data;

The function then loops through data pixel-by-pixel to calculate the average brightness.

The problem is that if part of the bounding rectangle is outside the viewport, getImageData will return rgba(0,0,0,0) for those pixels. The formula that calculatePixelBrightness() uses to calculate average brightness doesn't consider the alpha value, causing it to see these pixels as black, thus skewing the calculated brightness to be much darker than the visible image.

The pull request attempts to fix this by trimming off the parts of the bounding rectangle that are outside the viewport before calling getImageData.

@DanActions DanActions force-pushed the fix-onscroll-viewport-check branch from d403521 to 6338e82 Compare February 23, 2022 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Checking Text on Background onscroll - Viewport Problem

1 participant