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.

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