Skip to content

Commit

Permalink
✨ Add rounded rectangle option
Browse files Browse the repository at this point in the history
  • Loading branch information
GMartigny committed Sep 25, 2023
1 parent c319def commit 7a5b425
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/modules/rectangle/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ const rectangle = new Rectangle(position, width, height, options);
## RectangleOptions
Inherit from [ComponentOptions](../component/readme.md#componentoptions).

Rectangle have no specific options.
| Name | Type | Default | Comment |
|---------|-----------------------------|---------|-------------------------------------------------------------------------------------|
| rounded | `Number` or `Array<Number>` | `0` | Corner radius or an array of radii [top-left, top-right, bottom-right, bottom-left] |
23 changes: 22 additions & 1 deletion src/modules/rectangle/rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ export default class Rectangle extends Component {
* @return {Rectangle} Itself
*/
trace (path) {
path.rect(0, 0, this.width, this.height);
const { rounded } = this.options;
if (rounded) {
path.roundRect(0, 0, this.width, this.height, ...(Array.isArray(rounded) ? rounded : [rounded]));
}
else {
path.rect(0, 0, this.width, this.height);
}
return this;
}

Expand Down Expand Up @@ -91,6 +97,21 @@ export default class Rectangle extends Component {
return new Rectangle(definition.position, definition.width, definition.height, definition.options);
}

/**
* @typedef {Object} RectangleOptions
* @extends ComponentOptions
* @prop {Number|Array<Number>} rounded - Corner radius or an array of radii [top-left, top-right, bottom-right, bottom-left]
*/
/**
* @type {RectangleOptions}
*/
static get defaultOptions () {
return {
...super.defaultOptions,
rounded: 0,
};
}

/**
* @typedef {Object} RectangleOrigins
* @prop {String} topLeft
Expand Down
11 changes: 11 additions & 0 deletions src/modules/rectangle/rectangle.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ test("trace", (t) => {
t.context.trace(path);
});

test("trace rounded", (t) => {
t.context.options.rounded = 11;
const path = {
roundRect: (...params) => {
t.deepEqual(params, [0, 0, 123, 22, 11]);
},
};
t.plan(1);
t.context.trace(path);
});

test("getOrigin", (t) => {
const { origins } = Rectangle;
const expected = {
Expand Down

0 comments on commit 7a5b425

Please sign in to comment.