From 3016790e8ac1d3ebc9a80336aeb54ad645068b35 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Thu, 18 Feb 2021 13:55:26 -0600 Subject: [PATCH] drawSegment + updated examples --- README.md | 20 +++++++++----------- examples/line.nim | 17 ++++++----------- examples/square.nim | 9 +++++---- src/pixie.nim | 18 ++++++++++++++++++ tests/images/drawSegment.png | Bin 0 -> 1275 bytes tests/images/masks/drawSegment.png | Bin 0 -> 466 bytes tests/test_images_draw.nim | 10 ++++++++++ tests/test_masks.nim | 8 ++++++++ 8 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 tests/images/drawSegment.png create mode 100644 tests/images/masks/drawSegment.png 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 0000000000000000000000000000000000000000..a2a446b91c70dd8745bd336ad52d7ee4b2bf2ed8 GIT binary patch literal 1275 zcmeAS@N?(olHy`uVBq!ia0vp^DIm<)>|#E5sjZ#Hm{#KW3p#j-lWY6_Dt!|H<~q0-uvm-uPCu@ zSGiJ)8`b(sE8BFI7;Tzx>V%V)GWW_Jzt%a^+CRncX;)9p6wrIJ>CTkfMS*frsOs?N!vU&})kSD%WQwDimA{jDZbH>!BQ>_6so zYYK?bzO^+XH#b;t)0DH3(H;MJR_mS=JahV~*PY(2tqmbXo_z^%es-M~BRD)%r$6C% z_Vmf*Z-1f@oU$!iEK4SN7qNUU`gHRD7Ckt}(|DnigVs(JcLRO(`D@f;5L&L5AZdxW z5@!lJscP9&Y5DVsy0j>Q)7{I>xbn#)nS*=g)O>Q(M>t0FFGAJM%eTchwLqO&ckW9Z z+?wa76yRFw`gp<+62}%GB>Z@gbQ}ufg4y^-=0=pF!qk{a$}^@s83Z>?a64tq|8&ip>C=ls=G{AgJ1Nd@ zpKf*P#)vJl^NeB2a!)uh!OZ&aE+zy^%lkTYmN3Hr^2&YAMNEjLRx@`QuU^k%>CbCA*nUerA1)pXRFd2*+_EU-{x@O1Ta JS?83{1OWJ;IOhNW literal 0 HcmV?d00001 diff --git a/tests/images/masks/drawSegment.png b/tests/images/masks/drawSegment.png new file mode 100644 index 0000000000000000000000000000000000000000..fc209a69bb59e98c3421b0b4180ce5b02176ab95 GIT binary patch literal 466 zcmV;@0WJQCP)EZ}jxL8fvp(mB%3;R)I$vP;!~ZEy^npYO{>mG>S!(gCi{DZHRJqUyhn z>wH&@L0U1c={C&LgPL+=x`vUWWXN<4fun55!{8_@k_?KnAgjSp5^}y}uLXYl3Aqm= zQAtRcnaYAtl@(zs8$whvgs0>PO(_tTQX(W}hj5fVLQ$R&hVqOMlmi5>93gP!5J4-) z2w15gSfz$Ql`4W%>IhJ2AULIkz?3F}QrZYe=^z-Thd`7rf>8PhKoKB*MS{2$5#m*3 zh*J?FK1GVS6fxpam~p6f>ex?1(~fAaccnh!qziReXq0aUwFsi-;6AB2oN^Kmj0q1%Y%G2+~t9 zNJjx7Z3Tri6&TV|a7aS|B6S6cR23*vQ?STh`HA~|Fj6X)&$uB)Be`<fC{z`ZL2>!Za@qa9lkg_+T>Lug)1)J%z&cb$(fB*mh07*qo IM6N<$f_=oi>i_@% literal 0 HcmV?d00001 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())