Code Monkey home page Code Monkey logo

canvgc's Introduction

canvgc

canvgc is a nodejs tool for compiling SVG to html5 CanvasRenderingContext2D commands.

Version

0.1.3

Installation

npm install canvgc

Example

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="2" fill="red"/>
</svg>
{
  "w": 100, 
  "h": 100,
  "d": [
    function ($, p) {
      $.save();
      p.stack++;
      $.strokeStyle = "rgba(0,0,0,0)";
      $.miterLimit = 4;
      $.font = "   10px sans-serif";
      $.translate(0,0);
      $.save();
      p.stack++;
      $.fillStyle = "red";
      $.strokeStyle = "black";
      $.beginPath();
      $.arc(50,50,40,0,6.283185307179586,true);
      $.closePath();
      $.fill();
      $.stroke();
      $.restore();
      p.stack--;
      $.restore();
      p.stack--;
    }
  ], 
  "i": {}
}

Usage (Server Side)

canvgc file.svg file.js # basic conversion

canvgc file.svg file.js --prepend 'window.canvgc={"file":' --append '};' # assign to some variable

canvgc file.svg file.js --prepend 'callback(' --append ');' # call a function with result

canvgc file.svg file.js --chunk 500 # break in to blocks of code (prevent event loop starvation when rendering large files)

Usage (Client Side)

  // basic render at original width & height, without transforms
  function render(canvas, plan){
    var painter = new Painter(plan,function(cb){window.setTimeout(cb,0);}); // can use setImmediate poly-fill
    
    canvas.width = plan.w;
    canvas.height = plan.h;
    
    // need to wait on load images - even for dataURL images.
    painter.loadImages(function(){
      // renders the whole file (even if chunked) in one go
      painter.renderImmediately(canvas.getContext('2d'))
      // if large file & is chunked
      //painter.renderDeferred(canvas.getContext('2d'), function(){ console.log('done');})
    })
  }
  
  render(document.getElementById('canvas'), window.canvgc.file);

Credits

Gabe Lerner ([email protected]) - http://code.google.com/p/canvg/

Michael Thomas - https://code.google.com/p/jscapturecanvas/

Thank You

To the authors of all of the projects on which this depends & is built upon.

canvgc's People

Contributors

nathan-muir 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

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.