diff --git a/README.md b/README.md index 2af4d5b..9a3de71 100644 --- a/README.md +++ b/README.md @@ -26,25 +26,23 @@ Features: ### Square [examples/square.nim](examples/square.nim) ```nim -var p: Path -p.rect(50, 50, 100, 100) +let + pos = vec2(50, 50) + wh = vec2(100, 100) -image.fillPath(p, rgba(255, 0, 0, 255)) +image.drawRect(rect(pos, wh), rgba(255, 0, 0, 255)) ``` ![example output](examples/square.png) ### Line [examples/line.nim](examples/line.nim) ```nim -var p: Path -p.moveTo(25, 25) -p.lineTo(175, 175) +let + start = vec2(25, 25) + stop = vec2(175, 175) + color = parseHtmlColor("#FF5C00").rgba -image.strokePath( - p, - parseHtmlColor("#FF5C00").rgba, - strokeWidth = 10, -) +image.drawSegment(segment(start, stop), color, strokeWidth = 10) ``` ![example output](examples/line.png) diff --git a/examples/line.nim b/examples/line.nim index 84a14cd..3a03e23 100644 --- a/examples/line.nim +++ b/examples/line.nim @@ -1,18 +1,13 @@ import pixie -let - image = newImage(200, 200) - +let image = newImage(200, 200) image.fill(rgba(255, 255, 255, 255)) -var p: Path -p.moveTo(25, 25) -p.lineTo(175, 175) +let + start = vec2(25, 25) + stop = vec2(175, 175) + color = parseHtmlColor("#FF5C00").rgba -image.strokePath( - p, - parseHtmlColor("#FF5C00").rgba, - strokeWidth = 10, -) +image.drawSegment(segment(start, stop), color, strokeWidth = 10) image.writeFile("examples/line.png") diff --git a/examples/square.nim b/examples/square.nim index 81796b8..578007c 100644 --- a/examples/square.nim +++ b/examples/square.nim @@ -1,11 +1,12 @@ import pixie -var image = newImage(200, 200) +let image = newImage(200, 200) image.fill(rgba(255, 255, 255, 255)) -var p: Path -p.rect(50, 50, 100, 100) +let + pos = vec2(50, 50) + wh = vec2(100, 100) -image.fillPath(p, rgba(255, 0, 0, 255)) +image.drawRect(rect(pos, wh), rgba(255, 0, 0, 255)) image.writeFile("examples/square.png") diff --git a/src/pixie.nim b/src/pixie.nim index faada81..5305e4b 100644 --- a/src/pixie.nim +++ b/src/pixie.nim @@ -61,3 +61,21 @@ proc drawRect*(mask: Mask, rect: Rect) = var path: Path path.rect(rect) mask.fillPath(path) + +proc drawSegment*( + image: Image, + segment: Segment, + color: ColorRGBA, + strokeWidth = 1.0, + blendMode = bmNormal +) = + var path: Path + path.moveTo(segment.at) + path.lineTo(segment.to) + image.strokePath(path, color, strokeWidth, wrNonZero, blendMode) + +proc drawSegment*(mask: Mask, segment: Segment, strokeWidth: float32) = + var path: Path + path.moveTo(segment.at) + path.lineTo(segment.to) + mask.strokePath(path, strokeWidth) diff --git a/tests/images/drawSegment.png b/tests/images/drawSegment.png new file mode 100644 index 0000000..a2a446b Binary files /dev/null and b/tests/images/drawSegment.png differ diff --git a/tests/images/masks/drawSegment.png b/tests/images/masks/drawSegment.png new file mode 100644 index 0000000..fc209a6 Binary files /dev/null and b/tests/images/masks/drawSegment.png differ diff --git a/tests/test_images_draw.nim b/tests/test_images_draw.nim index 3b3e945..274c369 100644 --- a/tests/test_images_draw.nim +++ b/tests/test_images_draw.nim @@ -55,3 +55,13 @@ block: image.fill(rgba(0, 255, 255, 255)) 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.drawSegment( + segment(vec2(10, 10), vec2(90, 90)), + rgba(255, 255, 0, 255), + strokeWidth = 10 + ) + image.writeFile("tests/images/drawSegment.png") diff --git a/tests/test_masks.nim b/tests/test_masks.nim index b6171f8..48d163f 100644 --- a/tests/test_masks.nim +++ b/tests/test_masks.nim @@ -107,3 +107,11 @@ block: let mask = newMask(100, 100) mask.drawRect(rect(vec2(10, 10), vec2(30, 30))) writeFile("tests/images/masks/drawRect.png", mask.encodePng()) + +block: + let mask = newMask(100, 100) + mask.drawSegment( + segment(vec2(10, 10), vec2(90, 90)), + strokeWidth = 10 + ) + writeFile("tests/images/masks/drawSegment.png", mask.encodePng())