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.

348 lines
10 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. if (typeof window !== 'undefined') {
  2. window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
  3. window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
  4. }
  5. let 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.allowRedraw = 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("_resizeNodes", () => this._resizeNodes());
  33. this.body.emitter.on("_redraw", () => {
  34. if (this.renderingActive === false) {
  35. this._redraw();
  36. }
  37. });
  38. this.body.emitter.on("_blockRedraw", () => {this.allowRedraw = false;});
  39. this.body.emitter.on("_allowRedraw", () => {this.allowRedraw = true; this.redrawRequested = false;});
  40. this.body.emitter.on("_requestRedraw", this._requestRedraw.bind(this));
  41. this.body.emitter.on("_startRendering", () => {
  42. this.renderRequests += 1;
  43. this.renderingActive = true;
  44. this._startRendering();
  45. });
  46. this.body.emitter.on("_stopRendering", () => {
  47. this.renderRequests -= 1;
  48. this.renderingActive = this.renderRequests > 0;
  49. this.renderTimer = undefined;
  50. });
  51. this.body.emitter.on('destroy', () => {
  52. this.renderRequests = 0;
  53. this.allowRedraw = false;
  54. this.renderingActive = false;
  55. if (this.requiresTimeout === true) {
  56. clearTimeout(this.renderTimer);
  57. }
  58. else {
  59. cancelAnimationFrame(this.renderTimer);
  60. }
  61. this.body.emitter.off();
  62. });
  63. }
  64. setOptions(options) {
  65. if (options !== undefined) {
  66. let fields = ['hideEdgesOnDrag','hideNodesOnDrag'];
  67. util.selectiveDeepExtend(fields,this.options, options);
  68. }
  69. }
  70. _startRendering() {
  71. if (this.renderingActive === true) {
  72. if (this.renderTimer === undefined) {
  73. if (this.requiresTimeout === true) {
  74. this.renderTimer = window.setTimeout(this._renderStep.bind(this), this.simulationInterval); // wait this.renderTimeStep milliseconds and perform the animation step function
  75. }
  76. else {
  77. this.renderTimer = window.requestAnimationFrame(this._renderStep.bind(this)); // wait this.renderTimeStep milliseconds and perform the animation step function
  78. }
  79. }
  80. }
  81. }
  82. _renderStep() {
  83. if (this.renderingActive === true) {
  84. // reset the renderTimer so a new scheduled animation step can be set
  85. this.renderTimer = undefined;
  86. if (this.requiresTimeout === true) {
  87. // this schedules a new simulation step
  88. this._startRendering();
  89. }
  90. this._redraw();
  91. if (this.requiresTimeout === false) {
  92. // this schedules a new simulation step
  93. this._startRendering();
  94. }
  95. }
  96. }
  97. /**
  98. * Redraw the network with the current data
  99. * chart will be resized too.
  100. */
  101. redraw() {
  102. this.body.emitter.emit('setSize');
  103. this._redraw();
  104. }
  105. /**
  106. * Redraw the network with the current data
  107. * @param hidden | used to get the first estimate of the node sizes. only the nodes are drawn after which they are quickly drawn over.
  108. * @private
  109. */
  110. _requestRedraw() {
  111. if (this.redrawRequested !== true && this.renderingActive === false && this.allowRedraw === true) {
  112. this.redrawRequested = true;
  113. if (this.requiresTimeout === true) {
  114. window.setTimeout(() => {this._redraw(false);}, 0);
  115. }
  116. else {
  117. window.requestAnimationFrame(() => {this._redraw(false);});
  118. }
  119. }
  120. }
  121. _redraw(hidden = false) {
  122. if (this.allowRedraw === true) {
  123. this.body.emitter.emit("initRedraw");
  124. this.redrawRequested = false;
  125. let ctx = this.canvas.frame.canvas.getContext('2d');
  126. // when the container div was hidden, this fixes it back up!
  127. if (this.canvas.frame.canvas.width === 0 || this.canvas.frame.canvas.height === 0) {
  128. this.canvas.setSize();
  129. }
  130. if (this.pixelRatio === undefined) {
  131. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  132. ctx.mozBackingStorePixelRatio ||
  133. ctx.msBackingStorePixelRatio ||
  134. ctx.oBackingStorePixelRatio ||
  135. ctx.backingStorePixelRatio || 1);
  136. }
  137. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  138. // clear the canvas
  139. let w = this.canvas.frame.canvas.clientWidth;
  140. let h = this.canvas.frame.canvas.clientHeight;
  141. ctx.clearRect(0, 0, w, h);
  142. // set scaling and translation
  143. ctx.save();
  144. ctx.translate(this.body.view.translation.x, this.body.view.translation.y);
  145. ctx.scale(this.body.view.scale, this.body.view.scale);
  146. ctx.beginPath();
  147. this.body.emitter.emit("beforeDrawing", ctx);
  148. ctx.closePath();
  149. if (hidden === false) {
  150. if (this.dragging === false || (this.dragging === true && this.options.hideEdgesOnDrag === false)) {
  151. this._drawEdges(ctx);
  152. }
  153. }
  154. if (this.dragging === false || (this.dragging === true && this.options.hideNodesOnDrag === false)) {
  155. this._drawNodes(ctx, hidden);
  156. }
  157. if (this.controlNodesActive === true) {
  158. this._drawControlNodes(ctx);
  159. }
  160. ctx.beginPath();
  161. //this.physics.nodesSolver._debug(ctx,"#F00F0F");
  162. this.body.emitter.emit("afterDrawing", ctx);
  163. ctx.closePath();
  164. // restore original scaling and translation
  165. ctx.restore();
  166. if (hidden === true) {
  167. ctx.clearRect(0, 0, w, h);
  168. }
  169. }
  170. }
  171. /**
  172. * Redraw all nodes
  173. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  174. * @param {CanvasRenderingContext2D} ctx
  175. * @param {Boolean} [alwaysShow]
  176. * @private
  177. */
  178. _resizeNodes() {
  179. let ctx = this.canvas.frame.canvas.getContext('2d');
  180. if (this.pixelRatio === undefined) {
  181. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  182. ctx.mozBackingStorePixelRatio ||
  183. ctx.msBackingStorePixelRatio ||
  184. ctx.oBackingStorePixelRatio ||
  185. ctx.backingStorePixelRatio || 1);
  186. }
  187. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  188. ctx.save();
  189. ctx.translate(this.body.view.translation.x, this.body.view.translation.y);
  190. ctx.scale(this.body.view.scale, this.body.view.scale);
  191. let nodes = this.body.nodes;
  192. let node;
  193. // resize all nodes
  194. for (let nodeId in nodes) {
  195. if (nodes.hasOwnProperty(nodeId)) {
  196. node = nodes[nodeId];
  197. node.resize(ctx);
  198. node.updateBoundingBox(ctx, node.selected);
  199. }
  200. }
  201. // restore original scaling and translation
  202. ctx.restore();
  203. }
  204. /**
  205. * Redraw all nodes
  206. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  207. * @param {CanvasRenderingContext2D} ctx
  208. * @param {Boolean} [alwaysShow]
  209. * @private
  210. */
  211. _drawNodes(ctx, alwaysShow = false) {
  212. let nodes = this.body.nodes;
  213. let nodeIndices = this.body.nodeIndices;
  214. let node;
  215. let selected = [];
  216. let margin = 20;
  217. let topLeft = this.canvas.DOMtoCanvas({x:-margin,y:-margin});
  218. let bottomRight = this.canvas.DOMtoCanvas({
  219. x: this.canvas.frame.canvas.clientWidth+margin,
  220. y: this.canvas.frame.canvas.clientHeight+margin
  221. });
  222. let viewableArea = {top:topLeft.y,left:topLeft.x,bottom:bottomRight.y,right:bottomRight.x};
  223. // draw unselected nodes;
  224. for (let i = 0; i < nodeIndices.length; i++) {
  225. node = nodes[nodeIndices[i]];
  226. // set selected nodes aside
  227. if (node.isSelected()) {
  228. selected.push(nodeIndices[i]);
  229. }
  230. else {
  231. if (alwaysShow === true) {
  232. node.draw(ctx);
  233. }
  234. else if (node.isBoundingBoxOverlappingWith(viewableArea) === true) {
  235. node.draw(ctx);
  236. }
  237. else {
  238. node.updateBoundingBox(ctx, node.selected);
  239. }
  240. }
  241. }
  242. // draw the selected nodes on top
  243. for (let i = 0; i < selected.length; i++) {
  244. node = nodes[selected[i]];
  245. node.draw(ctx);
  246. }
  247. }
  248. /**
  249. * Redraw all edges
  250. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  251. * @param {CanvasRenderingContext2D} ctx
  252. * @private
  253. */
  254. _drawEdges(ctx) {
  255. let edges = this.body.edges;
  256. let edgeIndices = this.body.edgeIndices;
  257. let edge;
  258. for (let i = 0; i < edgeIndices.length; i++) {
  259. edge = edges[edgeIndices[i]];
  260. if (edge.connected === true) {
  261. edge.draw(ctx);
  262. }
  263. }
  264. }
  265. /**
  266. * Redraw all edges
  267. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  268. * @param {CanvasRenderingContext2D} ctx
  269. * @private
  270. */
  271. _drawControlNodes(ctx) {
  272. let edges = this.body.edges;
  273. let edgeIndices = this.body.edgeIndices;
  274. let edge;
  275. for (let i = 0; i < edgeIndices.length; i++) {
  276. edge = edges[edgeIndices[i]];
  277. edge._drawControlNodes(ctx);
  278. }
  279. }
  280. /**
  281. * Determine if the browser requires a setTimeout or a requestAnimationFrame. This was required because
  282. * some implementations (safari and IE9) did not support requestAnimationFrame
  283. * @private
  284. */
  285. _determineBrowserMethod() {
  286. if (typeof window !== 'undefined') {
  287. let browserType = navigator.userAgent.toLowerCase();
  288. this.requiresTimeout = false;
  289. if (browserType.indexOf('msie 9.0') != -1) { // IE 9
  290. this.requiresTimeout = true;
  291. }
  292. else if (browserType.indexOf('safari') != -1) { // safari
  293. if (browserType.indexOf('chrome') <= -1) {
  294. this.requiresTimeout = true;
  295. }
  296. }
  297. }
  298. else {
  299. this.requiresTimeout = true;
  300. }
  301. }
  302. }
  303. export default CanvasRenderer;