diff --git a/README.md b/README.md index ec98a8a..3b81851 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,18 @@ 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 + pos = vec2(50, 50) + wh = vec2(100, 100) + r = 25.0 + +image.drawRoundedRect(rect(pos, wh), r, rgba(0, 255, 0, 255)) +``` +![example output](examples/rounded_rectangle.png) + ### Heart [examples/heart.nim](examples/heart.nim) ```nim diff --git a/examples/rounded_rectangle.nim b/examples/rounded_rectangle.nim index 8b6f41b..776fb6d 100644 --- a/examples/rounded_rectangle.nim +++ b/examples/rounded_rectangle.nim @@ -9,3 +9,5 @@ let r = 25.0 image.drawRoundedRect(rect(pos, wh), r, rgba(0, 255, 0, 255)) + +image.writeFile("examples/rounded_rectangle.png") diff --git a/src/pixie.nim b/src/pixie.nim index ba91f48..a757b2e 100644 --- a/src/pixie.nim +++ b/src/pixie.nim @@ -111,3 +111,43 @@ proc drawSegment*(mask: Mask, segment: Segment, strokeWidth: float32) = path.moveTo(segment.at) path.lineTo(segment.to) mask.strokePath(path, strokeWidth) + +proc drawEllipse*( + image: Image, + center: Vec2, + rx, ry: float32, + color: ColorRGBA, + blendMode = bmNormal +) = + var path: Path + path.ellipse(center, rx, ry) + image.fillPath(path, color, wrNonZero, blendMode) + +proc drawEllipse*( + mask: Mask, + center: Vec2, + rx, ry: float32 +) = + var path: Path + path.ellipse(center, rx, ry) + mask.fillPath(path) + +proc drawCircle*( + image: Image, + center: Vec2, + radius: float32, + color: ColorRGBA, + blendMode = bmNormal +) = + var path: Path + path.ellipse(center, radius, radius) + image.fillPath(path, color, wrNonZero, blendMode) + +proc drawCircle*( + mask: Mask, + center: Vec2, + radius: float32 +) = + var path: Path + path.ellipse(center, radius, radius) + mask.fillPath(path) diff --git a/src/pixie/paths.nim b/src/pixie/paths.nim index d040cf6..e6737a5 100644 --- a/src/pixie/paths.nim +++ b/src/pixie/paths.nim @@ -450,6 +450,9 @@ proc ellipse*(path: var Path, cx, cy, rx, ry: float32) = path.bezierCurveTo(cx + magicX, cy - ry, cx + rx, cy - magicY, cx + rx, cy) path.closePath() +proc ellipse*(path: var Path, center: Vec2, rx, ry: float32) {.inline.} = + path.ellipse(center.x, center.y, rx, ry) + proc polygon*(path: var Path, x, y, size: float32, sides: int) = ## Draws a n sided regular polygon at (x, y) with size. path.moveTo(x + size * cos(0.0), y + size * sin(0.0)) diff --git a/tests/images/drawEllipse.png b/tests/images/drawEllipse.png new file mode 100644 index 0000000..a816ac3 Binary files /dev/null and b/tests/images/drawEllipse.png differ diff --git a/tests/images/drawRoundedRect.png b/tests/images/drawRoundedRect.png new file mode 100644 index 0000000..c3050ad Binary files /dev/null and b/tests/images/drawRoundedRect.png differ diff --git a/tests/images/masks/drawEllipse.png b/tests/images/masks/drawEllipse.png new file mode 100644 index 0000000..1bd4af3 Binary files /dev/null and b/tests/images/masks/drawEllipse.png differ diff --git a/tests/images/masks/drawRoundedRect.png b/tests/images/masks/drawRoundedRect.png new file mode 100644 index 0000000..13bb852 Binary files /dev/null and b/tests/images/masks/drawRoundedRect.png differ diff --git a/tests/test_images_draw.nim b/tests/test_images_draw.nim index cbebcb5..216b74b 100644 --- a/tests/test_images_draw.nim +++ b/tests/test_images_draw.nim @@ -75,3 +75,14 @@ block: strokeWidth = 10 ) image.writeFile("tests/images/drawSegment.png") + +block: + let image = newImage(100, 100) + image.fill(rgba(0, 255, 255, 255)) + image.drawEllipse( + vec2(50, 50), + 25, + 25, + rgba(255, 255, 0, 255) + ) + image.writeFile("tests/images/drawEllipse.png") diff --git a/tests/test_masks.nim b/tests/test_masks.nim index 2338bec..24a391a 100644 --- a/tests/test_masks.nim +++ b/tests/test_masks.nim @@ -120,3 +120,8 @@ block: strokeWidth = 10 ) writeFile("tests/images/masks/drawSegment.png", mask.encodePng()) + +block: + let mask = newMask(100, 100) + mask.drawEllipse(vec2(50, 50), 20, 10) + writeFile("tests/images/masks/drawEllipse.png", mask.encodePng())