Skip to content

Commit

Permalink
adding round() to rectangle shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
runemadsen committed Feb 8, 2016
1 parent 2e7d489 commit 5d44075
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Adding `Rune.Image` to draw images in a SVG element
- `strokeWidth` is now scaled when calling `.scale()` on a shape
- Adding `round()` function to rectangles

## 0.2.13

Expand Down
3 changes: 0 additions & 3 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# TODO

- [ ] Strokeweight scale
- [ ] Rounded rect

- [ ] Rewrite with react and redux. Prevent re-drawing all the time.
- [ ] Text layout with bounds, lineHeight, etc
- [ ] Ability to pass vectors into x,y functions
Expand Down
2 changes: 2 additions & 0 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class Render {
width: this.s(rect.vars.width),
height: this.s(rect.vars.height)
}
if(rect.vars.rx) attr.rx = this.s(rect.vars.rx);
if(rect.vars.ry) attr.ry = this.s(rect.vars.ry);
this.transformAttribute(attr, rect);
this.styleableAttributes(rect, attr);
return svg('rect', attr);
Expand Down
6 changes: 6 additions & 0 deletions src/shapes/rectangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class Rectangle {
this.vars.height = height;
}

round(rx, ry) {
if(!ry) ry = rx;
this.vars.rx = rx;
this.vars.ry = ry;
}

toPolygon(opts, parent) {
var poly = new Polygon(this.vars.x, this.vars.y)
.lineTo(0, 0)
Expand Down
9 changes: 9 additions & 0 deletions test/shared/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,19 @@ describe("Rune.Render", function() {
width: s.vars.width,
height: s.vars.height
});
expect(rect).toNotHaveAttr("rx");
expect(rect).toNotHaveAttr("ry");
expect(rect).not.toHaveTranslation(100, 105);
expectShared(rect);
});

it("should render rounded corners", function() {
var s = r.rect(100, 105, 300, 400).round(25, 15);
r.draw();
var rect = el.childNodes[0];
expect(rect).toHaveAttrs({ rx: 25, ry: 15 });
});

});

describe("Rune.Ellipse", function() {
Expand Down
16 changes: 16 additions & 0 deletions test/shared/shapes/rect.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ describe("Rune.Rectangle", function() {

});

describe("round()", function() {

it('sets uniform round corners', function() {
s.round(25);
expect(s.vars.rx).toEqual(25);
expect(s.vars.ry).toEqual(25);
});

it('sets roundness for both x and y', function() {
s.round(25, 15);
expect(s.vars.rx).toEqual(25);
expect(s.vars.ry).toEqual(15);
});

});

describe("scale()", function() {

it("scales the rectangle", function() {
Expand Down

0 comments on commit 5d44075

Please sign in to comment.