From b1d672e88fb69e8fc32389d5c2fb37504d2275bf Mon Sep 17 00:00:00 2001 From: Pedro Ferreira Date: Fri, 8 Mar 2024 19:37:34 +0000 Subject: [PATCH] feat(canvas): add roundRect (#105) --- README.md | 1 + .../CanvasRenderingContext2D.roundRect.js | 39 +++++++++++++++++++ src/classes/CanvasRenderingContext2D.js | 36 +++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 __tests__/classes/CanvasRenderingContext2D.roundRect.js diff --git a/README.md b/README.md index 58820da..a9b9801 100644 --- a/README.md +++ b/README.md @@ -197,6 +197,7 @@ canvas.toDataURL.mockReturnValueOnce( - [@LitoMore](https://github.com/LitoMore) - [@hrd543](https://github.com/hrd543) - [@danielrentz](https://github.com/danielrentz) +- [@pedroordep](https://github.com/pedroordep) ## License diff --git a/__tests__/classes/CanvasRenderingContext2D.roundRect.js b/__tests__/classes/CanvasRenderingContext2D.roundRect.js new file mode 100644 index 0000000..622c22b --- /dev/null +++ b/__tests__/classes/CanvasRenderingContext2D.roundRect.js @@ -0,0 +1,39 @@ +let canvas; +let ctx; + +beforeEach(() => { + canvas = document.createElement('canvas'); + ctx = canvas.getContext('2d'); + canvas.width = 400; + canvas.height = 300; +}); + +describe('roundRect', () => { + it('should be a function', () => { + expect(typeof ctx.roundRect).toBe('function'); + }); + + it('should be callable', () => { + ctx.roundRect(1, 2, 3, 4, [5, 6, 7, 8]); + expect(ctx.roundRect).toBeCalled(); + }); + + it('should throw if less than 4 parameters are given', () => { + expect(() => ctx.roundRect()).toThrow(TypeError); + expect(() => ctx.roundRect(1)).toThrow(TypeError); + expect(() => ctx.roundRect(1, 2)).toThrow(TypeError); + expect(() => ctx.roundRect(1, 2, 3)).toThrow(TypeError); + }); + + describe('radii parameter', () => { + it('should throw if is an empty array', () => { + expect(() => ctx.roundRect(1, 2, 3, 4, [])).toThrow(TypeError); + }); + + it('should throw if has more than 4 elements', () => { + expect(() => ctx.roundRect(1, 2, 3, 4, [1, 2, 3, 4, 5])).toThrow( + TypeError + ); + }); + }); +}); diff --git a/src/classes/CanvasRenderingContext2D.js b/src/classes/CanvasRenderingContext2D.js index c4800b3..51f10d6 100644 --- a/src/classes/CanvasRenderingContext2D.js +++ b/src/classes/CanvasRenderingContext2D.js @@ -29,6 +29,7 @@ const testFuncs = [ 'fillRect', 'strokeRect', 'rect', + 'roundRect', 'resetTransform', 'translate', 'moveTo', @@ -1514,6 +1515,41 @@ export default class CanvasRenderingContext2D { this._events.push(event); } + roundRect(x, y, width, height, radii) { + if (arguments.length < 4) + throw new TypeError( + "Failed to execute 'roundRect' on '" + + this.constructor.name + + "': 4 arguments required, but only " + + arguments.length + + ' present.' + ); + if (radii.constructor === Array && (radii.length === 0 || radii.length > 4)) + throw new TypeError( + "Failed to execute 'roundRect' on '" + + this.constructor.name + + "': " + + radii.length + + ' radii provided. Between one and four radii are necessary.' + ); + if (!Number.isFinite(x + y + width + height)) return; + + const xResult = Number(x); + const yResult = Number(y); + const widthResult = Number(width); + const heightResult = Number(height); + const event = createCanvasEvent('roundRect', getTransformSlice(this), { + x: xResult, + y: yResult, + width: widthResult, + height: heightResult, + radii: radii, + }); + + this._events.push(event); + this._path.push(event); + } + rotate(angle) { if (arguments.length < 1) throw new TypeError(