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.

298 lines
10 KiB

9 years ago
9 years ago
9 years ago
  1. let Hammer = require('../../module/hammer');
  2. let hammerUtil = require('../../hammerUtil');
  3. let util = require('../../util');
  4. /**
  5. * Create the main frame for the Network.
  6. * This function is executed once when a Network object is created. The frame
  7. * contains a canvas, and this canvas contains all objects like the axis and
  8. * nodes.
  9. * @private
  10. */
  11. class Canvas {
  12. constructor(body) {
  13. this.body = body;
  14. this.pixelRatio = 1;
  15. this.resizeTimer = undefined;
  16. this.resizeFunction = this._onResize.bind(this);
  17. this.options = {};
  18. this.defaultOptions = {
  19. width:'100%',
  20. height:'100%',
  21. autoResize: true
  22. }
  23. util.extend(this.options, this.defaultOptions);
  24. this.bindEventListeners();
  25. }
  26. bindEventListeners() {
  27. // bind the events
  28. this.body.emitter.once("resize", (obj) => {
  29. if (obj.width !== 0) {
  30. this.body.view.translation.x = obj.width * 0.5;
  31. }
  32. if (obj.height !== 0) {
  33. this.body.view.translation.y = obj.height * 0.5;
  34. }
  35. });
  36. this.body.emitter.on("setSize", this.setSize.bind(this));
  37. this.body.emitter.on("destroy", () => {
  38. this.hammerFrame.destroy();
  39. this.hammer.destroy();
  40. this._cleanUp();
  41. });
  42. }
  43. setOptions(options) {
  44. if (options !== undefined) {
  45. if (options.width !== undefined) {this.options.width = this._prepareValue(options.width );}
  46. if (options.height!== undefined) {this.options.height= this._prepareValue(options.height);}
  47. }
  48. if (this.options.autoResize === true) {
  49. // automatically adapt to a changing size of the browser.
  50. this._cleanUp();
  51. this.resizeTimer = setInterval(() => {this.setSize(); this.body.emitter.emit("_requestRedraw");}, 1000);
  52. this.resizeFunction = this._onResize.bind(this);
  53. util.addEventListener(window,'resize',this.resizeFunction);
  54. }
  55. }
  56. _cleanUp() {
  57. // automatically adapt to a changing size of the browser.
  58. if (this.resizeTimer !== undefined) {
  59. clearInterval(this.resizeTimer);
  60. }
  61. util.removeEventListener(window,'resize',this.resizeFunction);
  62. this.resizeFunction = undefined;
  63. }
  64. _onResize() {
  65. this.setSize();
  66. this.body.emitter.emit("_redraw");
  67. }
  68. _prepareValue(value) {
  69. if (typeof value === 'number') {
  70. return value + 'px';
  71. }
  72. else if (typeof value === 'string') {
  73. if (value.indexOf('%') !== -1 || value.indexOf('px') !== -1) {
  74. return value;
  75. }
  76. else if (value.indexOf('%') === -1) {
  77. return value + 'px';
  78. }
  79. }
  80. throw new Error('Could not use the value supplie for width or height:' + value);
  81. }
  82. /**
  83. * Create the HTML
  84. */
  85. _create() {
  86. // remove all elements from the container element.
  87. while (this.body.container.hasChildNodes()) {
  88. this.body.container.removeChild(this.body.container.firstChild);
  89. }
  90. this.frame = document.createElement('div');
  91. this.frame.className = 'vis-network';
  92. this.frame.style.position = 'relative';
  93. this.frame.style.overflow = 'hidden';
  94. this.frame.tabIndex = 900; // tab index is required for keycharm to bind keystrokes to the div instead of the window
  95. //////////////////////////////////////////////////////////////////
  96. this.frame.canvas = document.createElement("canvas");
  97. this.frame.canvas.style.position = 'relative';
  98. this.frame.appendChild(this.frame.canvas);
  99. if (!this.frame.canvas.getContext) {
  100. let noCanvas = document.createElement( 'DIV' );
  101. noCanvas.style.color = 'red';
  102. noCanvas.style.fontWeight = 'bold' ;
  103. noCanvas.style.padding = '10px';
  104. noCanvas.innerHTML = 'Error: your browser does not support HTML canvas';
  105. this.frame.canvas.appendChild(noCanvas);
  106. }
  107. else {
  108. let ctx = this.frame.canvas.getContext("2d");
  109. this.pixelRatio = (window.devicePixelRatio || 1) / (ctx.webkitBackingStorePixelRatio ||
  110. ctx.mozBackingStorePixelRatio ||
  111. ctx.msBackingStorePixelRatio ||
  112. ctx.oBackingStorePixelRatio ||
  113. ctx.backingStorePixelRatio || 1);
  114. this.frame.canvas.getContext("2d").setTransform(this.pixelRatio, 0, 0, this.pixelRatio, 0, 0);
  115. }
  116. // add the frame to the container element
  117. this.body.container.appendChild(this.frame);
  118. this.body.view.scale = 1;
  119. this.body.view.translation = {x: 0.5 * this.frame.canvas.clientWidth,y: 0.5 * this.frame.canvas.clientHeight};
  120. this._bindHammer();
  121. }
  122. /**
  123. * This function binds hammer, it can be repeated over and over due to the uniqueness check.
  124. * @private
  125. */
  126. _bindHammer() {
  127. if (this.hammer !== undefined) {
  128. this.hammer.destroy();
  129. }
  130. this.drag = {};
  131. this.pinch = {};
  132. // init hammer
  133. this.hammer = new Hammer(this.frame.canvas);
  134. this.hammer.get('pinch').set({enable: true});
  135. hammerUtil.onTouch(this.hammer, (event) => {this.body.eventListeners.onTouch(event)});
  136. this.hammer.on('tap', (event) => {this.body.eventListeners.onTap(event)});
  137. this.hammer.on('doubletap', (event) => {this.body.eventListeners.onDoubleTap(event)});
  138. this.hammer.on('press', (event) => {this.body.eventListeners.onHold(event)});
  139. this.hammer.on('panstart', (event) => {this.body.eventListeners.onDragStart(event)});
  140. this.hammer.on('panmove', (event) => {this.body.eventListeners.onDrag(event)});
  141. this.hammer.on('panend', (event) => {this.body.eventListeners.onDragEnd(event)});
  142. this.hammer.on('pinch', (event) => {this.body.eventListeners.onPinch(event)});
  143. // TODO: neatly cleanup these handlers when re-creating the Canvas, IF these are done with hammer, event.stopPropagation will not work?
  144. this.frame.canvas.addEventListener('mousewheel', (event) => {this.body.eventListeners.onMouseWheel(event)});
  145. this.frame.canvas.addEventListener('DOMMouseScroll', (event) => {this.body.eventListeners.onMouseWheel(event)});
  146. this.frame.canvas.addEventListener('mousemove', (event) => {this.body.eventListeners.onMouseMove(event)});
  147. this.frame.canvas.addEventListener('contextmenu', (event) => {this.body.eventListeners.onContext(event)});
  148. this.hammerFrame = new Hammer(this.frame);
  149. hammerUtil.onRelease(this.hammerFrame, (event) => {this.body.eventListeners.onRelease(event)});
  150. }
  151. /**
  152. * Set a new size for the network
  153. * @param {string} width Width in pixels or percentage (for example '800px'
  154. * or '50%')
  155. * @param {string} height Height in pixels or percentage (for example '400px'
  156. * or '30%')
  157. */
  158. setSize(width = this.options.width, height = this.options.height) {
  159. width = this._prepareValue(width);
  160. height= this._prepareValue(height);
  161. let emitEvent = false;
  162. let oldWidth = this.frame.canvas.width;
  163. let oldHeight = this.frame.canvas.height;
  164. if (width != this.options.width || height != this.options.height || this.frame.style.width != width || this.frame.style.height != height) {
  165. this.frame.style.width = width;
  166. this.frame.style.height = height;
  167. this.frame.canvas.style.width = '100%';
  168. this.frame.canvas.style.height = '100%';
  169. this.frame.canvas.width = this.frame.canvas.clientWidth * this.pixelRatio;
  170. this.frame.canvas.height = this.frame.canvas.clientHeight * this.pixelRatio;
  171. this.options.width = width;
  172. this.options.height = height;
  173. emitEvent = true;
  174. }
  175. else {
  176. // this would adapt the width of the canvas to the width from 100% if and only if
  177. // there is a change.
  178. if (this.frame.canvas.width != this.frame.canvas.clientWidth * this.pixelRatio) {
  179. this.frame.canvas.width = this.frame.canvas.clientWidth * this.pixelRatio;
  180. emitEvent = true;
  181. }
  182. if (this.frame.canvas.height != this.frame.canvas.clientHeight * this.pixelRatio) {
  183. this.frame.canvas.height = this.frame.canvas.clientHeight * this.pixelRatio;
  184. emitEvent = true;
  185. }
  186. }
  187. if (emitEvent === true) {
  188. this.body.emitter.emit('resize', {width:this.frame.canvas.width / this.pixelRatio, height:this.frame.canvas.height / this.pixelRatio, oldWidth: oldWidth / this.pixelRatio, oldHeight: oldHeight / this.pixelRatio});
  189. }
  190. };
  191. /**
  192. * Convert the X coordinate in DOM-space (coordinate point in browser relative to the container div) to
  193. * the X coordinate in canvas-space (the simulation sandbox, which the camera looks upon)
  194. * @param {number} x
  195. * @returns {number}
  196. * @private
  197. */
  198. _XconvertDOMtoCanvas(x) {
  199. return (x - this.body.view.translation.x) / this.body.view.scale;
  200. }
  201. /**
  202. * Convert the X coordinate in canvas-space (the simulation sandbox, which the camera looks upon) to
  203. * the X coordinate in DOM-space (coordinate point in browser relative to the container div)
  204. * @param {number} x
  205. * @returns {number}
  206. * @private
  207. */
  208. _XconvertCanvasToDOM(x) {
  209. return x * this.body.view.scale + this.body.view.translation.x;
  210. }
  211. /**
  212. * Convert the Y coordinate in DOM-space (coordinate point in browser relative to the container div) to
  213. * the Y coordinate in canvas-space (the simulation sandbox, which the camera looks upon)
  214. * @param {number} y
  215. * @returns {number}
  216. * @private
  217. */
  218. _YconvertDOMtoCanvas(y) {
  219. return (y - this.body.view.translation.y) / this.body.view.scale;
  220. }
  221. /**
  222. * Convert the Y coordinate in canvas-space (the simulation sandbox, which the camera looks upon) to
  223. * the Y coordinate in DOM-space (coordinate point in browser relative to the container div)
  224. * @param {number} y
  225. * @returns {number}
  226. * @private
  227. */
  228. _YconvertCanvasToDOM(y) {
  229. return y * this.body.view.scale + this.body.view.translation.y;
  230. }
  231. /**
  232. *
  233. * @param {object} pos = {x: number, y: number}
  234. * @returns {{x: number, y: number}}
  235. * @constructor
  236. */
  237. canvasToDOM (pos) {
  238. return {x: this._XconvertCanvasToDOM(pos.x), y: this._YconvertCanvasToDOM(pos.y)};
  239. }
  240. /**
  241. *
  242. * @param {object} pos = {x: number, y: number}
  243. * @returns {{x: number, y: number}}
  244. * @constructor
  245. */
  246. DOMtoCanvas (pos) {
  247. return {x: this._XconvertDOMtoCanvas(pos.x), y: this._YconvertDOMtoCanvas(pos.y)};
  248. }
  249. }
  250. export default Canvas;