• Stars
    star
    142
  • Rank 251,057 (Top 6 %)
  • Language
    Nim
  • Created almost 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Nim-based DSL allowing to generate SVG files and GIF animations.

NimSvg Build Status

Nim-based DSL allowing to generate SVG files and GIF animations.

DSL

NimSvg is inspired by Karax, and offers a similar DSL to generate SVG trees. A simple hello world

import nimsvg

buildSvgFile("examples/basic1.svg"):
  svg(width=200, height=200):
    circle(cx=100, cy=100, r=80, stroke="teal", `stroke-width`=4, fill="#EEF")

produces the following SVG:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg width="200" height="200">
  <circle cx="100" cy="100" r="80" stroke="teal" stroke-width="4" fill="#EEF"/>
</svg>

Output:

basic1

The DSL allows to mix tag expressions with regular Nim expressions like variable definitions, for loops, or if statements, which makes it easy to generate SVGs programmatically:

import nimsvg, random

buildSvgFile("examples/basic2.svg"):
  let size = 200
  svg(width=size, height=size):
    for _ in 0 .. 1000:
      let x = rand(size)
      let y = rand(size)
      let radius = rand(5)
      circle(cx=x, cy=y, r=radius, stroke="#111122", fill="#E0E0F0", `fill-opacity`=0.5)

Output:

basic2

NimSvg also allows to render a sequence of SVG files into an animated GIF (requires Imagemagick for the rendering):

import nimsvg

let settings = animSettings("filenameBase", backAndForth=true)
let numFrames = 100

settings.buildAnimation(numFrames) do (i: int) -> Nodes:
  let w = 200
  let h = 200
  buildSvg:
    svg(width=w, height=h):
      let r = 0.4 * w.float * i.float / numFrames.float + 10
      circle(cx=w/2, cy=h/2, r=r, stroke="#445", `stroke-width`=4, fill="#EEF")

Output:

animation1

Special syntax

  • t: The t keyword can be used to create text nodes:

    let svg = buildSvg:
      text(x=0, y=0):
        t "Hello World"
  • embed: The embed keyword can be used to embed the result of other nodes.

    proc sub(): Nodes = buildSvg:
      b()
      c()
    
    let svg = buildSvg:
      # produces tags <a><b><c><d>
      a()
      embed sub()
      d()

Gallery

Click on an image to see the corresponding implementation.

spinner1 spinner2 spinner3

Example algorithm visualization of rust-array-stump:

algo-viz-1

More Repositories

1

NimData

DataFrame API written in Nim, enabling fast out-of-core data processing
Nim
326
star
2

PandasDataFrameGUI

A minimalistic GUI for analyzing Pandas DataFrames.
Python
314
star
3

tabloo

Minimalistic dashboard app for visualizing tabular data
TypeScript
149
star
4

yachalk

🖍️ Terminal string styling done right
Python
141
star
5

oop_utils

Nim macros for building OOP class hierarchies.
Nim
36
star
6

nim-electron-karax

A minimal Nim+Karax+Electron application
Nim
21
star
7

nim-heap

Simple binary heap implementation in Nim
Nim
20
star
8

HiCS

Efficient multivariate correlation measure / high contrast subspace miner.
Nim
18
star
9

KaraxExamples

Some experiments with Karax
Nim
16
star
10

typed_argparse

💡 type-safe args for argparse without much refactoring
Python
11
star
11

nim-stringinterpolation

String interpolation with printf syntax
Nim
11
star
12

gervill

Java
10
star
13

nim-matplotlib

Simple wrapper of matplotlib for Nim
Nim
6
star
14

Hydra

Distributed computing experiments in Nim
Nim
6
star
15

watchcode

Generic tool to solve the modify + re-run problem
Python
4
star
16

kadro

Nim
4
star
17

NimExperiments

Just a few experiments with Nim
Nim
4
star
18

rust-array-stump

A data structure mixing dynamic array and sorted set semantics -- O(sqrt N) insert/remove, O(1) rank/next/prev
Rust
4
star
19

DeepAudio

Nim
3
star
20

nim-ovr

Nim bindings for libOVR (Oculus Rift)
Nim
3
star
21

NimNimble

Nim
2
star
22

GodotExperiments

Rust
2
star
23

ScalaOculusRiftExample

Simple Oculus Rift example based on JOVR
Scala
2
star
24

ChinaStudyRawData

Attempt to make the original data of The China Study more accessible
Python
2
star
25

NimOculusRiftExample

Simple Oculus Rift example written in Nim
Nim
2
star
26

NotemarksLegacy

A markdown based note taking app -- WIP
TypeScript
1
star
27

ChordViewer

Tool to visualize guitar chord inversions with color-coded intervals
Python
1
star
28

SimpleLanguageBenchmarks

Benchmark collection of random problem + language combinations.
Python
1
star
29

GodotConcept

GDScript
1
star
30

RustFaustExperiments

Rust
1
star
31

pyTranscribe

pyTranscribe is a audio/video player featuring constant pitch/tempo adjustment
Python
1
star