Change APIs some. Add 2 examples.
This commit is contained in:
parent
c94db0cff7
commit
37428fb28c
9 changed files with 174 additions and 71 deletions
39
README.md
39
README.md
|
@ -15,3 +15,42 @@ This library is being actively developed and is not yet ready for use. Since you
|
||||||
## Testing
|
## Testing
|
||||||
|
|
||||||
`nimble test`
|
`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)
|
||||||
|
```
|
||||||
|

|
||||||
|
|
||||||
|
### 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)
|
||||||
|
```
|
||||||
|

|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
## Include an example on how to use your library
|
|
25
examples/rounded_rectangle.nim
Normal file
25
examples/rounded_rectangle.nim
Normal 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")
|
BIN
examples/rounded_rectangle.png
Normal file
BIN
examples/rounded_rectangle.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.1 KiB |
14
examples/square.nim
Normal file
14
examples/square.nim
Normal 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
BIN
examples/square.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 918 B |
|
@ -32,6 +32,10 @@ proc newImageNoInit*(width, height: int): Image =
|
||||||
result.height = height
|
result.height = height
|
||||||
result.data = newSeq[ColorRGBA](width * 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 =
|
proc copy*(image: Image): Image =
|
||||||
## Copies an image creating a new image.
|
## Copies an image creating a new image.
|
||||||
result = newImage(image.width, image.height)
|
result = newImage(image.width, image.height)
|
||||||
|
|
|
@ -554,14 +554,14 @@ proc strokePolygons*(ps: seq[seq[Vec2]], strokeWidthR, strokeWidthL: float32): s
|
||||||
{.push checks: off, stacktrace: off.}
|
{.push checks: off, stacktrace: off.}
|
||||||
|
|
||||||
proc fillPolygons*(
|
proc fillPolygons*(
|
||||||
image: Image,
|
size: Vec2,
|
||||||
polys: seq[seq[Vec2]],
|
polys: seq[seq[Vec2]],
|
||||||
color: ColorRGBA,
|
color: ColorRGBA,
|
||||||
quality = 4,
|
quality = 4,
|
||||||
): Image =
|
): Image =
|
||||||
const ep = 0.0001 * PI
|
const ep = 0.0001 * PI
|
||||||
|
|
||||||
result = newImage(image.width, image.height)
|
result = newImage(size.x.int, size.y.int)
|
||||||
|
|
||||||
proc scanLineHits(
|
proc scanLineHits(
|
||||||
polys: seq[seq[Vec2]],
|
polys: seq[seq[Vec2]],
|
||||||
|
@ -579,7 +579,7 @@ proc fillPolygons*(
|
||||||
var at: Vec2
|
var at: Vec2
|
||||||
if line.intersects(scan, at):
|
if line.intersects(scan, at):
|
||||||
let winding = line.at.y > line.to.y
|
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.add((x, winding))
|
||||||
|
|
||||||
hits.sort(proc(a, b: (float32, bool)): int = cmp(a[0], b[0]))
|
hits.sort(proc(a, b: (float32, bool)): int = cmp(a[0], b[0]))
|
||||||
|
@ -623,103 +623,91 @@ proc fillPolygons*(
|
||||||
{.pop.}
|
{.pop.}
|
||||||
|
|
||||||
proc fillPath*(
|
proc fillPath*(
|
||||||
image: Image,
|
image: Image,
|
||||||
path: Path,
|
path: Path,
|
||||||
color: ColorRGBA
|
color: ColorRGBA
|
||||||
): Image =
|
) =
|
||||||
let polys = commandsToPolygons(path.commands)
|
let
|
||||||
image.fillPolygons(polys, color)
|
polys = commandsToPolygons(path.commands)
|
||||||
|
tmp = fillPolygons(image.wh, polys, color)
|
||||||
|
image.draw(tmp)
|
||||||
|
|
||||||
proc fillPath*(
|
proc fillPath*(
|
||||||
image: Image,
|
image: Image,
|
||||||
path: string,
|
path: Path,
|
||||||
color: ColorRGBA
|
color: ColorRGBA,
|
||||||
): Image =
|
mat: Mat3
|
||||||
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 =
|
|
||||||
var polys = commandsToPolygons(path.commands)
|
var polys = commandsToPolygons(path.commands)
|
||||||
for poly in polys.mitems:
|
for poly in polys.mitems:
|
||||||
for i, p in poly.mpairs:
|
for i, p in poly.mpairs:
|
||||||
poly[i] = mat * p
|
poly[i] = mat * p
|
||||||
image.fillPolygons(polys, color)
|
let tmp = fillPolygons(image.wh, polys, color)
|
||||||
|
image.draw(tmp)
|
||||||
|
|
||||||
proc fillPath*(
|
proc fillPath*(
|
||||||
image: Image,
|
image: Image,
|
||||||
path: string,
|
path: string,
|
||||||
color: ColorRGBA,
|
color: ColorRGBA,
|
||||||
mat: Mat3
|
mat: Mat3
|
||||||
): Image =
|
) =
|
||||||
image.fillPath(parsePath(path), color, mat)
|
image.fillPath(parsePath(path), color, mat)
|
||||||
|
|
||||||
proc strokePath*(
|
proc strokePath*(
|
||||||
image: Image,
|
image: Image,
|
||||||
path: Path,
|
path: Path,
|
||||||
color: ColorRGBA,
|
color: ColorRGBA,
|
||||||
strokeWidth: float32,
|
strokeWidth: float32 = 1.0,
|
||||||
# strokeLocation: StrokeLocation,
|
# strokeLocation: StrokeLocation,
|
||||||
# strokeCap: StorkeCap,
|
# strokeCap: StorkeCap,
|
||||||
# strokeJoin: StorkeJoin
|
# strokeJoin: StorkeJoin
|
||||||
): Image =
|
) =
|
||||||
let polys = commandsToPolygons(path.commands)
|
let
|
||||||
let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
|
polys = commandsToPolygons(path.commands)
|
||||||
let polys2 = strokePolygons(polys, strokeL, strokeR)
|
(strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
|
||||||
image.fillPolygons(polys2, color)
|
polys2 = strokePolygons(polys, strokeL, strokeR)
|
||||||
|
tmp = fillPolygons(image.wh, polys2, color)
|
||||||
|
image.draw(tmp)
|
||||||
|
|
||||||
proc strokePath*(
|
proc strokePath*(
|
||||||
image: Image,
|
image: Image,
|
||||||
path: string,
|
path: string,
|
||||||
color: ColorRGBA,
|
color: ColorRGBA,
|
||||||
strokeWidth: float32
|
strokeWidth: float32
|
||||||
): Image =
|
) =
|
||||||
image.strokePath(parsePath(path), color, strokeWidth)
|
image.strokePath(parsePath(path), color, strokeWidth)
|
||||||
|
|
||||||
proc strokePath*(
|
proc strokePath*(
|
||||||
image: Image,
|
image: Image,
|
||||||
path: string,
|
path: string,
|
||||||
color: ColorRGBA,
|
color: ColorRGBA,
|
||||||
strokeWidth: float32,
|
strokeWidth: float32,
|
||||||
pos: Vec2
|
pos: Vec2
|
||||||
): Image =
|
) =
|
||||||
var polys = commandsToPolygons(parsePath(path).commands)
|
var polys = commandsToPolygons(parsePath(path).commands)
|
||||||
let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
|
let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
|
||||||
var polys2 = strokePolygons(polys, strokeL, strokeR)
|
var polys2 = strokePolygons(polys, strokeL, strokeR)
|
||||||
for poly in polys2.mitems:
|
for poly in polys2.mitems:
|
||||||
for i, p in poly.mpairs:
|
for i, p in poly.mpairs:
|
||||||
poly[i] = p + pos
|
poly[i] = p + pos
|
||||||
image.fillPolygons(polys2, color)
|
let tmp = fillPolygons(image.wh, polys2, color)
|
||||||
|
image.draw(tmp)
|
||||||
|
|
||||||
proc strokePath*(
|
proc strokePath*(
|
||||||
image: Image,
|
image: Image,
|
||||||
path: string,
|
path: string,
|
||||||
color: ColorRGBA,
|
color: ColorRGBA,
|
||||||
strokeWidth: float32,
|
strokeWidth: float32,
|
||||||
mat: Mat3
|
mat: Mat3
|
||||||
): Image =
|
) =
|
||||||
var polys = commandsToPolygons(parsePath(path).commands)
|
var polys = commandsToPolygons(parsePath(path).commands)
|
||||||
let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
|
let (strokeL, strokeR) = (strokeWidth/2, strokeWidth/2)
|
||||||
var polys2 = strokePolygons(polys, strokeL, strokeR)
|
var polys2 = strokePolygons(polys, strokeL, strokeR)
|
||||||
for poly in polys2.mitems:
|
for poly in polys2.mitems:
|
||||||
for i, p in poly.mpairs:
|
for i, p in poly.mpairs:
|
||||||
poly[i] = mat * p
|
poly[i] = mat * p
|
||||||
image.fillPolygons(polys2, color)
|
let tmp = fillPolygons(image.wh, polys2, color)
|
||||||
|
image.draw(tmp)
|
||||||
|
|
||||||
proc addPath*(path: Path, other: Path) =
|
proc addPath*(path: Path, other: Path) =
|
||||||
## Adds a path to the current path.
|
## Adds a path to the current path.
|
||||||
|
|
34
tools/gen_readme.nim
Normal file
34
tools/gen_readme.nim
Normal 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 ".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)
|
Loading…
Reference in a new issue