diff --git a/README.md b/README.md index 88d3ab4..4a44802 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/blur.png b/examples/blur.png index 0f4e20b..a56cd1b 100644 Binary files a/examples/blur.png and b/examples/blur.png differ diff --git a/examples/heart.nim b/examples/heart.nim new file mode 100644 index 0000000..6397cee --- /dev/null +++ b/examples/heart.nim @@ -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") diff --git a/examples/heart.png b/examples/heart.png new file mode 100644 index 0000000..b7ca705 Binary files /dev/null and b/examples/heart.png differ diff --git a/examples/rounded_rectangle.nim b/examples/rounded_rectangle.nim index 164a76b..63bb3cb 100644 --- a/examples/rounded_rectangle.nim +++ b/examples/rounded_rectangle.nim @@ -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") diff --git a/examples/rounded_rectangle.png b/examples/rounded_rectangle.png index d79c77c..9bd100f 100644 Binary files a/examples/rounded_rectangle.png and b/examples/rounded_rectangle.png differ diff --git a/examples/shadow.nim b/examples/shadow.nim new file mode 100644 index 0000000..db78970 --- /dev/null +++ b/examples/shadow.nim @@ -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") diff --git a/examples/shadow.png b/examples/shadow.png new file mode 100644 index 0000000..4bcbae3 Binary files /dev/null and b/examples/shadow.png differ diff --git a/examples/tiger.png b/examples/tiger.png index da08162..f32886d 100644 Binary files a/examples/tiger.png and b/examples/tiger.png differ diff --git a/tools/gen_readme.nim b/tools/gen_readme.nim index f67ba6e..27cb9dc 100644 --- a/tools/gen_readme.nim +++ b/tools/gen_readme.nim @@ -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")