Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to convert JSON to an SVG object?

I'm working on a signature capture applet and one of the requirements is that it stores the signature as an SVG for future use. I am currently using Signature Pad to capture the signature, but it stores it as JSON. Is there anyway to generate an SVG object using JSON, or am I going about this the wrong way?

like image 499
rkingrum Avatar asked Jul 03 '13 18:07

rkingrum


People also ask

Can you convert JSON to SVG?

You can convert your JSON documents from any platform (Windows, Linux, macOS). No registration needed. Just drag and drop your JSON file on upload form, choose the desired output format and click convert button. Once conversion completed you can download your SVG file.

What files can be converted to SVG?

You can use jpg or png image files and turn them into svg files to use with your cutting machine. Learn how you can convert a basic image file, for example a jpg or png file, to svg format that you can use in your Cricut or Silhouette cutting machine.

What file format is SVG?

What is an SVG file? Scalable Vector Graphics (SVG) is a web-friendly vector file format. As opposed to pixel-based raster files like JPEGs, vector files store images via mathematical formulas based on points and lines on a grid.


1 Answers

Thankfully Signature Pad encodes the JSON data in a super readable way. Because SVG is just a text document, we can easily programmatically generate an SVG image given the encoded JSON signature.

As a proof-of-concept, take this regenerated signature from the Pad docs. We just need to generate SVG paths from each object. Looking at the source for how it's done for canvas (search for drawSignature), you can make a simple example in whatever language you choose.

Here's a jsfiddle for it in JavaScript.

function generate_svg(paths) {
  var svg = '';
  svg += '<svg width="198px" height="55px" version="1.1" xmlns="http://www.w3.org/2000/svg">\n';

  for(var i in paths) {
    var path = '';
    path += 'M' + paths[i].mx + ' ' + paths[i].my;   // moveTo
    path += ' L ' + paths[i].lx + ' ' + paths[i].ly; // lineTo
    path += ' Z';                                    // closePath
    svg += '<path d="' + path + '"stroke="blue" stroke-width="2"/>\n';
  }

  svg += '</svg>\n';
  return svg;
}
like image 170
Oliver Avatar answered Oct 21 '22 04:10

Oliver