drawRoundedRect + updated example
This commit is contained in:
parent
224f21337e
commit
318d90e509
17
README.md
17
README.md
|
@ -46,23 +46,6 @@ image.drawSegment(segment(start, stop), color, strokeWidth = 10)
|
|||
```
|
||||
![example output](examples/line.png)
|
||||
|
||||
### Rounded rectangle
|
||||
[examples/rounded_rectangle.nim](examples/rounded_rectangle.nim)
|
||||
```nim
|
||||
let
|
||||
x = 50.0
|
||||
y = 50.0
|
||||
w = 100.0
|
||||
h = 100.0
|
||||
r = 25.0
|
||||
|
||||
var path: Path
|
||||
path.roundedRect(vec2(x, y), vec2(w, h), r, r, r, r)
|
||||
|
||||
image.fillPath(path, rgba(0, 255, 0, 255))
|
||||
```
|
||||
![example output](examples/rounded_rectangle.png)
|
||||
|
||||
### Heart
|
||||
[examples/heart.nim](examples/heart.nim)
|
||||
```nim
|
||||
|
|
|
@ -4,15 +4,8 @@ let image = newImage(200, 200)
|
|||
image.fill(rgba(255, 255, 255, 255))
|
||||
|
||||
let
|
||||
x = 50.0
|
||||
y = 50.0
|
||||
w = 100.0
|
||||
h = 100.0
|
||||
pos = vec2(50, 50)
|
||||
wh = vec2(100, 100)
|
||||
r = 25.0
|
||||
|
||||
var path: Path
|
||||
path.roundedRect(vec2(x, y), vec2(w, h), r, r, r, r)
|
||||
|
||||
image.fillPath(path, rgba(0, 255, 0, 255))
|
||||
|
||||
image.writeFile("examples/rounded_rectangle.png")
|
||||
image.drawRoundedRect(rect(pos, wh), r, rgba(0, 255, 0, 255))
|
||||
|
|
|
@ -62,6 +62,38 @@ proc drawRect*(mask: Mask, rect: Rect) =
|
|||
path.rect(rect)
|
||||
mask.fillPath(path)
|
||||
|
||||
proc drawRoundedRect*(
|
||||
image: Image,
|
||||
rect: Rect,
|
||||
nw, ne, se, sw: float32,
|
||||
color: ColorRGBA,
|
||||
blendMode = bmNormal
|
||||
) =
|
||||
var path: Path
|
||||
path.roundedRect(rect, nw, ne, se, sw)
|
||||
image.fillPath(path, color, wrNonZero, blendMode)
|
||||
|
||||
proc drawRoundedRect*(
|
||||
image: Image,
|
||||
rect: Rect,
|
||||
radius: float32,
|
||||
color: ColorRGBA,
|
||||
blendMode = bmNormal
|
||||
) =
|
||||
var path: Path
|
||||
path.roundedRect(rect, radius, radius, radius, radius)
|
||||
image.fillPath(path, color, wrNonZero, blendMode)
|
||||
|
||||
proc drawRoundedRect*(mask: Mask, rect: Rect, nw, ne, se, sw: float32) =
|
||||
var path: Path
|
||||
path.roundedRect(rect, nw, ne, se, sw)
|
||||
mask.fillPath(path)
|
||||
|
||||
proc drawRoundedRect*(mask: Mask, rect: Rect, radius: float32) =
|
||||
var path: Path
|
||||
path.roundedRect(rect, radius, radius, radius, radius)
|
||||
mask.fillPath(path)
|
||||
|
||||
proc drawSegment*(
|
||||
image: Image,
|
||||
segment: Segment,
|
||||
|
|
|
@ -376,13 +376,9 @@ proc rect*(path: var Path, rect: Rect, clockwise = true) {.inline.} =
|
|||
const splineCircleK = 4.0 * (-1.0 + sqrt(2.0)) / 3
|
||||
|
||||
proc roundedRect*(
|
||||
path: var Path, pos, wh: Vec2, nw, ne, se, sw: float32, clockwise = true
|
||||
path: var Path, x, y, w, h, nw, ne, se, sw: float32, clockwise = true
|
||||
) =
|
||||
let
|
||||
x = pos.x
|
||||
y = pos.y
|
||||
w = wh.x
|
||||
h = wh.y
|
||||
s = splineCircleK
|
||||
|
||||
maxRadius = min(w / 2, h / 2)
|
||||
|
@ -432,6 +428,16 @@ proc roundedRect*(
|
|||
|
||||
path.closePath()
|
||||
|
||||
proc roundedRect*(
|
||||
path: var Path, pos, wh: Vec2, nw, ne, se, sw: float32, clockwise = true
|
||||
) {.inline.} =
|
||||
path.roundedRect(pos.x, pos.y, wh.x, wh.y, nw, ne, se, sw, clockwise)
|
||||
|
||||
proc roundedRect*(
|
||||
path: var Path, rect: Rect, nw, ne, se, sw: float32, clockwise = true
|
||||
) {.inline.} =
|
||||
path.roundedRect(rect.x, rect.y, rect.w, rect.h, nw, ne, se, sw, clockwise)
|
||||
|
||||
proc ellipse*(path: var Path, cx, cy, rx, ry: float32) =
|
||||
let
|
||||
magicX = splineCircleK * rx
|
||||
|
|
|
@ -56,6 +56,16 @@ block:
|
|||
image.drawRect(rect(vec2(10, 10), vec2(30, 30)), rgba(255, 255, 0, 255))
|
||||
image.writeFile("tests/images/drawRect.png")
|
||||
|
||||
block:
|
||||
let image = newImage(100, 100)
|
||||
image.fill(rgba(0, 255, 255, 255))
|
||||
image.drawRoundedRect(
|
||||
rect(vec2(10, 10), vec2(30, 30)),
|
||||
10,
|
||||
rgba(255, 255, 0, 255)
|
||||
)
|
||||
image.writeFile("tests/images/drawRoundedRect.png")
|
||||
|
||||
block:
|
||||
let image = newImage(100, 100)
|
||||
image.fill(rgba(0, 255, 255, 255))
|
||||
|
|
|
@ -108,6 +108,11 @@ block:
|
|||
mask.drawRect(rect(vec2(10, 10), vec2(30, 30)))
|
||||
writeFile("tests/images/masks/drawRect.png", mask.encodePng())
|
||||
|
||||
block:
|
||||
let mask = newMask(100, 100)
|
||||
mask.drawRoundedRect(rect(vec2(10, 10), vec2(30, 30)), 10)
|
||||
writeFile("tests/images/masks/drawRoundedRect.png", mask.encodePng())
|
||||
|
||||
block:
|
||||
let mask = newMask(100, 100)
|
||||
mask.drawSegment(
|
||||
|
|
Loading…
Reference in a new issue