Remove treeview-1.3 since it is unused.
[openemr.git] / interface / static / literallycanvas-0-4-13 / js / core / renderSnapshotToImage.js
blobc9ab84120ae528b9820aebc74add63dc6f93ad3b
1 var INFINITE, JSONToShape, renderWatermark, util;
3 util = require('./util');
5 JSONToShape = require('./shapes').JSONToShape;
7 INFINITE = 'infinite';
9 renderWatermark = function(ctx, image, scale) {
10   if (!image.width) {
11     return;
12   }
13   ctx.save();
14   ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
15   ctx.scale(scale, scale);
16   ctx.drawImage(image, -image.width / 2, -image.height / 2);
17   return ctx.restore();
20 module.exports = function(snapshot, opts) {
21   var allShapes, backgroundShapes, colors, imageSize, s, shapes, watermarkCanvas, watermarkCtx;
22   if (opts == null) {
23     opts = {};
24   }
25   if (opts.scale == null) {
26     opts.scale = 1;
27   }
28   shapes = (function() {
29     var i, len, ref, results;
30     ref = snapshot.shapes;
31     results = [];
32     for (i = 0, len = ref.length; i < len; i++) {
33       s = ref[i];
34       results.push(JSONToShape(s));
35     }
36     return results;
37   })();
38   backgroundShapes = [];
39   if (snapshot.backgroundShapes) {
40     backgroundShapes = (function() {
41       var i, len, ref, results;
42       ref = snapshot.backgroundShapes;
43       results = [];
44       for (i = 0, len = ref.length; i < len; i++) {
45         s = ref[i];
46         results.push(JSONToShape(s));
47       }
48       return results;
49     })();
50   }
51   if (opts.margin == null) {
52     opts.margin = {
53       top: 0,
54       right: 0,
55       bottom: 0,
56       left: 0
57     };
58   }
59   imageSize = snapshot.imageSize || {
60     width: INFINITE,
61     height: INFINITE
62   };
63   colors = snapshot.colors || {
64     background: 'transparent'
65   };
66   allShapes = shapes.concat(backgroundShapes);
67   watermarkCanvas = document.createElement('canvas');
68   watermarkCtx = watermarkCanvas.getContext('2d');
69   if (opts.rect) {
70     opts.rect.x -= opts.margin.left;
71     opts.rect.y -= opts.margin.top;
72     opts.rect.width += opts.margin.left + opts.margin.right;
73     opts.rect.height += opts.margin.top + opts.margin.bottom;
74   } else {
75     opts.rect = util.getDefaultImageRect((function() {
76       var i, len, results;
77       results = [];
78       for (i = 0, len = allShapes.length; i < len; i++) {
79         s = allShapes[i];
80         results.push(s.getBoundingRect(watermarkCtx));
81       }
82       return results;
83     })(), imageSize, opts.margin);
84   }
85   watermarkCanvas.width = opts.rect.width * opts.scale;
86   watermarkCanvas.height = opts.rect.height * opts.scale;
87   watermarkCtx.fillStyle = colors.background;
88   watermarkCtx.fillRect(0, 0, watermarkCanvas.width, watermarkCanvas.height);
89   if (!(opts.rect.width && opts.rect.height)) {
90     return null;
91   }
92   if (opts.watermarkImage) {
93     renderWatermark(watermarkCtx, opts.watermarkImage, opts.watermarkScale);
94   }
95   return util.combineCanvases(watermarkCanvas, util.renderShapes(backgroundShapes, opts.rect, opts.scale), util.renderShapes(shapes, opts.rect, opts.scale));