More readme and examples.

This commit is contained in:
treeform 2021-02-13 20:45:27 -08:00
parent a4a3e3a728
commit 409848a4ad
10 changed files with 132 additions and 43 deletions

View file

@ -23,6 +23,70 @@ Features:
## Examples
### Square
[examples/square.nim](examples/square.nim)
```nim
var p: Path
p.rect(50, 50, 100, 100)
image.fillPath(p, rgba(255, 0, 0, 255))
```
![example output](examples/square.png)
### Rounded rectangle
[examples/rounded_rectangle.nim](examples/rounded_rectangle.nim)
```nim
let
x = 50.0
y = 50.0
w = 100.0
h = 100.0
r = 25.0
var path: Path
path.roundedRect(vec2(x, y), vec2(w, h), r, r, r, r)
image.fillPath(path, rgba(0, 255, 0, 255))
```
![example output](examples/rounded_rectangle.png)
### Heart
[examples/heart.nim](examples/heart.nim)
```nim
image.fillPath(
"""
M 20 60
A 40 40 90 0 1 100 60
A 40 40 90 0 1 180 60
Q 180 120 100 180
Q 20 120 20 60
z
""",
parseHtmlColor("#FC427B").rgba
)
```
![example output](examples/heart.png)
### Shadow
[examples/shadow.nim](examples/shadow.nim)
```nim
var p: Path
p.polygon(100, 100, 70, sides=8)
p.closePath()
var polyImage = newImage(200, 200)
polyImage.fillPath(p, rgba(255, 255, 255, 255))
image.draw(polyImage.shadow(
offset = vec2(2, 2),
spread = 2,
blur = 10,
color = rgba(0, 0, 0, 200)
))
image.draw(polyImage)
```
![example output](examples/shadow.png)
### Blur
[examples/blur.nim](examples/blur.nim)
```nim
@ -41,33 +105,6 @@ image.draw(blur)
```
![example output](examples/blur.png)
### Rounded rectangle
[examples/rounded_rectangle.nim](examples/rounded_rectangle.nim)
```nim
let
x = 50.0
y = 50.0
w = 100.0
h = 100.0
r = 25.0
var path: Path
path.roundedRect(vec2(x, y), vec2(w, h), r, r, r, r)
image.fillPath(path, rgba(255, 0, 0, 255))
```
![example output](examples/rounded_rectangle.png)
### Square
[examples/square.nim](examples/square.nim)
```nim
var p: Path
p.rect(50, 50, 100, 100)
image.fillPath(p, rgba(255, 0, 0, 255))
```
![example output](examples/square.png)
### Tiger
[examples/tiger.nim](examples/tiger.nim)
```nim

Binary file not shown.

Before

Width:  |  Height:  |  Size: 88 KiB

After

Width:  |  Height:  |  Size: 88 KiB

20
examples/heart.nim Normal file
View file

@ -0,0 +1,20 @@
import pixie, chroma, vmath
let
image = newImage(200, 200)
image.fill(rgba(255, 255, 255, 255))
image.fillPath(
"""
M 20 60
A 40 40 90 0 1 100 60
A 40 40 90 0 1 180 60
Q 180 120 100 180
Q 20 120 20 60
z
""",
parseHtmlColor("#FC427B").rgba
)
image.writeFile("examples/heart.png")

BIN
examples/heart.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

View file

@ -13,6 +13,6 @@ let
var path: Path
path.roundedRect(vec2(x, y), vec2(w, h), r, r, r, r)
image.fillPath(path, rgba(255, 0, 0, 255))
image.fillPath(path, rgba(0, 255, 0, 255))
image.writeFile("examples/rounded_rectangle.png")

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.1 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

24
examples/shadow.nim Normal file
View file

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

BIN
examples/shadow.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 42 KiB

After

Width:  |  Height:  |  Size: 42 KiB

View file

@ -1,4 +1,4 @@
import os, strutils, osproc
import os, strutils, osproc, sequtils, algorithm
proc cutBetween(str, a, b: string): string =
let
@ -10,20 +10,28 @@ proc cutBetween(str, a, b: string): string =
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("image.fill(rgba(255, 255, 255, 255))", "image.writeFile")
if innerCode != "":
let path = path.replace("\\", "/")
md.add "### " & path.splitFile().name.replace("_", " ").capitalizeAscii()
md.add "[" & path & "](" & path & ")"
md.add "```nim"
md.add innerCode.strip()
md.add "```"
md.add "![example output](" & path.replace(".nim", ".png").replace("\\", "/") & ")"
md.add ""
var exampleFiles = [
"examples/square.nim",
"examples/rounded_rectangle.nim",
"examples/heart.nim",
"examples/shadow.nim",
"examples/blur.nim",
"examples/tiger.nim"
]
for path in exampleFiles:
discard execCmd("nim c -r -d:danger " & path)
let code = readFile(path)
let innerCode = code.cutBetween("image.fill(rgba(255, 255, 255, 255))", "image.writeFile")
if innerCode != "":
let path = path.replace("\\", "/")
md.add "### " & path.splitFile().name.replace("_", " ").capitalizeAscii()
md.add "[" & path & "](" & path & ")"
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")