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.

296 lines
8.1 KiB

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