drawPolygon

This commit is contained in:
Ryan Oldenburg 2021-02-18 16:40:35 -06:00
parent 0362d03b0b
commit 203a657c8a
9 changed files with 62 additions and 30 deletions

View file

@ -78,32 +78,29 @@ image.fillPath(
### Shadow
[examples/shadow.nim](examples/shadow.nim)
```nim
var p: Path
p.polygon(100, 100, 70, sides = 8)
p.closePath()
let polygonImage = newImage(200, 200)
polygonImage.drawPolygon(
vec2(100, 100),
70,
sides = 8,
rgba(255, 255, 255, 255)
)
var polyImage = newImage(200, 200)
polyImage.fillPath(p, rgba(255, 255, 255, 255))
image.draw(polyImage.shadow(
image.draw(polygonImage.shadow(
offset = vec2(2, 2),
spread = 2,
blur = 10,
color = rgba(0, 0, 0, 200)
))
image.draw(polyImage)
image.draw(polygonImage)
```
![example output](examples/shadow.png)
### Blur
[examples/blur.nim](examples/blur.nim)
```nim
var p: Path
p.polygon(100, 100, 70, sides = 6)
p.closePath()
let mask = newMask(200, 200)
mask.fillPath(p)
mask.drawPolygon(vec2(100, 100), 70, sides = 6)
blur.blur(20)
blur.draw(mask, blendMode = bmMask)

View file

@ -7,12 +7,8 @@ let
image.fill(rgba(255, 255, 255, 255))
var p: Path
p.polygon(100, 100, 70, sides = 6)
p.closePath()
let mask = newMask(200, 200)
mask.fillPath(p)
mask.drawPolygon(vec2(100, 100), 70, sides = 6)
blur.blur(20)
blur.draw(mask, blendMode = bmMask)

View file

@ -1,24 +1,22 @@
import pixie
let
trees = readImage("examples/data/trees.png")
image = newImage(200, 200)
let image = newImage(200, 200)
image.fill(rgba(255, 255, 255, 255))
var p: Path
p.polygon(100, 100, 70, sides = 8)
p.closePath()
let polygonImage = newImage(200, 200)
polygonImage.drawPolygon(
vec2(100, 100),
70,
sides = 8,
rgba(255, 255, 255, 255)
)
var polyImage = newImage(200, 200)
polyImage.fillPath(p, rgba(255, 255, 255, 255))
image.draw(polyImage.shadow(
image.draw(polygonImage.shadow(
offset = vec2(2, 2),
spread = 2,
blur = 10,
color = rgba(0, 0, 0, 200)
))
image.draw(polyImage)
image.draw(polygonImage)
image.writeFile("examples/shadow.png")

View file

@ -151,3 +151,25 @@ proc drawCircle*(
var path: Path
path.ellipse(center, radius, radius)
mask.fillPath(path)
proc drawPolygon*(
image: Image,
pos: Vec2,
size: float32,
sides: int,
color: ColorRGBA,
blendMode = bmNormal
) =
var path: Path
path.polygon(pos, size, sides)
image.fillPath(path, color, wrNonZero, blendMode)
proc drawPolygon*(
mask: Mask,
pos: Vec2,
size: float32,
sides: int
) =
var path: Path
path.polygon(pos, size, sides)
mask.fillPath(path)

View file

@ -462,6 +462,9 @@ proc polygon*(path: var Path, x, y, size: float32, sides: int) =
y + size * sin(side.float32 * 2.0 * PI / sides.float32)
)
proc polygon*(path: var Path, pos: Vec2, size: float32, sides: int) {.inline.} =
path.polygon(pos.x, pos.y, size, sides)
proc commandsToShapes*(path: Path): seq[seq[Vec2]] =
## Converts SVG-like commands to line segments.

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 854 B

View file

@ -86,3 +86,14 @@ block:
rgba(255, 255, 0, 255)
)
image.writeFile("tests/images/drawEllipse.png")
block:
let image = newImage(100, 100)
image.fill(rgba(0, 255, 255, 255))
image.drawPolygon(
vec2(50, 50),
30,
6,
rgba(255, 255, 0, 255)
)
image.writeFile("tests/images/drawPolygon.png")

View file

@ -125,3 +125,8 @@ block:
let mask = newMask(100, 100)
mask.drawEllipse(vec2(50, 50), 20, 10)
writeFile("tests/images/masks/drawEllipse.png", mask.encodePng())
block:
let mask = newMask(100, 100)
mask.drawPolygon(vec2(50, 50), 30, 6)
writeFile("tests/images/masks/drawPolygon.png", mask.encodePng())