Skip to content
This repository was archived by the owner on Sep 21, 2022. It is now read-only.

Commit 0f8b2c7

Browse files
fix: ignoreElements fail if outside viewport
1 parent 082442a commit 0f8b2c7

2 files changed

Lines changed: 71 additions & 9 deletions

File tree

lib/capture-session/viewport.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,28 @@ module.exports = class Viewport {
2020
}
2121

2222
ignoreAreas(areas) {
23-
areas.forEach((area) => {
24-
this._image.clear(this._transformToViewportOrigin(area), {scaleFactor: this._pixelRatio});
25-
});
23+
_(areas)
24+
.map((area) => this._getIntersectionWithViewport(area))
25+
.compact()
26+
.forEach((area) => this._image.clear(this._transformToViewportOrigin(area), {scaleFactor: this._pixelRatio}));
2627
}
2728

2829
crop(captureArea) {
2930
return this._image.crop(this._transformToViewportOrigin(captureArea), {scaleFactor: this._pixelRatio});
3031
}
3132

33+
_getIntersectionWithViewport(area) {
34+
const top = Math.max(this._viewport.top, area.top);
35+
const bottom = Math.min(getAreaBottom(this._viewport), getAreaBottom(area));
36+
const left = Math.max(this._viewport.left, area.left);
37+
const right = Math.min(getAreaRight(this._viewport), getAreaRight(area));
38+
39+
if (left >= right || top >= bottom) {
40+
return null;
41+
}
42+
return {top, left, width: right - left, height: bottom - top};
43+
}
44+
3245
_transformToViewportOrigin(area) {
3346
return _.extend({}, area, {
3447
top: area.top - this._viewport.top,
@@ -59,3 +72,11 @@ module.exports = class Viewport {
5972
return captureArea.height - this._viewport.height;
6073
}
6174
};
75+
76+
function getAreaBottom(area) {
77+
return area.top + area.height;
78+
}
79+
80+
function getAreaRight(area) {
81+
return area.left + area.width;
82+
}

test/unit/capture-session/viewport.js

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,60 @@ describe('Viewport', () => {
5454
beforeEach(() => image = sinon.createStubInstance(Image));
5555

5656
it('should ignore passed areas', () => {
57-
const viewport = createViewport({image, pixelRatio: 1});
57+
const viewport = createViewport({coords: {top: 0, left: 0, width: 20, height: 20}, image, pixelRatio: 1});
5858

59-
viewport.ignoreAreas([{top: 1, left: 1}]);
59+
viewport.ignoreAreas([{top: 1, left: 1, width: 10, height: 10}]);
6060

61-
assert.calledWith(image.clear, {top: 1, left: 1}, {scaleFactor: 1});
61+
assert.calledWith(image.clear, {top: 1, left: 1, width: 10, height: 10}, {scaleFactor: 1});
6262
});
6363

6464
it('should transform area coordinates to a viewport origin', () => {
65-
const viewport = createViewport({coords: {top: 1, left: 1}, image});
65+
const viewport = createViewport({coords: {top: 1, left: 1, width: 20, height: 20}, image});
66+
67+
viewport.ignoreAreas([{top: 1, left: 1, width: 10, height: 10}]);
68+
69+
assert.calledWith(image.clear, {top: 0, left: 0, width: 10, height: 10});
70+
});
71+
72+
it('should crop area size to a viewport origin (inside)', () => {
73+
const viewport = createViewport({coords: {top: 0, left: 0, width: 30, height: 20}, image});
74+
75+
const area = {top: 10, left: 5, width: 20, height: 5};
76+
viewport.ignoreAreas([area]);
77+
78+
assert.calledWith(image.clear, area);
79+
});
80+
81+
it('should crop area size to a viewport origin (bottom right)', () => {
82+
const viewport = createViewport({coords: {top: 0, left: 0, width: 30, height: 20}, image});
83+
84+
viewport.ignoreAreas([{top: 10, left: 5, width: 30, height: 5}]);
85+
86+
assert.calledWith(image.clear, {top: 10, left: 5, width: 25, height: 5});
87+
});
88+
89+
it('should crop area size to a viewport origin (top left)', () => {
90+
const viewport = createViewport({coords: {top: 20, left: 15, width: 30, height: 20}, image});
91+
92+
viewport.ignoreAreas([{top: 10, left: 5, width: 20, height: 20}]);
93+
94+
assert.calledWith(image.clear, {top: 0, left: 0, width: 10, height: 10});
95+
});
96+
97+
it('should not clear image if area is outside of viewport (bottom right)', () => {
98+
const viewport = createViewport({coords: {top: 0, left: 0, width: 30, height: 20}, image});
99+
100+
viewport.ignoreAreas([{top: 21, left: 31, width: 30, height: 5}]);
101+
102+
assert.notCalled(image.clear);
103+
});
104+
105+
it('should not clear image if area is outside of viewport (top left)', () => {
106+
const viewport = createViewport({coords: {top: 21, left: 31, width: 30, height: 20}, image});
66107

67-
viewport.ignoreAreas([{top: 1, left: 1}]);
108+
viewport.ignoreAreas([{top: 0, left: 0, width: 30, height: 20}]);
68109

69-
assert.calledWith(image.clear, {top: 0, left: 0});
110+
assert.notCalled(image.clear);
70111
});
71112
});
72113

0 commit comments

Comments
 (0)