Skip to content

Commit

Permalink
graphics: add Graphics.clipRoundRect
Browse files Browse the repository at this point in the history
  • Loading branch information
briansfrank committed Jul 21, 2023
1 parent 77d7a32 commit 9cdf96f
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/doc/docIntro/doc/ChangeLog.fandoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
** license: Licensed under the Academic Free License version 3.0
**************************************************************************

*Build 1.0.80 (working)*

*Build 1.0.79 (17 Jul 2023)*
- New yaml API
- Zip.readEach, Zip.unzipInto
Expand Down
2 changes: 2 additions & 0 deletions src/dom/fan/CanvasGraphics.fan
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ internal native class CanvasGraphics : Graphics

override native This fillRoundRect(Float x, Float y, Float w, Float h, Float wArc, Float hArc)

override native This clipRoundRect(Float x, Float y, Float w, Float h, Float wArc, Float hArc)

override native This drawText(Str s, Float x, Float y)

override native This drawImage(Image img, Float x, Float y, Float w := img.w, Float h := img.h)
Expand Down
8 changes: 8 additions & 0 deletions src/dom/js/CanvasGraphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ fan.dom.CanvasGraphics.prototype.fillRoundRect = function(x, y, w, h, wArc, hArc
return this;
}

// This clipRoundRect(Float x, Float y, Float w, Float h, Float wArc, Float hArc)
fan.dom.CanvasGraphics.prototype.clipRoundRect = function(x, y, w, h, wArc, hArc)
{
this.pathRoundRect(x, y, w, h, wArc, hArc);
this.cx.clip();
return this;
}

// generate path for a rounded rectangle
fan.dom.CanvasGraphics.prototype.pathRoundRect = function(x, y, w, h, wArc, hArc)
{
Expand Down
4 changes: 4 additions & 0 deletions src/graphics/fan/Graphics.fan
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ mixin Graphics
** The ellipse of the corners is specified by wArc and hArc.
abstract This fillRoundRect(Float x, Float y, Float w, Float h, Float wArc, Float hArc)

** Clip a rectangle with rounded corners with the current paint.
** The ellipse of the corners is specified by wArc and hArc.
abstract This clipRoundRect(Float x, Float y, Float w, Float h, Float wArc, Float hArc)

** Draw a the text string with the current paint and font. The x, y
** coordinate specifies the left baseline corner of where the text
** is to be drawn. Technically this is a fill operation similiar to
Expand Down
8 changes: 8 additions & 0 deletions src/graphicsJava/fan/java2D/Java2DGraphics.fan
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ class Java2DGraphics : Graphics
return this
}

** Clip a rectangle with rounded corners with the current paint.
** The ellipse of the corners is specified by wArc and hArc.
override This clipRoundRect(Float x, Float y, Float w, Float h, Float wArc, Float hArc)
{
g.clip(RoundRectangle2D(x, y, w, h, wArc, hArc))
return this
}

** Draw a the text string with the current fill and font.
** The x, y coordinate specifies the top left corner of
** the rectangular area where the text is to be drawn.
Expand Down
14 changes: 10 additions & 4 deletions src/testGraphics/fan/ClipTest.fan
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,31 @@ class ClipTest : AbstractTest
drawStuff(g)

g.push
g.translate(250f, 0f)
g.translate(200f, 0f)
g.path.arc(120f, 120f, 75f, 0f, 360f).clip
drawStuff(g)
g.pop

g.push
g.translate(450f, 0f)
g.translate(400f, 0f)
g.clipRect(45f, 45f, 150f, 150f)
drawStuff(g)
g.pop

g.push
g.translate(600f, 0f)
g.clipRoundRect(45f, 45f, 150f, 150f, 50f, 50f)
drawStuff(g)
g.pop
}

static Void drawStuff(Graphics g)
{
g.color = Color("darkorange")
g.fillRect(20f, 20f, 220f, 220f)
g.fillRect(20f, 20f, 200f, 200f)
g.font = Font("24pt Times")
g.color = Color("Blue")
g.drawText("Clip this text!", 35f, 130f)
g.drawText("Clip this text!", 30f, 120f)
}

}
Expand Down

0 comments on commit 9cdf96f

Please sign in to comment.