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.

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