sigma.renderers.snapshot.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ;(function(undefined) {
  2. /**
  3. * Sigma Renderer Snapshot Utility
  4. * ================================
  5. *
  6. * The aim of this plugin is to enable users to retrieve a static image
  7. * of the graph being rendered.
  8. *
  9. * Author: Guillaume Plique (Yomguithereal)
  10. * Version: 0.0.1
  11. */
  12. // Terminating if sigma were not to be found
  13. if (typeof sigma === 'undefined')
  14. throw 'sigma.renderers.snapshot: sigma not in scope.';
  15. // Constants
  16. var CONTEXTS = ['scene', 'edges', 'nodes', 'labels'],
  17. TYPES = {
  18. png: 'image/png',
  19. jpg: 'image/jpeg',
  20. gif: 'image/gif',
  21. tiff: 'image/tiff'
  22. };
  23. // Utilities
  24. function download(dataUrl, extension, filename) {
  25. // Anchor
  26. var anchor = document.createElement('a');
  27. anchor.setAttribute('href', dataUrl);
  28. anchor.setAttribute('download', filename || 'graph.' + extension);
  29. // Click event
  30. var event = document.createEvent('MouseEvent');
  31. event.initMouseEvent('click', true, false, window, 0, 0, 0 ,0, 0,
  32. false, false, false, false, 0, null);
  33. anchor.dispatchEvent(event);
  34. delete anchor;
  35. }
  36. // Main function
  37. function snapshot(params) {
  38. params = params || {};
  39. // Enforcing
  40. if (params.format && !(params.format in TYPES))
  41. throw Error('sigma.renderers.snaphot: unsupported format "' +
  42. params.format + '".');
  43. var self = this,
  44. webgl = this instanceof sigma.renderers.webgl,
  45. doneContexts = [];
  46. // Creating a false canvas where we'll merge the other
  47. var merged = document.createElement('canvas'),
  48. mergedContext = merged.getContext('2d'),
  49. sized = false;
  50. // Iterating through context
  51. CONTEXTS.forEach(function(name) {
  52. if (!self.contexts[name])
  53. return;
  54. if (params.labels === false && name === 'labels')
  55. return;
  56. var canvas = self.domElements[name] || self.domElements['scene'],
  57. context = self.contexts[name];
  58. if (~doneContexts.indexOf(context))
  59. return;
  60. if (!sized) {
  61. merged.width = webgl && context instanceof WebGLRenderingContext ?
  62. canvas.width / 2 :
  63. canvas.width;
  64. merged.height = webgl && context instanceof WebGLRenderingContext ?
  65. canvas.height / 2 :
  66. canvas.height
  67. sized = true;
  68. // Do we want a background color?
  69. if (params.background) {
  70. mergedContext.rect(0, 0, merged.width, merged.height);
  71. mergedContext.fillStyle = params.background;
  72. mergedContext.fill();
  73. }
  74. }
  75. if (context instanceof WebGLRenderingContext)
  76. mergedContext.drawImage(canvas, 0, 0,
  77. canvas.width / 2, canvas.height / 2);
  78. else
  79. mergedContext.drawImage(canvas, 0, 0);
  80. doneContexts.push(context);
  81. });
  82. var dataUrl = merged.toDataURL(TYPES[params.format || 'png']);
  83. if (params.download)
  84. download(
  85. dataUrl,
  86. params.format || 'png',
  87. params.filename
  88. );
  89. // Cleaning
  90. delete mergedContext;
  91. delete merged;
  92. delete doneContexts;
  93. return dataUrl;
  94. }
  95. // Extending canvas and webl renderers
  96. sigma.renderers.canvas.prototype.snapshot = snapshot;
  97. sigma.renderers.webgl.prototype.snapshot = snapshot;
  98. }).call(this);