drawRoundedRect + updated example
This commit is contained in:
parent
224f21337e
commit
318d90e509
6 changed files with 61 additions and 32 deletions
17
README.md
17
README.md
|
@ -46,23 +46,6 @@ image.drawSegment(segment(start, stop), color, strokeWidth = 10)
|
||||||
```
|
```
|
||||||

|

|
||||||
|
|
||||||
### 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))
|
|
||||||
```
|
|
||||||

|
|
||||||
|
|
||||||
### Heart
|
### Heart
|
||||||
[examples/heart.nim](examples/heart.nim)
|
[examples/heart.nim](examples/heart.nim)
|
||||||
```nim
|
```nim
|
||||||
|
|
|
@ -4,15 +4,8 @@ let image = newImage(200, 200)
|
||||||
image.fill(rgba(255, 255, 255, 255))
|
image.fill(rgba(255, 255, 255, 255))
|
||||||
|
|
||||||
let
|
let
|
||||||
x = 50.0
|
pos = vec2(50, 50)
|
||||||
y = 50.0
|
wh = vec2(100, 100)
|
||||||
w = 100.0
|
|
||||||
h = 100.0
|
|
||||||
r = 25.0
|
r = 25.0
|
||||||
|
|
||||||
var path: Path
|
image.drawRoundedRect(rect(pos, wh), r, rgba(0, 255, 0, 255))
|
||||||
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")
|
|
||||||
|
|
|
@ -62,6 +62,38 @@ proc drawRect*(mask: Mask, rect: Rect) =
|
||||||
path.rect(rect)
|
path.rect(rect)
|
||||||
mask.fillPath(path)
|
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*(
|
proc drawSegment*(
|
||||||
image: Image,
|
image: Image,
|
||||||
segment: Segment,
|
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
|
const splineCircleK = 4.0 * (-1.0 + sqrt(2.0)) / 3
|
||||||
|
|
||||||
proc roundedRect*(
|
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
|
let
|
||||||
x = pos.x
|
|
||||||
y = pos.y
|
|
||||||
w = wh.x
|
|
||||||
h = wh.y
|
|
||||||
s = splineCircleK
|
s = splineCircleK
|
||||||
|
|
||||||
maxRadius = min(w / 2, h / 2)
|
maxRadius = min(w / 2, h / 2)
|
||||||
|
@ -432,6 +428,16 @@ proc roundedRect*(
|
||||||
|
|
||||||
path.closePath()
|
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) =
|
proc ellipse*(path: var Path, cx, cy, rx, ry: float32) =
|
||||||
let
|
let
|
||||||
magicX = splineCircleK * rx
|
magicX = splineCircleK * rx
|
||||||
|
|
|
@ -56,6 +56,16 @@ block:
|
||||||
image.drawRect(rect(vec2(10, 10), vec2(30, 30)), rgba(255, 255, 0, 255))
|
image.drawRect(rect(vec2(10, 10), vec2(30, 30)), rgba(255, 255, 0, 255))
|
||||||
image.writeFile("tests/images/drawRect.png")
|
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:
|
block:
|
||||||
let image = newImage(100, 100)
|
let image = newImage(100, 100)
|
||||||
image.fill(rgba(0, 255, 255, 255))
|
image.fill(rgba(0, 255, 255, 255))
|
||||||
|
|
|
@ -108,6 +108,11 @@ block:
|
||||||
mask.drawRect(rect(vec2(10, 10), vec2(30, 30)))
|
mask.drawRect(rect(vec2(10, 10), vec2(30, 30)))
|
||||||
writeFile("tests/images/masks/drawRect.png", mask.encodePng())
|
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:
|
block:
|
||||||
let mask = newMask(100, 100)
|
let mask = newMask(100, 100)
|
||||||
mask.drawSegment(
|
mask.drawSegment(
|
||||||
|
|
Loading…
Reference in a new issue