Added literallycanvas and react libraries.
[openemr.git] / library / js / literallycanvas / js / core / renderSnapshotToSVG.js
blobaa21a540336b6b6c78a2c1436bfa0b6879f1fc4d
1 var INFINITE, JSONToShape, util;
3 util = require('./util');
5 JSONToShape = require('./shapes').JSONToShape;
7 INFINITE = 'infinite';
9 module.exports = function(snapshot, opts) {
10   var allShapes, backgroundShapes, colors, ctx, dummyCanvas, imageSize, s, shapes;
11   if (opts == null) {
12     opts = {};
13   }
14   shapes = (function() {
15     var i, len, ref, results;
16     ref = snapshot.shapes;
17     results = [];
18     for (i = 0, len = ref.length; i < len; i++) {
19       s = ref[i];
20       results.push(JSONToShape(s));
21     }
22     return results;
23   })();
24   backgroundShapes = [];
25   if (snapshot.backgroundShapes) {
26     backgroundShapes = (function() {
27       var i, len, ref, results;
28       ref = snapshot.backgroundShapes;
29       results = [];
30       for (i = 0, len = ref.length; i < len; i++) {
31         s = ref[i];
32         results.push(JSONToShape(s));
33       }
34       return results;
35     })();
36   }
37   if (opts.margin == null) {
38     opts.margin = {
39       top: 0,
40       right: 0,
41       bottom: 0,
42       left: 0
43     };
44   }
45   imageSize = snapshot.imageSize || {
46     width: INFINITE,
47     height: INFINITE
48   };
49   colors = snapshot.colors || {
50     background: 'transparent'
51   };
52   allShapes = shapes.concat(backgroundShapes);
53   dummyCanvas = document.createElement('canvas');
54   ctx = dummyCanvas.getContext('2d');
55   if (opts.rect) {
56     opts.rect.x -= opts.margin.left;
57     opts.rect.y -= opts.margin.top;
58     opts.rect.width += opts.margin.left + opts.margin.right;
59     opts.rect.height += opts.margin.top + opts.margin.bottom;
60   } else {
61     opts.rect = util.getDefaultImageRect((function() {
62       var i, len, results;
63       results = [];
64       for (i = 0, len = allShapes.length; i < len; i++) {
65         s = allShapes[i];
66         results.push(s.getBoundingRect(ctx));
67       }
68       return results;
69     })(), imageSize, opts.margin);
70   }
71   return LC.renderShapesToSVG(backgroundShapes.concat(shapes), opts.rect, colors.background);