1 var INFINITE, JSONToShape, renderWatermark, util;
3 util = require('./util');
5 JSONToShape = require('./shapes').JSONToShape;
9 renderWatermark = function(ctx, image, scale) {
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);
20 module.exports = function(snapshot, opts) {
21 var allShapes, backgroundShapes, colors, imageSize, s, shapes, watermarkCanvas, watermarkCtx;
25 if (opts.scale == null) {
28 shapes = (function() {
29 var i, len, ref, results;
30 ref = snapshot.shapes;
32 for (i = 0, len = ref.length; i < len; i++) {
34 results.push(JSONToShape(s));
38 backgroundShapes = [];
39 if (snapshot.backgroundShapes) {
40 backgroundShapes = (function() {
41 var i, len, ref, results;
42 ref = snapshot.backgroundShapes;
44 for (i = 0, len = ref.length; i < len; i++) {
46 results.push(JSONToShape(s));
51 if (opts.margin == null) {
59 imageSize = snapshot.imageSize || {
63 colors = snapshot.colors || {
64 background: 'transparent'
66 allShapes = shapes.concat(backgroundShapes);
67 watermarkCanvas = document.createElement('canvas');
68 watermarkCtx = watermarkCanvas.getContext('2d');
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;
75 opts.rect = util.getDefaultImageRect((function() {
78 for (i = 0, len = allShapes.length; i < len; i++) {
80 results.push(s.getBoundingRect(watermarkCtx));
83 })(), imageSize, opts.margin);
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)) {
92 if (opts.watermarkImage) {
93 renderWatermark(watermarkCtx, opts.watermarkImage, opts.watermarkScale);
95 return util.combineCanvases(watermarkCanvas, util.renderShapes(backgroundShapes, opts.rect, opts.scale), util.renderShapes(shapes, opts.rect, opts.scale));