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
  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.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("_resizeNodes", () => this._resizeNodes());
  33. this.body.emitter.on("_redraw", () => {
  34. if (this.renderingActive === false) {
  35. this._redraw();
  36. }
  37. });
  38. this.body.emitter.on("_blockRedrawRequests", () => {this.allowRedrawRequests = false;});
  39. this.body.emitter.on("_allowRedrawRequests", () => {this.allowRedrawRequests = true; });
  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.renderingActive = false;
  54. if (this.requiresTimeout === true) {
  55. clearTimeout(this.renderTimer);
  56. }
  57. else {
  58. cancelAnimationFrame(this.renderTimer);
  59. }
  60. this.body.emitter.off();
  61. });
  62. }
  63. setOptions(options) {
  64. if (options !== undefined) {
  65. let fields = ['hideEdgesOnDrag','hideNodesOnDrag'];
  66. util.selectiveDeepExtend(fields,this.options, options);
  67. }
  68. }
  69. _startRendering() {
  70. if (this.renderingActive === true) {
  71. if (this.renderTimer === undefined) {
  72. if (this.requiresTimeout === true) {
  73. this.renderTimer = window.setTimeout(this._renderStep.bind(this), this.simulationInterval); // wait this.renderTimeStep milliseconds and perform the animation step function
  74. }
  75. else {
  76. this.renderTimer = window.requestAnimationFrame(this._renderStep.bind(this)); // wait this.renderTimeStep milliseconds and perform the animation step function
  77. }
  78. }
  79. }
  80. }
  81. _renderStep() {
  82. if (this.renderingActive === true) {
  83. // reset the renderTimer so a new scheduled animation step can be set
  84. this.renderTimer = undefined;
  85. if (this.requiresTimeout === true) {
  86. // this schedules a new simulation step
  87. this._startRendering();
  88. }
  89. this._redraw();
  90. if (this.requiresTimeout === false) {
  91. // this schedules a new simulation step
  92. this._startRendering();
  93. }
  94. }
  95. }
  96. /**
  97. * Redraw the network with the current data
  98. * chart will be resized too.
  99. */
  100. redraw() {
  101. this.body.emitter.emit('setSize');
  102. this._redraw();
  103. }
  104. /**
  105. * Redraw the network with the current data
  106. * @param hidden | used to get the first estimate of the node sizes. only the nodes are drawn after which they are quickly drawn over.
  107. * @private
  108. */
  109. _requestRedraw() {
  110. if (this.redrawRequested !== true && this.renderingActive === false && this.allowRedrawRequests === true) {
  111. this.redrawRequested = true;
  112. if (this.requiresTimeout === true) {
  113. window.setTimeout(() => {this._redraw(false);}, 0);
  114. }
  115. else {
  116. window.requestAnimationFrame(() => {this._redraw(false);});
  117. }
  118. }
  119. }
  120. _redraw(hidden = false) {
  121. this.body.emitter.emit("initRedraw");
  122. this.redrawRequested = false;
  123. let ctx = this.canvas.frame.canvas.getContext('2d');
  124. // when the container div was hidden, this fixes it back up!
  125. if (this.canvas.frame.canvas.width === 0 || this.canvas.frame.canvas.height === 0) {
  126. this.canvas.setSize();
  127. }
  128. if (this.pixelRatio === undefined) {
  129. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  130. ctx.mozBackingStorePixelRatio ||
  131. ctx.msBackingStorePixelRatio ||
  132. ctx.oBackingStorePixelRatio ||
  133. ctx.backingStorePixelRatio || 1);
  134. }
  135. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  136. // clear the canvas
  137. let w = this.canvas.frame.canvas.clientWidth;
  138. let h = this.canvas.frame.canvas.clientHeight;
  139. ctx.clearRect(0, 0, w, h);
  140. // set scaling and translation
  141. ctx.save();
  142. ctx.translate(this.body.view.translation.x, this.body.view.translation.y);
  143. ctx.scale(this.body.view.scale, this.body.view.scale);
  144. ctx.beginPath();
  145. this.body.emitter.emit("beforeDrawing", ctx);
  146. ctx.closePath();
  147. console.log(arguments)
  148. if (hidden === false) {
  149. if (this.dragging === false || (this.dragging === true && this.options.hideEdgesOnDrag === false)) {
  150. this._drawEdges(ctx);
  151. }
  152. }
  153. if (this.dragging === false || (this.dragging === true && this.options.hideNodesOnDrag === false)) {
  154. this._drawNodes(ctx, hidden);
  155. }
  156. if (this.controlNodesActive === true) {
  157. this._drawControlNodes(ctx);
  158. }
  159. ctx.beginPath();
  160. //this.physics.nodesSolver._debug(ctx,"#F00F0F");
  161. this.body.emitter.emit("afterDrawing", ctx);
  162. ctx.closePath();
  163. // restore original scaling and translation
  164. ctx.restore();
  165. if (hidden === true) {
  166. ctx.clearRect(0, 0, w, h);
  167. }
  168. }
  169. /**
  170. * Redraw all nodes
  171. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  172. * @param {CanvasRenderingContext2D} ctx
  173. * @param {Boolean} [alwaysShow]
  174. * @private
  175. */
  176. _resizeNodes() {
  177. let ctx = this.canvas.frame.canvas.getContext('2d');
  178. if (this.pixelRatio === undefined) {
  179. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  180. ctx.mozBackingStorePixelRatio ||
  181. ctx.msBackingStorePixelRatio ||
  182. ctx.oBackingStorePixelRatio ||
  183. ctx.backingStorePixelRatio || 1);
  184. }
  185. ctx.setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  186. ctx.save();
  187. ctx.translate(this.body.view.translation.x, this.body.view.translation.y);
  188. ctx.scale(this.body.view.scale, this.body.view.scale);
  189. let nodes = this.body.nodes;
  190. let node;
  191. // resize all nodes
  192. for (let nodeId in nodes) {
  193. if (nodes.hasOwnProperty(nodeId)) {
  194. node = nodes[nodeId];
  195. node.resize(ctx);
  196. node.updateBoundingBox(ctx);
  197. }
  198. }
  199. // restore original scaling and translation
  200. ctx.restore();
  201. }
  202. /**
  203. * Redraw all nodes
  204. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  205. * @param {CanvasRenderingContext2D} ctx
  206. * @param {Boolean} [alwaysShow]
  207. * @private
  208. */
  209. _drawNodes(ctx, alwaysShow = false) {
  210. console.log("draw nodes")
  211. let nodes = this.body.nodes;
  212. let nodeIndices = this.body.nodeIndices;
  213. let node;
  214. let selected = [];
  215. let margin = 20;
  216. let topLeft = this.canvas.DOMtoCanvas({x:-margin,y:-margin});
  217. let bottomRight = this.canvas.DOMtoCanvas({
  218. x: this.canvas.frame.canvas.clientWidth+margin,
  219. y: this.canvas.frame.canvas.clientHeight+margin
  220. });
  221. let viewableArea = {top:topLeft.y,left:topLeft.x,bottom:bottomRight.y,right:bottomRight.x};
  222. // draw unselected nodes;
  223. for (let i = 0; i < nodeIndices.length; i++) {
  224. node = nodes[nodeIndices[i]];
  225. // set selected nodes aside
  226. if (node.isSelected()) {
  227. selected.push(nodeIndices[i]);
  228. }
  229. else {
  230. if (alwaysShow === true) {
  231. node.draw(ctx);
  232. }
  233. else if (node.isBoundingBoxOverlappingWith(viewableArea) === true) {
  234. node.draw(ctx);
  235. }
  236. else {
  237. node.updateBoundingBox(ctx);
  238. }
  239. }
  240. }
  241. // draw the selected nodes on top
  242. for (let i = 0; i < selected.length; i++) {
  243. node = nodes[selected[i]];
  244. node.draw(ctx);
  245. }
  246. }
  247. /**
  248. * Redraw all edges
  249. * The 2d context of a HTML canvas can be retrieved by canvas.getContext('2d');
  250. * @param {CanvasRenderingContext2D} ctx
  251. * @private
  252. */
  253. _drawEdges(ctx) {
  254. console.log("draw edges")
  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;