Change APIs some. Add 2 examples.

This commit is contained in:
treeform 2020-12-01 09:49:41 -08:00
parent c94db0cff7
commit 37428fb28c
9 changed files with 174 additions and 71 deletions

View file

@ -15,3 +15,42 @@ This library is being actively developed and is not yet ready for use. Since you
## Testing
`nimble test`
## Examples
### examples/rounded_rectangle.nim
```nim
var path = newPath()
let
x = 50.0
y = 50.0
w = 100.0
h = 100.0
nw = 25.0
ne = 25.0
se = 25.0
sw = 25.0
path.moveTo(x+nw, y)
path.arcTo(x+w, y, x+w, y+h, ne)
path.arcTo(x+w, y+h, x, y+h, se)
path.arcTo(x, y+h, x, y, sw)
path.arcTo(x, y, x+w, y, nw)
path.closePath()
path.closePath()
image.fillPath(path, rgba(255, 0, 0, 255))
#image.strokePath(path, rgba(0, 0, 0, 255), strokeWidth = 5.0)
```
![example output](examples/rounded_rectangle.png)
### examples/square.nim
```nim
var p = newPath()
p.moveTo(50, 50)
p.lineTo(50, 150)
p.lineTo(150, 150)
p.lineTo(150, 50)
p.closePath()
image.fillPath(p, rgba(255, 0, 0, 255))
#image.strokePath(p, rgba(0, 0, 0, 255), strokeWidth = 5.0)
```
![example output](examples/square.png)

View file

@ -1 +0,0 @@
## Include an example on how to use your library

View file

@ -0,0 +1,25 @@
import pixie, chroma
var image = newImageFill(200, 200, rgba(255, 255, 255, 255))
var path = newPath()
let
x = 50.0
y = 50.0
w = 100.0
h = 100.0
nw = 25.0
ne = 25.0
se = 25.0
sw = 25.0
path.moveTo(x+nw, y)
path.arcTo(x+w, y, x+w, y+h, ne)
path.arcTo(x+w, y+h, x, y+h, se)
path.arcTo(x, y+h, x, y, sw)
path.arcTo(x, y, x+w, y, nw)
path.closePath()
path.closePath()
image.fillPath(path, rgba(255, 0, 0, 255))
#image.strokePath(path, rgba(0, 0, 0, 255), strokeWidth = 5.0)
image.writeFile("examples/rounded_rectangle.png")

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

14
examples/square.nim Normal file
View file

@ -0,0 +1,14 @@
import pixie, chroma
var image = newImageFill(200, 200, rgba(255, 255, 255, 255))
var p = newPath()
p.moveTo(50, 50)
p.lineTo(50, 150)
p.lineTo(150, 150)
p.lineTo(150, 50)
p.closePath()
image.fillPath(p, rgba(255, 0, 0, 255))
#image.strokePath(p, rgba(0, 0, 0, 255), strokeWidth = 5.0)
image.writeFile("examples/square.png")

BIN
examples/square.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 918 B

View file

@ -32,6 +32,10 @@ proc newImageNoInit*(width, height: int): Image =
result.height = height
result.data = newSeq[ColorRGBA](width * height)
proc wh*(image: Image): Vec2 {.inline.} =
## Return with and height as a size vector.
vec2(image.width.float32, image.height.float32)
proc copy*(image: Image): Image =
## Copies an image creating a new image.
result = newImage(image.width, image.height)

View file

@ -554,14 +554,14 @@ proc strokePolygons*(ps: seq[seq[Vec2]], strokeWidthR, strokeWidthL: float32): s
{.push checks: off, stacktrace: off.}
proc fillPolygons*(
image: Image,
size: Vec2,
polys: seq[seq[Vec2]],
color: ColorRGBA,
quality = 4,
): Image =
const ep = 0.0001 * PI
result = newImage(image.width, image.height)
result = newImage(size.x.int, size.y.int)
proc scanLineHits(
polys: seq[seq[Vec2]],
@ -579,7 +579,7 @@ proc fillPolygons*(
var at: Vec2
if line.intersects(scan, at):
let winding = line.at.y > line.to.y
let x = at.x.clamp(0, image.width.float32)
let x = at.x.clamp(0, size.x)
hits.add((x, winding))
hits.sort(proc(a, b: (float32, bool)): int = cmp(a[0], b[0]))
@ -623,103 +623,91 @@ proc fillPolygons*(
{.pop.}
proc fillPath*(
image: Image,
path: Path,
color: ColorRGBA
): Image =
let polys = commandsToPolygons(path.commands)
image.fillPolygons(polys, color)
image: Image,
path: Path,
color: ColorRGBA
) =
let
polys = commandsToPolygons(path.commands)
tmp = fillPolygons(image.wh, polys, color)
image.draw(tmp)
proc fillPath*(
image: Image,
path: string,
color: ColorRGBA
): Image =
image.fillPath(parsePath(path), color)
proc fillPath*(
image: Image,
path: string,
color: ColorRGBA,
pos: Vec2
): Image =
var polys = commandsToPolygons(parsePath(path).commands)
for poly in polys.mitems:
for i, p in poly.mpairs:
poly[i] = p + pos
image.fillPolygons(polys, color)
proc fillPath*(
image: Image,
path: Path,
color: ColorRGBA,
mat: Mat3
): Image =
image: Image,
path: Path,
color: ColorRGBA,
mat: Mat3
) =
var polys = commandsToPolygons(path.commands)
for poly in polys.mitems:
for i, p in poly.mpairs:
poly[i] = mat * p
image.fillPolygons(polys, color)
let tmp = fillPolygons(image.wh, polys, color)
image.draw(tmp)
proc fillPath*(
image: Image,
path: string,
color: ColorRGBA,
mat: Mat3
): Image =
image: Image,
path: string,
color: ColorRGBA,
mat: Mat3
) =
image.fillPath(parsePath(path), color, mat)
proc strokePath*(
image: Image,
path: Path,
color: ColorRGBA,
strokeWidth: float32,
# strokeLocation: StrokeLocation,
# strokeCap: StorkeCap,
# strokeJoin: StorkeJoin
): Image =
let polys = commandsToPolygons(path.commands)
let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
let polys2 = strokePolygons(polys, strokeL, strokeR)
image.fillPolygons(polys2, color)
image: Image,
path: Path,
color: ColorRGBA,
strokeWidth: float32 = 1.0,
# strokeLocation: StrokeLocation,
# strokeCap: StorkeCap,
# strokeJoin: StorkeJoin
) =
let
polys = commandsToPolygons(path.commands)
(strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
polys2 = strokePolygons(polys, strokeL, strokeR)
tmp = fillPolygons(image.wh, polys2, color)
image.draw(tmp)
proc strokePath*(
image: Image,
path: string,
color: ColorRGBA,
strokeWidth: float32
): Image =
image: Image,
path: string,
color: ColorRGBA,
strokeWidth: float32
) =
image.strokePath(parsePath(path), color, strokeWidth)
proc strokePath*(
image: Image,
path: string,
color: ColorRGBA,
strokeWidth: float32,
pos: Vec2
): Image =
image: Image,
path: string,
color: ColorRGBA,
strokeWidth: float32,
pos: Vec2
) =
var polys = commandsToPolygons(parsePath(path).commands)
let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
var polys2 = strokePolygons(polys, strokeL, strokeR)
for poly in polys2.mitems:
for i, p in poly.mpairs:
poly[i] = p + pos
image.fillPolygons(polys2, color)
let tmp = fillPolygons(image.wh, polys2, color)
image.draw(tmp)
proc strokePath*(
image: Image,
path: string,
color: ColorRGBA,
strokeWidth: float32,
mat: Mat3
): Image =
image: Image,
path: string,
color: ColorRGBA,
strokeWidth: float32,
mat: Mat3
) =
var polys = commandsToPolygons(parsePath(path).commands)
let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
var polys2 = strokePolygons(polys, strokeL, strokeR)
for poly in polys2.mitems:
for i, p in poly.mpairs:
poly[i] = mat * p
image.fillPolygons(polys2, color)
let tmp = fillPolygons(image.wh, polys2, color)
image.draw(tmp)
proc addPath*(path: Path, other: Path) =
## Adds a path to the current path.

34
tools/gen_readme.nim Normal file
View file

@ -0,0 +1,34 @@
import os, strutils, osproc
proc cutBetween(str, a, b: string): string =
let
cutA = str.find(a)
cutB = str.find(b)
if cutA == -1 or cutB == -1:
return ""
return str[cutA + a.len..<cutB]
var md: seq[string]
for k, path in walkDir("examples"):
if path.endsWith(".nim"):
discard execCmd("nim c -r -d:danger " & path)
let code = readFile(path)
let innerCode = code.cutBetween("var image = newImageFill(200, 200, rgba(255, 255, 255, 255))", "image.writeFile")
if innerCode != "":
md.add "### " & path.replace("\\", "/")
md.add "```nim"
md.add innerCode.strip()
md.add "```"
md.add "![example output](" & path.replace(".nim", ".png").replace("\\", "/") & ")"
md.add ""
var readme = readFile("README.md")
let at = readme.find("## Examples")
if at != -1:
readme = readme[0 .. at]
readme.add("# Examples\n\n")
readme.add(md.join("\n"))
writeFile("README.md", readme)