@napi-rs/canvas Documentation

repository·main·Indexed 25 days ago

https://github.com/brooooooklyn/canvas

A high-performance Google Skia binding for Node.js via Node-API, providing canvas drawing capabilities with zero system dependencies. It supports 2D context drawing, Lottie animation rendering, Path2D manipulation via PathKit, and GIF encoding. The library provides pre-compiled binaries for various architectures including Linux (glibc/musl), macOS (Intel/Apple Silicon), and Android (arm64).

Tokens
18.8K
Snippets
28
Records
125
Agent score
80%

What's inside @napi-rs/canvas

  1. Identify the @napi-rs/canvas-linux-arm-gnueabihf binary

    main
    The @napi-rs/canvas-linux-arm-gnueabihf package provides the pre-compiled armv7-unknown-linux-gnueabihf binary for use with @napi-rs/canvas. This specific build is intended for ARMv7 Linux environments using the gnueabihf (GNU Embedded Application Binary Interface with Hard Float) ABI.
  2. Identify the @napi-rs/canvas-linux-arm64-musl binary

    main
    The @napi-rs/canvas-linux-arm64-musl package provides the pre-compiled aarch64-unknown-linux-musl binary for the @napi-rs/canvas library. Use this specific package when your target environment is a Linux system running on arm64 architecture using the musl C library (common in lightweight distributions like Alpine Linux).
  3. Load and Render Lottie Animations

    main

    You can render Lottie animations using the LottieAnimation class from @napi-rs/canvas.

    Loading Animations

    • From file: Use LottieAnimation.loadFromFile(path).
    • From JSON string: Use LottieAnimation.loadFromData(jsonString, options) where options can include a resourcePath for external assets.

    Animation Properties

    • duration: Total duration in seconds.
    • fps: Frames per second.
    • frames: Total frame count.
    • width: Animation width.
    • height: Animation height.
    • version: Lottie format version.

    Playback Control

    • seekFrame(frame): Seek to a specific frame.
    • seek(seconds): Seek to a specific time in seconds.

    Rendering

    To render to a canvas, use animation.render(ctx) or animation.render(ctx, { x, y, width, height }) to render with a custom destination rectangle.

    const { createCanvas, LottieAnimation } = require('@napi-rs/canvas')
    
    const animation = LottieAnimation.loadFromFile('animation.json')
    const canvas = createCanvas(animation.width, animation.height)
    const ctx = canvas.getContext('2d')
    
    // Render at original size
    animation.render(ctx)
    
    // Render with custom destination rect
    animation.render(ctx, { x: 0, y: 0, width: 800, height: 600 })
  4. System requirements for @napi-rs/canvas

    main

    Before installing, ensure your environment meets the following requirements:

    CPU Architectures

    • arm64:
      • Linux: Requires cortex-a57 or newer.
      • macOS: Supports all Apple M-series chips.
    • armv7: Requires cortex-a7 or newer.

    glibc

    • Skia requires glibc >= 2.18.
  5. Build the project from source

    main

    To build the project from source without installing additional system dependencies, follow these steps:

    1. Clone the repository with submodules.
    2. Build Skia using the provided script.
    3. Install NPM packages (ignoring scripts) and build the Node.js addon.
    4. Run tests or examples.
    # Clone the code:
    $ git clone --recurse-submodules https://github.com/Brooooooklyn/canvas.git
    $ cd canvas
    
    # Build Skia:
    $ node scripts/build-skia.js
    
    # Install NPM packages, build the Node.js addon:
    $ yarn install --ignore-scripts
    $ yarn build
    
    # All done! Run test cases or examples now:
    $ yarn test
    $ node example/tiger.js
  6. Pull pre-built Skia binaries

    main

    If you only require the Rust components, you can pull pre-built Skia binaries instead of building Skia from source.

    1. Clone the repository with submodules.
    2. Run node scripts/release-skia-binary.mjs --download to pull binaries matching the git hash in the ./skia submodule.
    3. Install dependencies with yarn install --mode=skip-build.
    4. Install clang.
    5. Run yarn build.
    # Clone the code:
    $ git clone --recurse-submodules https://github.com/Brooooooklyn/canvas.git
    $ cd canvas
    
    # Download Skia binaries:
    $ node scripts/release-skia-binary.mjs --download
    
    # Install NPM packages, build the Node.js addon:
    $ npm install -g yarn
    $ yarn install --mode=skip-build
    $ sudo dnf install clang
    $ yarn build
    
    # Run tests or examples:
    $ yarn test
    $ node example/tiger.js