Skip to content

Commit

Permalink
implement rect() and add unit test.
Browse files Browse the repository at this point in the history
fix for #100
  • Loading branch information
joshmarinacci committed Aug 20, 2020
1 parent 4c3ed00 commit 27a7b01
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/context.js
Expand Up @@ -770,16 +770,28 @@ class Context {
}

/**
* Rect
* @see https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/rect
* Draws a rectangle with the upper left corner at the specified (x, y)
*
* @ignore
* @returns {void}
*
* @memberof Context
*
*
* @param {number} x The x coordinate of the rectangle
* @param {number} y The y coordinate of the rectangle
* @param {number} width The width of the rectangle
* @param {number} height The height of the rectangle
*
* @throws {Error} Method is not yet implemented
*
* @memberof Context
*/
rect() {
throw new Error("rect not yet supported");
rect(x,y,width,height) {
this.moveTo(x,y);
this.lineTo(x+width,y);
this.lineTo(x+width,y+height);
this.lineTo(x,y+height);
this.lineTo(x,y);
}

/**
Expand Down Expand Up @@ -1143,7 +1155,7 @@ class Context {
let green = (greenNibble << 4) | greenNibble;
let blueNibble = parseInt(str[3], 16);
let blue = (blueNibble << 4) | blueNibble;

let int = uint32.toUint32(red << 16 | green << 8 | blue);
int = uint32.shiftLeft(int,8);
return uint32.or(int,0xff);
Expand All @@ -1157,7 +1169,7 @@ class Context {
let blue = (blueNibble << 4) | blueNibble;
let alphaNibble = parseInt(str[4], 16);
let alpha = (alphaNibble << 4) | alphaNibble;

let int = uint32.toUint32(red << 16 | green << 8 | blue);
int = uint32.shiftLeft(int,8);
return uint32.or(int,alpha);
Expand Down
15 changes: 13 additions & 2 deletions tests/unit/specs/path.test.js
Expand Up @@ -18,8 +18,7 @@ describe('draw curve',() => {
done()
})

//draw square
it('making a square', (done) => {
it('making a square with lines', (done) => {
c.beginPath()
c.moveTo(10,10)
c.lineTo(100,10)
Expand All @@ -35,6 +34,18 @@ describe('draw curve',() => {
done()
})

it('making a square with rect', (done) => {
c.beginPath()
c.rect(10,10,90,90)
c.fillStyle = 'black'
c.fill()
expect(image.getPixelRGBA(0,0)).toBe(WHITE)
expect(image.getPixelRGBA(11,11)).toBe(BLACK)
expect(image.getPixelRGBA(50,50)).toBe(BLACK)
expect(image.getPixelRGBA(100,100)).toBe(WHITE)
done()
})

//draw bezier curve
it('bezier curve', (done) => {
c.fillStyle = 'white'
Expand Down

0 comments on commit 27a7b01

Please sign in to comment.