vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.6 KiB

  1. /**
  2. * Created by Alex on 26-Feb-15.
  3. */
  4. if (typeof window !== 'undefined') {
  5. window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
  6. window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
  7. }
  8. var util = require('../../util');
  9. class CanvasRenderer {
  10. constructor(body, canvas) {
  11. this.body = body;
  12. this.canvas = canvas;
  13. this.redrawRequested = false;
  14. this.renderTimer = false;
  15. this.requiresTimeout = true;
  16. this.renderingActive = false;
  17. this.renderRequests = 0;
  18. this.pixelRatio = undefined;
  19. this.canvasTopLeft = {x: 0, y: 0};
  20. this.canvasBottomRight = {x: 0, y: 0};
  21. this.dragging = false;
  22. this.body.emitter.on("dragStart", () => {this.dragging = true;});
  23. this.body.emitter.on("dragEnd", () => this.dragging = false);
  24. this.body.emitter.on("_redraw", () => {if (this.renderingActive === false) {this._redraw();}});
  25. this.body.emitter.on("_requestRedraw", this._requestRedraw.bind(this));
  26. this.body.emitter.on("_startRendering", () => {this.renderRequests += 1; this.renderingActive = true; this.startRendering();});
  27. this.body.emitter.on("_stopRendering", () => {this.renderRequests -= 1; this.renderingActive = this.renderRequests > 0;});
  28. this.options = {};
  29. this.defaultOptions = {
  30. hideEdgesOnDrag: false,
  31. hideNodesOnDrag: false
  32. }
  33. util.extend(this.options,this.defaultOptions);
  34. this._determineBrowserMethod();
  35. }
  36. setOptions(options) {
  37. if (options !== undefined) {
  38. util.deepExtend(this.options, options);
  39. }
  40. }
  41. startRendering() {
  42. if (this.renderingActive === true) {
  43. if (!this.renderTimer) {
  44. if (this.requiresTimeout == true) {
  45. this.renderTimer = window.setTimeout(this.renderStep.bind(this), this.simulationInterval); // wait this.renderTimeStep milliseconds and perform the animation step function
  46. }
  47. else {
  48. this.renderTimer = window.requestAnimationFrame(this.renderStep.bind(this)); // wait this.renderTimeStep milliseconds and perform the animation step function
  49. }
  50. }
  51. }
  52. else {
  53. }
  54. }
  55. renderStep() {
  56. // reset the renderTimer so a new scheduled animation step can be set
  57. this.renderTimer = undefined;
  58. if (this.requiresTimeout == true) {
  59. // this schedules a new simulation step
  60. this.startRendering();
  61. }
  62. this._redraw();
  63. if (this.requiresTimeout == false) {
  64. // this schedules a new simulation step
  65. this.startRendering();
  66. }
  67. }
  68. /**
  69. * Redraw the network with the current data
  70. * chart will be resized too.
  71. */
  72. redraw() {
  73. this.setSize(this.constants.width, this.constants.height);
  74. this._redraw();
  75. }
  76. /**
  77. * Redraw the network with the current data
  78. * @param hidden | used to get the first estimate of the node sizes. only the nodes are drawn after which they are quickly drawn over.
  79. * @private
  80. */
  81. _requestRedraw() {
  82. if (this.redrawRequested !== true && this.renderingActive === false) {
  83. this.redrawRequested = true;
  84. if (this.requiresTimeout === true) {
  85. window.setTimeout(this._redraw.bind(this, false),0);
  86. }
  87. else {
  88. window.requestAnimationFrame(this._redraw.bind(this, false));
  89. }
  90. }
  91. }
  92. _redraw(hidden = false) {
  93. this.body.emitter.emit("_beforeRender");
  94. this.redrawRequested = false;
  95. var ctx = this.canvas.frame.canvas.getContext('2d');
  96. if (this.pixelRation === undefined) {
  97. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  98. ctx.mozBackingStorePixelRatio ||
  99. ctx.msBackingStorePixelRatio ||
  100. ctx.oBackingStorePixelRatio ||
  101. ctx.backingStorePixelRatio || 1);
  102. }
  103. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  104. // clear the canvas
  105. var w = this.canvas.frame.canvas.clientWidth;
  106. var h = this.canvas.frame.canvas.clientHeight;
  107. ctx.clearRect(0, 0, w, h);
  108. this.body.emitter.emit("beforeDrawing", ctx);
  109. // set scaling and translation
  110. ctx.save();
  111. ctx.translate(this.body.view.translation.x, this.body.view.translation.y);
  112. ctx.scale(this.body.view.scale, this.body.view.scale);
  113. this.canvasTopLeft = this.canvas.DOMtoCanvas({x:0,y:0});
  114. this.canvasBottomRight = this.canvas.DOMtoCanvas({x:this.canvas.frame.canvas.clientWidth,y:this.canvas.frame.canvas.clientHeight});
  115. if (hidden === false) {
  116. if (this.dragging === false || (this.dragging === true && this.options.hideEdgesOnDrag === false)) {
  117. this._drawEdges(ctx);
  118. }
  119. }
  120. if (this.dragging === false || (this.dragging === true && this.options.hideNodesOnDrag === false)) {
  121. this._drawNodes(ctx, this.body.nodes, hidden);
  122. }
  123. if (this.controlNodesActive === true) {
  124. this._drawControlNodes(ctx);
  125. }
  126. //this._drawNodes(ctx,this.body.supportNodes,true);
  127. //this.physics.nodesSolver._debug(ctx,"#F00F0F");
  128. // restore original scaling and translation
  129. ctx.restore();
  130. if (hidden === true) {
  131. ctx.clearRect(0, 0, w, h);
  132. }
  133. this.body.emitter.emit("afterDrawing", ctx);
  134. }
  135. /**
  136. * Redraw all nodes
  137. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  138. * @param {CanvasRenderingContext2D} ctx
  139. * @param {Boolean} [alwaysShow]
  140. * @private
  141. */
  142. _drawNodes(ctx,nodes,alwaysShow = false) {
  143. // first draw the unselected nodes
  144. var selected = [];
  145. for (var id in nodes) {
  146. if (nodes.hasOwnProperty(id)) {
  147. nodes[id].setScaleAndPos(this.body.view.scale,this.canvasTopLeft,this.canvasBottomRight);
  148. if (nodes[id].isSelected()) {
  149. selected.push(id);
  150. }
  151. else {
  152. if (alwaysShow === true) {
  153. nodes[id].draw(ctx);
  154. }
  155. else if (nodes[id].inArea() === true) {
  156. nodes[id].draw(ctx);
  157. }
  158. }
  159. }
  160. }
  161. // draw the selected nodes on top
  162. for (var s = 0, sMax = selected.length; s < sMax; s++) {
  163. if (nodes[selected[s]].inArea() || alwaysShow) {
  164. nodes[selected[s]].draw(ctx);
  165. }
  166. }
  167. }
  168. /**
  169. * Redraw all edges
  170. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  171. * @param {CanvasRenderingContext2D} ctx
  172. * @private
  173. */
  174. _drawEdges(ctx) {
  175. var edges = this.body.edges;
  176. for (var id in edges) {
  177. if (edges.hasOwnProperty(id)) {
  178. var edge = edges[id];
  179. edge.setScale(this.body.view.scale);
  180. if (edge.connected === true) {
  181. edges[id].draw(ctx);
  182. }
  183. }
  184. }
  185. }
  186. /**
  187. * Redraw all edges
  188. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  189. * @param {CanvasRenderingContext2D} ctx
  190. * @private
  191. */
  192. _drawControlNodes(ctx) {
  193. var edges = this.body.edges;
  194. for (var id in edges) {
  195. if (edges.hasOwnProperty(id)) {
  196. edges[id]._drawControlNodes(ctx);
  197. }
  198. }
  199. }
  200. /**
  201. * Determine if the browser requires a setTimeout or a requestAnimationFrame. This was required because
  202. * some implementations (safari and IE9) did not support requestAnimationFrame
  203. * @private
  204. */
  205. _determineBrowserMethod() {
  206. if (typeof window !== 'undefined') {
  207. var browserType = navigator.userAgent.toLowerCase();
  208. this.requiresTimeout = false;
  209. if (browserType.indexOf('msie 9.0') != -1) { // IE 9
  210. this.requiresTimeout = true;
  211. }
  212. else if (browserType.indexOf('safari') != -1) { // safari
  213. if (browserType.indexOf('chrome') <= -1) {
  214. this.requiresTimeout = true;
  215. }
  216. }
  217. }
  218. else {
  219. this.requiresTimeout = true;
  220. }
  221. }
  222. }
  223. export {CanvasRenderer};