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.

297 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.body.emitter.emit('setSize');
  100. this._redraw();
  101. }
  102. /**
  103. * Redraw the network with the current data
  104. * @param hidden | used to get the first estimate of the node sizes. only the nodes are drawn after which they are quickly drawn over.
  105. * @private
  106. */
  107. _requestRedraw() {
  108. if (this.redrawRequested !== true && this.renderingActive === false && this.allowRedrawRequests === true) {
  109. this.redrawRequested = true;
  110. if (this.requiresTimeout === true) {
  111. window.setTimeout(this._redraw.bind(this, false), 0);
  112. }
  113. else {
  114. window.requestAnimationFrame(this._redraw.bind(this, false));
  115. }
  116. }
  117. }
  118. _redraw(hidden = false) {
  119. this.body.emitter.emit("initRedraw");
  120. this.redrawRequested = false;
  121. var ctx = this.canvas.frame.canvas.getContext('2d');
  122. // when the container div was hidden, this fixes it back up!
  123. if (this.canvas.frame.canvas.width === 0 || this.canvas.frame.canvas.height === 0) {
  124. this.canvas.setSize();
  125. }
  126. if (this.pixelRation === undefined) {
  127. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  128. ctx.mozBackingStorePixelRatio ||
  129. ctx.msBackingStorePixelRatio ||
  130. ctx.oBackingStorePixelRatio ||
  131. ctx.backingStorePixelRatio || 1);
  132. }
  133. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  134. // clear the canvas
  135. var w = this.canvas.frame.canvas.clientWidth;
  136. var h = this.canvas.frame.canvas.clientHeight;
  137. ctx.clearRect(0, 0, w, h);
  138. this.body.emitter.emit("beforeDrawing", ctx);
  139. // set scaling and translation
  140. ctx.save();
  141. ctx.translate(this.body.view.translation.x, this.body.view.translation.y);
  142. ctx.scale(this.body.view.scale, this.body.view.scale);
  143. if (hidden === false) {
  144. if (this.dragging === false || (this.dragging === true && this.options.hideEdgesOnDrag === false)) {
  145. this._drawEdges(ctx);
  146. }
  147. }
  148. if (this.dragging === false || (this.dragging === true && this.options.hideNodesOnDrag === false)) {
  149. this._drawNodes(ctx, hidden);
  150. }
  151. if (this.controlNodesActive === true) {
  152. this._drawControlNodes(ctx);
  153. }
  154. //this.physics.nodesSolver._debug(ctx,"#F00F0F");
  155. this.body.emitter.emit("afterDrawing", ctx);
  156. // restore original scaling and translation
  157. ctx.restore();
  158. if (hidden === true) {
  159. ctx.clearRect(0, 0, w, h);
  160. }
  161. }
  162. /**
  163. * Redraw all nodes
  164. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  165. * @param {CanvasRenderingContext2D} ctx
  166. * @param {Boolean} [alwaysShow]
  167. * @private
  168. */
  169. _drawNodes(ctx, alwaysShow = false) {
  170. var nodes = this.body.nodes;
  171. var nodeIndices = this.body.nodeIndices;
  172. var node;
  173. var selected = [];
  174. // draw unselected nodes;
  175. for (let i = 0; i < nodeIndices.length; i++) {
  176. node = nodes[nodeIndices[i]];
  177. // set selected nodes aside
  178. if (node.isSelected()) {
  179. selected.push(nodeIndices[i]);
  180. }
  181. else {
  182. if (alwaysShow === true) {
  183. node.draw(ctx);
  184. }
  185. // todo: replace check
  186. //else if (node.inArea() === true) {
  187. node.draw(ctx);
  188. //}
  189. }
  190. }
  191. // draw the selected nodes on top
  192. for (let i = 0; i < selected.length; i++) {
  193. node = nodes[selected[i]];
  194. node.draw(ctx);
  195. }
  196. }
  197. /**
  198. * Redraw all edges
  199. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  200. * @param {CanvasRenderingContext2D} ctx
  201. * @private
  202. */
  203. _drawEdges(ctx) {
  204. var edges = this.body.edges;
  205. var edgeIndices = this.body.edgeIndices;
  206. var edge;
  207. for (let i = 0; i < edgeIndices.length; i++) {
  208. edge = edges[edgeIndices[i]];
  209. if (edge.connected === true) {
  210. edge.draw(ctx);
  211. }
  212. }
  213. }
  214. /**
  215. * Redraw all edges
  216. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  217. * @param {CanvasRenderingContext2D} ctx
  218. * @private
  219. */
  220. _drawControlNodes(ctx) {
  221. var edges = this.body.edges;
  222. var edgeIndices = this.body.edgeIndices;
  223. var edge;
  224. for (let i = 0; i < edgeIndices.length; i++) {
  225. edge = edges[edgeIndices[i]];
  226. edge._drawControlNodes(ctx);
  227. }
  228. }
  229. /**
  230. * Determine if the browser requires a setTimeout or a requestAnimationFrame. This was required because
  231. * some implementations (safari and IE9) did not support requestAnimationFrame
  232. * @private
  233. */
  234. _determineBrowserMethod() {
  235. if (typeof window !== 'undefined') {
  236. var browserType = navigator.userAgent.toLowerCase();
  237. this.requiresTimeout = false;
  238. if (browserType.indexOf('msie 9.0') != -1) { // IE 9
  239. this.requiresTimeout = true;
  240. }
  241. else if (browserType.indexOf('safari') != -1) { // safari
  242. if (browserType.indexOf('chrome') <= -1) {
  243. this.requiresTimeout = true;
  244. }
  245. }
  246. }
  247. else {
  248. this.requiresTimeout = true;
  249. }
  250. }
  251. }
  252. export default CanvasRenderer;