admin管理员组

文章数量:1415111

Is it possible to export Kinetic JS object to SVG?

Or workaround is to convert Kintetic JS's canvas to SVG.

EDIT:

The best is to use fabricJS since it supports canvas to SVG rendering while working with fabric objects.

I accepted Phrogz's answer since it also does conversion canvas to svg without using some other library for drawing on canvas.

EDIT 2: OK, i messed up, Phrogz's library is wrapper around canvas element so you use it's methods to draw on canvas (I thought that it just 'listens' on canvas and creates SVG paths). So the best solution is fabricJS definitely.

Is it possible to export Kinetic JS object to SVG?

Or workaround is to convert Kintetic JS's canvas to SVG.

EDIT:

The best is to use fabricJS since it supports canvas to SVG rendering while working with fabric objects.

I accepted Phrogz's answer since it also does conversion canvas to svg without using some other library for drawing on canvas.

EDIT 2: OK, i messed up, Phrogz's library is wrapper around canvas element so you use it's methods to draw on canvas (I thought that it just 'listens' on canvas and creates SVG paths). So the best solution is fabricJS definitely.

Share Improve this question edited Apr 17, 2012 at 19:23 vale4674 asked Apr 16, 2012 at 16:39 vale4674vale4674 4,27113 gold badges50 silver badges72 bronze badges 8
  • Kinect is an HTML5 Canvas library. HTML5 Canvas is a bitmap format (sometimes drawn to using vector-like mands). SVG is a vector format. The simplest answer is to take the output of your canvas and use it as an image in an SVG file. What are you really trying to do? – Phrogz Commented Apr 16, 2012 at 17:07
  • Thx, but that is not an option because I need SVG paths, not base64 png because of the file size after. – vale4674 Commented Apr 16, 2012 at 18:16
  • Are you canvas drawings prised only of path mands that could be captured? Can you alter the source of the JS populating the canvas? – Phrogz Commented Apr 16, 2012 at 18:25
  • We are trying to make a drawing application which could export to SVG. You say that I could write my own SVG renderer based on my JS data since I know what I am drawing on canvas? – vale4674 Commented Apr 16, 2012 at 18:53
  • You could swap out your HTML5 graphics context with a wrapper object that performs the same actions, but also records all the paths and strokes and whatnot. (Either per update, or storing only the mands for the last update.) You could then translate the recorded mands to SVG mands. When I get time I'll work up an answer based on this. – Phrogz Commented Apr 16, 2012 at 19:50
 |  Show 3 more ments

3 Answers 3

Reset to default 4

The best solution is to use Fabric.js!

I've created an alpha version of a library that allows you to extend an HTML5 Canvas Context such that it tracks all vector drawing mands and stores them as an array SVG elements in a ctx.svgObjects property of the context.

You can see the library in action here: http://phrogz/svg/HTML5CanvasRecordsSVG.html
The demo turns on recording, draws a few shapes to the HTML5 Canvas, and then appends the 'recorded' SVG objects to an SVG container next door.

In general the library:

  1. Keeps track of the current context transformation via an SVGMatrix object. (This is because the HTML5 Context api lets you set the current transform to a matrix, but does not let you get the current matrix.) It does this by intercepting calls like translate() and rotate() and updating the matrix accordingly.
  2. Intercepts beginPath() and creates a new SVG Path element, and then intercepts further mands like moveTo() and lineTo() in order to add the equivalent path mands to the SVG path.
    • Note: not all path mands are supported or tested in the library at the time of this writing.
  3. Intercepts fill() and stroke() to add a copy of the current SVG <path> to the svgObjects array, setting the current transformation matrix, fill and stroke styles.
    • Note: not all stroke styles (lineCap, lineJoin, miterLimit) are supported as of this writing.
    • Note: calling fill() followed by stroke() creates two separate SVG elements; there is no optimization to differentiate this specific case from stroke/fill, or changing the transform or path between calls.
  4. Intercepts fillRect() and strokeRect() to create an SVG <rect> element.

More work could be done on this library to flesh out all the mands (not just path mands, but also things like fillText()). But it's not something that I personally need, so I'm not inclined to spend hours carrying it over the finish line.

basicly you can convert the canvas to base64 png and then put it on svg

maybe this could help you

http://svgkit.sourceforge/tests/canvas_tests.html

本文标签: javascriptExport KinteticJS drawing to SVGStack Overflow