|
| 1 | +/** |
| 2 | +* Copyright 2012-2016, Plotly, Inc. |
| 3 | +* All rights reserved. |
| 4 | +* |
| 5 | +* This source code is licensed under the MIT license found in the |
| 6 | +* LICENSE file in the root directory of this source tree. |
| 7 | +*/ |
| 8 | + |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +var Plotly = require('../plotly'); |
| 12 | + |
| 13 | +var isNumeric = require('fast-isnumeric'); |
| 14 | + |
| 15 | +/** |
| 16 | + * @param {object} gd figure Object |
| 17 | + * @param {object} opts option object |
| 18 | + * @param opts.format 'jpeg' | 'png' | 'webp' | 'svg' |
| 19 | + * @param opts.width width of snapshot in px |
| 20 | + * @param opts.height height of snapshot in px |
| 21 | + */ |
| 22 | +function toImage(gd, opts) { |
| 23 | + var Snapshot = require('../snapshot'); |
| 24 | + |
| 25 | + var promise = new Promise(function(resolve, reject) { |
| 26 | + // check for undefined opts |
| 27 | + opts = opts || {}; |
| 28 | + // default to png |
| 29 | + opts.format = opts.format || 'png'; |
| 30 | + |
| 31 | + var isSizeGood = function(size) { |
| 32 | + // undefined and null are valid options |
| 33 | + if(size === undefined || size === null) { |
| 34 | + return true; |
| 35 | + } |
| 36 | + |
| 37 | + if(isNumeric(size) && size > 1) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + }; |
| 41 | + |
| 42 | + if(!isSizeGood(opts.width) || !isSizeGood(opts.height)) { |
| 43 | + reject(new Error('Height and width should be pixel values.')); |
| 44 | + } |
| 45 | + |
| 46 | + // first clone the GD so we can operate in a clean environment |
| 47 | + var clone = Snapshot.clone(gd, {format: 'png', height: opts.height, width: opts.width}); |
| 48 | + var clonedGd = clone.td; |
| 49 | + |
| 50 | + // put the cloned div somewhere off screen before attaching to DOM |
| 51 | + clonedGd.style.position = 'absolute'; |
| 52 | + clonedGd.style.left = '-5000px'; |
| 53 | + document.body.appendChild(clonedGd); |
| 54 | + |
| 55 | + function wait() { |
| 56 | + var delay = Snapshot.getDelay(clonedGd._fullLayout); |
| 57 | + |
| 58 | + return new Promise(function(resolve, reject) { |
| 59 | + setTimeout(function() { |
| 60 | + var svg = Snapshot.toSVG(clonedGd); |
| 61 | + |
| 62 | + var canvasContainer = window.document.createElement('div'); |
| 63 | + var canvas = window.document.createElement('canvas'); |
| 64 | + |
| 65 | + // window.document.body.appendChild(canvasContainer); |
| 66 | + canvasContainer.appendChild(canvas); |
| 67 | + |
| 68 | + canvasContainer.id = Plotly.Lib.randstr(); |
| 69 | + canvas.id = Plotly.Lib.randstr(); |
| 70 | + |
| 71 | + Snapshot.svgToImg({ |
| 72 | + format: opts.format, |
| 73 | + width: clonedGd._fullLayout.width, |
| 74 | + height: clonedGd._fullLayout.height, |
| 75 | + canvas: canvas, |
| 76 | + svg: svg, |
| 77 | + // ask svgToImg to return a Promise |
| 78 | + // rather than EventEmitter |
| 79 | + // leave EventEmitter for backward |
| 80 | + // compatibility |
| 81 | + promise: true |
| 82 | + }).then(function(url) { |
| 83 | + if(clonedGd) clonedGd.remove(); |
| 84 | + resolve(url); |
| 85 | + }).catch(function(err) { |
| 86 | + reject(err); |
| 87 | + }); |
| 88 | + }, delay); |
| 89 | + }); |
| 90 | + } |
| 91 | + |
| 92 | + var redrawFunc = Snapshot.getRedrawFunc(clonedGd); |
| 93 | + |
| 94 | + Plotly.plot(clonedGd, clone.data, clone.layout, clone.config) |
| 95 | + // TODO: the following is Plotly.Plots.redrawText but without the waiting. |
| 96 | + // we shouldn't need to do this, but in *occasional* cases we do. Figure |
| 97 | + // out why and take it out. |
| 98 | + |
| 99 | + // not sure the above TODO makes sense anymore since |
| 100 | + // we have converted to promises |
| 101 | + .then(redrawFunc) |
| 102 | + .then(wait) |
| 103 | + .then(function(url) { resolve(url); }) |
| 104 | + .catch(function(err) { |
| 105 | + reject(err); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + return promise; |
| 110 | +} |
| 111 | + |
| 112 | +module.exports = toImage; |
0 commit comments