-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,4 +51,26 @@ describe('__getClippingRegion', () => { | |
ctx.restore(); | ||
expect(region).toStrictEqual(ctx.__getClippingRegion()); | ||
}); | ||
|
||
it('should restore the clipping region correctly when restored', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can consolidate these two tests into a single one, because we already test to see if the clipping region is properly set without restoring, right? ctx.arc(1, 2, 3, 4, 5);
ctx.clip(); // push the arc to the clipping stack
ctx.save(); // push the current clipping region to the top of the stack
ctx.rect(1, 2, 3, 4);
ctx.arc(1, 2, 3, 4, 5);
ctx.clip(); // adds the path to the current stack clipping region
ctx.restore(); // restores back to a single arc It would be best to utilize the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. I just made the changes. Better? |
||
const region = ctx.__getClippingRegion(); | ||
ctx.save(); | ||
ctx.rect(1, 2, 3, 4); | ||
ctx.arc(1, 2, 3, 4, 5); | ||
ctx.clip(); | ||
expect(region).not.toStrictEqual(ctx.__getClippingRegion()); | ||
ctx.restore(); | ||
expect(region).toStrictEqual(ctx.__getClippingRegion()); | ||
}); | ||
|
||
it('should delete current clipping region when restored', () => { | ||
ctx.save(); | ||
ctx.rect(1, 2, 3, 4); | ||
ctx.arc(1, 2, 3, 4, 5); | ||
ctx.clip(); | ||
const region = ctx.__getClippingRegion(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. First, I want to admit that this test function does indeed assert that the With that being said, I believe simplifying the tests here will go a long way in describing how the functions are supposed to work. If possible, I would like to remove a few of the function calls in this test, so I will outline the reasons why I suggest them.
All in all, great work. Please remove the lines in question, and I will accept the contribution. Cheers. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for working with me on this. I haven't been working with javascript long. I tried removing lines 62 and 64-65, but then the test passes even when The Is there a better way to check that the pop happens? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay. At the very least, we should make sure not use expect calls here. We can just inspect the state after the test runs, and that's enough. As for your example, I think I'm being pedantic. Just remove the expect calls from this test and we should be good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an additional test that doesn't require expect calls too. I can understand why they were added in the first place. They aren't necessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense. Fixed. |
||
ctx.restore(); | ||
ctx.save(); | ||
expect(region).not.toStrictEqual(ctx.__getClippingRegion()); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -1477,6 +1477,7 @@ export default class CanvasRenderingContext2D { | |
if (this._stackIndex <= 0) return; | ||
|
||
this._transformStack.pop(); | ||
this._clipStack.pop(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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!