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

Fix(clip): delete clipping region with restore #73

Merged
merged 3 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ canvas.toDataURL.mockReturnValueOnce(
- [@hustcc](https://github.com/hustcc)
- [@jtenner](https://github.com/jtenner)
- [@evanoc0](https://github.com/evanoc0)
- [@lekha](https://github.com/lekha)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It's my first one ever!


## License

Expand Down
11 changes: 11 additions & 0 deletions __tests__/classes/CanvasRenderingContext2D.__getClippingPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ describe('__getClippingRegion', () => {
ctx.restore();
expect(region).toStrictEqual(ctx.__getClippingRegion());
});

it('should delete current clipping region when restored', () => {
ctx.rect(1, 2, 3, 4);
ctx.clip();
ctx.save();
ctx.rect(1, 2, 3, 4);
ctx.arc(1, 2, 3, 4, 5);
ctx.clip();
ctx.restore();
ctx.save();
});
});
20 changes: 20 additions & 0 deletions __tests__/classes/CanvasRenderingContext2D.restore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let canvas;
let ctx;

beforeEach(() => {
canvas = document.createElement('canvas');
ctx = canvas.getContext('2d');
canvas.width = 400;
canvas.height = 300;
});

describe('save', () => {
it('should be a function', () => {
expect(typeof ctx.restore).toBe('function');
});

it('should be callable', () => {
ctx.restore();
expect(ctx.restore).toBeCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@

exports[`__getClippingRegion should be empty when there are no path elements 1`] = `Array []`;

exports[`__getClippingRegion should delete current clipping region when restored 1`] = `
Array [
Object {
"props": Object {
"height": 4,
"width": 3,
"x": 1,
"y": 2,
},
"transform": Array [
1,
0,
0,
1,
0,
0,
],
"type": "rect",
},
]
`;

exports[`__getClippingRegion should save the clipping region correctly when saved 1`] = `
Array [
Object {
Expand Down
1 change: 1 addition & 0 deletions src/classes/CanvasRenderingContext2D.js
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,7 @@ export default class CanvasRenderingContext2D {
if (this._stackIndex <= 0) return;

this._transformStack.pop();
this._clipStack.pop();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant believe I missed this. Thanks for the pull request!

this._directionStack.pop();
this._fillStyleStack.pop();
this._filterStack.pop();
Expand Down