Giter Club home page Giter Club logo

nanovg-zig's Introduction

NanoVG - Zig Version

This is a rewrite of the original NanoVG library using the Zig programming language.

NanoVG is a small anti-aliased hardware-accelerated vector graphics library. It has a lean API modeled after the HTML5 canvas API. It is aimed to be a practical and fun toolset for building scalable user interfaces or any other real time visualizations.

Screenshot

screenshot of some text rendered with the example program

Examples

There's a WebAssembly example using WebGL which you can immediately try here: https://fabioarnold.github.io/nanovg-zig. The source for this example can be found in example_wasm.zig and can be built by running zig build -Dtarget=wasm32-freestanding.

A native cross-platform example using GLFW can be found in example_glfw.zig and can be built by running zig build. It requires GLFW to be installed. On Windows vcpkg is an additional requirement.

For an example on how to use nanovg-zig in your project's build.zig you can take a look at https://github.com/fabioarnold/MiniPixel/blob/main/build.zig.

Features

  • Basic shapes: rect, rounded rect, ellipse, arc
  • Arbitrary paths of bezier curves with holes
  • Arbitrary stack-based 2D transforms
  • Strokes with different types of caps and joins
  • Fills with gradient support
  • Types of gradients: linear, box (useful for shadows), radial
  • Text with automatic linebreaks and blurring
  • Images as pattern for fills and strokes

Features exclusive to the Zig version

  • Clip paths

Usage

The NanoVG API is modeled loosely on the HTML5 canvas API. If you know canvas, you're up to speed with NanoVG in no time.

Creating a drawing context

The drawing context is created using a backend-specific initialization function. If you're using the OpenGL backend the context is created as follows:

const nvg = @import("nanovg");
...
var vg = try nvg.gl.init(allocator, .{
	.debug = true,
});
defer vg.deinit();

The second parameter defines options for creating the renderer.

  • antialias means that the renderer adjusts the geometry to include anti-aliasing. If you're using MSAA, you can omit this option to be default initialized as false.
  • stencil_strokes means that the render uses better quality rendering for (overlapping) strokes. The quality is mostly visible on wider strokes. If you want speed, you can omit this option to be default initialized as false.

Currently, there is an OpenGL backend for NanoVG: nanovg_gl.zig for OpenGL 2.0 and WebGL. WebGL is automatically chosen when targeting WebAssembly. There's an interface called Params defined in internal.zig, which can be implemented by additional backends.

NOTE: The render target you're rendering to must have a stencil buffer.

Drawing shapes with NanoVG

Drawing a simple shape using NanoVG consists of four steps:

  1. begin a new shape,
  2. define the path to draw,
  3. set fill or stroke,
  4. and finally fill or stroke the path.
vg.beginPath();
vg.rect(100,100, 120,30);
vg.fillColor(nvg.rgba(255,192,0,255));
vg.fill();

Calling beginPath() will clear any existing paths and start drawing from a blank slate. There are a number of functions to define the path to draw, such as rectangle, rounded rectangle and ellipse, or you can use the common moveTo, lineTo, bezierTo and arcTo API to compose a path step-by-step.

Understanding Composite Paths

Because of the way the rendering backend is built in NanoVG, drawing a composite path - that is a path consisting of multiple paths defining holes and fills - is a bit more involved. NanoVG uses the even-odd filling rule and by default the paths are wound in counterclockwise order. Keep that in mind when drawing using the low-level drawing API. In order to wind one of the predefined shapes as a hole, you should call pathWinding(nvg.Winding.solidity(.hole)), or pathWinding(.cw) after defining the path.

vg.beginPath();
vg.rect(100,100, 120,30);
vg.circle(120,120, 5);
vg.pathWinding(.cw); // Mark circle as a hole.
vg.fillColor(nvg.rgba(255,192,0,255));
vg.fill();

API Reference

See nanovg.zig for an API reference.

Projects using nanovg-zig

License

The original library and this rewrite are licensed under the zlib license

Fonts used in the examples:

Links

Uses stb_truetype for font rendering. Uses stb_image for image loading.

nanovg-zig's People

Contributors

bel8z avatar bluealmost avatar cztomsik avatar fabioarnold avatar iacore avatar ieeemma avatar leroycep avatar rofrol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

nanovg-zig's Issues

Build error on macOS related to @sin/@cos usage

zig-build fails on macOS (12.3.1) with the following:

error(link): undefined reference to symbol '_sincosf'

Verbose output:

~/Desktop/scratch/nanovg-zig main*
❯ zig build --verbose
/Users/rudedogg/Desktop/contrib/zig-nightly/zig build-exe /Users/rudedogg/Desktop/scratch/nanovg-zig/examples/example_glfw.zig /Users/rudedogg/Desktop/scratch/nanovg-zig/lib/gl2/src/glad.c -I/usr/local/Cellar/glfw/3.3.7/include -L/usr/local/Cellar/glfw/3.3.7/lib -lglfw -cflags -DSTBI_NO_STDIO -fno-stack-protector -- /Users/rudedogg/Desktop/scratch/nanovg-zig/examples/stb_image_write.c -cflags -DFONS_NO_STDIO -fno-stack-protector -- /Users/rudedogg/Desktop/scratch/nanovg-zig/src/fontstash.c -cflags -DSTBI_NO_STDIO -fno-stack-protector -- /Users/rudedogg/Desktop/scratch/nanovg-zig/src/stb_image.c -lc --cache-dir /Users/rudedogg/Desktop/scratch/nanovg-zig/zig-cache --global-cache-dir /Users/rudedogg/.cache/zig --name main --pkg-begin nanovg /Users/rudedogg/Desktop/scratch/nanovg-zig/src/nanovg.zig --pkg-end -I /Users/rudedogg/Desktop/scratch/nanovg-zig/lib/gl2/include -I /Users/rudedogg/Desktop/scratch/nanovg-zig/examples -I /Users/rudedogg/Desktop/scratch/nanovg-zig/src -framework OpenGL --enable-cache 
LLVM Emit Object... error(link): undefined reference to symbol '_sincosf'
error(link):   first referenced in '/Users/rudedogg/Desktop/scratch/nanovg-zig/zig-cache/o/7066d86dec83eac5d133025cdd15134b/main.o'
error: UndefinedSymbolReference
main...The following command exited with error code 1:
/Users/rudedogg/Desktop/contrib/zig-nightly/zig build-exe /Users/rudedogg/Desktop/scratch/nanovg-zig/examples/example_glfw.zig /Users/rudedogg/Desktop/scratch/nanovg-zig/lib/gl2/src/glad.c -I/usr/local/Cellar/glfw/3.3.7/include -L/usr/local/Cellar/glfw/3.3.7/lib -lglfw -cflags -DSTBI_NO_STDIO -fno-stack-protector -- /Users/rudedogg/Desktop/scratch/nanovg-zig/examples/stb_image_write.c -cflags -DFONS_NO_STDIO -fno-stack-protector -- /Users/rudedogg/Desktop/scratch/nanovg-zig/src/fontstash.c -cflags -DSTBI_NO_STDIO -fno-stack-protector -- /Users/rudedogg/Desktop/scratch/nanovg-zig/src/stb_image.c -lc --cache-dir /Users/rudedogg/Desktop/scratch/nanovg-zig/zig-cache --global-cache-dir /Users/rudedogg/.cache/zig --name main --pkg-begin nanovg /Users/rudedogg/Desktop/scratch/nanovg-zig/src/nanovg.zig --pkg-end -I /Users/rudedogg/Desktop/scratch/nanovg-zig/lib/gl2/include -I /Users/rudedogg/Desktop/scratch/nanovg-zig/examples -I /Users/rudedogg/Desktop/scratch/nanovg-zig/src -framework OpenGL --enable-cache 
error: the following build command failed with exit code 1:
/Users/rudedogg/Desktop/scratch/nanovg-zig/zig-cache/o/d35cab824ee2e19ec076977315565bd8/build /Users/rudedogg/Desktop/contrib/zig-nightly/zig /Users/rudedogg/Desktop/scratch/nanovg-zig /Users/rudedogg/Desktop/scratch/nanovg-zig/zig-cache /Users/rudedogg/.cache/zig --verbose

On Discord prime31 suggested replacing all uses of @sin with std.math.sin, which fixed the build error.
This seems to be the same issue as ziglang/zig#10318

Wasm build but does not run

Hi

I'm trying to run the wasm version (zig build -Dtarget=wasm32-freestanding) the build seems ok as no error returned.

But when I run the index.html (live server on vcode) it complains no finding instance.exports.onInit();
From what I see it has built a very simple wasm file with only "memory" into the exports.

I'm doing this on Mac (M1), maybe this is the issue? Am I missing something super obvious?

Thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.