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.

300 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. autoResize: true,
  20. height: '100%',
  21. width: '100%'
  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. let fields = ['width','height','autoResize'];
  46. util.selectiveDeepExtend(fields,this.options, options);
  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. // enable to get better response, todo: test on mobile.
  136. //this.hammer.get('pan').set({threshold:2});
  137. hammerUtil.onTouch(this.hammer, (event) => {this.body.eventListeners.onTouch(event)});
  138. this.hammer.on('tap', (event) => {this.body.eventListeners.onTap(event)});
  139. this.hammer.on('doubletap', (event) => {this.body.eventListeners.onDoubleTap(event)});
  140. this.hammer.on('press', (event) => {this.body.eventListeners.onHold(event)});
  141. this.hammer.on('panstart', (event) => {this.body.eventListeners.onDragStart(event)});
  142. this.hammer.on('panmove', (event) => {this.body.eventListeners.onDrag(event)});
  143. this.hammer.on('panend', (event) => {this.body.eventListeners.onDragEnd(event)});
  144. this.hammer.on('pinch', (event) => {this.body.eventListeners.onPinch(event)});
  145. // TODO: neatly cleanup these handlers when re-creating the Canvas, IF these are done with hammer, event.stopPropagation will not work?
  146. this.frame.canvas.addEventListener('mousewheel', (event) => {this.body.eventListeners.onMouseWheel(event)});
  147. this.frame.canvas.addEventListener('DOMMouseScroll', (event) => {this.body.eventListeners.onMouseWheel(event)});
  148. this.frame.canvas.addEventListener('mousemove', (event) => {this.body.eventListeners.onMouseMove(event)});
  149. this.frame.canvas.addEventListener('contextmenu', (event) => {this.body.eventListeners.onContext(event)});
  150. this.hammerFrame = new Hammer(this.frame);
  151. hammerUtil.onRelease(this.hammerFrame, (event) => {this.body.eventListeners.onRelease(event)});
  152. }
  153. /**
  154. * Set a new size for the network
  155. * @param {string} width Width in pixels or percentage (for example '800px'
  156. * or '50%')
  157. * @param {string} height Height in pixels or percentage (for example '400px'
  158. * or '30%')
  159. */
  160. setSize(width = this.options.width, height = this.options.height) {
  161. width = this._prepareValue(width);
  162. height= this._prepareValue(height);
  163. let emitEvent = false;
  164. let oldWidth = this.frame.canvas.width;
  165. let oldHeight = this.frame.canvas.height;
  166. if (width != this.options.width || height != this.options.height || this.frame.style.width != width || this.frame.style.height != height) {
  167. this.frame.style.width = width;
  168. this.frame.style.height = height;
  169. this.frame.canvas.style.width = '100%';
  170. this.frame.canvas.style.height = '100%';
  171. this.frame.canvas.width = this.frame.canvas.clientWidth * this.pixelRatio;
  172. this.frame.canvas.height = this.frame.canvas.clientHeight * this.pixelRatio;
  173. this.options.width = width;
  174. this.options.height = height;
  175. emitEvent = true;
  176. }
  177. else {
  178. // this would adapt the width of the canvas to the width from 100% if and only if
  179. // there is a change.
  180. if (this.frame.canvas.width != this.frame.canvas.clientWidth * this.pixelRatio) {
  181. this.frame.canvas.width = this.frame.canvas.clientWidth * this.pixelRatio;
  182. emitEvent = true;
  183. }
  184. if (this.frame.canvas.height != this.frame.canvas.clientHeight * this.pixelRatio) {
  185. this.frame.canvas.height = this.frame.canvas.clientHeight * this.pixelRatio;
  186. emitEvent = true;
  187. }
  188. }
  189. if (emitEvent === true) {
  190. 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});
  191. }
  192. };
  193. /**
  194. * Convert the X coordinate in DOM-space (coordinate point in browser relative to the container div) to
  195. * the X coordinate in canvas-space (the simulation sandbox, which the camera looks upon)
  196. * @param {number} x
  197. * @returns {number}
  198. * @private
  199. */
  200. _XconvertDOMtoCanvas(x) {
  201. return (x - this.body.view.translation.x) / this.body.view.scale;
  202. }
  203. /**
  204. * Convert the X coordinate in canvas-space (the simulation sandbox, which the camera looks upon) to
  205. * the X coordinate in DOM-space (coordinate point in browser relative to the container div)
  206. * @param {number} x
  207. * @returns {number}
  208. * @private
  209. */
  210. _XconvertCanvasToDOM(x) {
  211. return x * this.body.view.scale + this.body.view.translation.x;
  212. }
  213. /**
  214. * Convert the Y coordinate in DOM-space (coordinate point in browser relative to the container div) to
  215. * the Y coordinate in canvas-space (the simulation sandbox, which the camera looks upon)
  216. * @param {number} y
  217. * @returns {number}
  218. * @private
  219. */
  220. _YconvertDOMtoCanvas(y) {
  221. return (y - this.body.view.translation.y) / this.body.view.scale;
  222. }
  223. /**
  224. * Convert the Y coordinate in canvas-space (the simulation sandbox, which the camera looks upon) to
  225. * the Y coordinate in DOM-space (coordinate point in browser relative to the container div)
  226. * @param {number} y
  227. * @returns {number}
  228. * @private
  229. */
  230. _YconvertCanvasToDOM(y) {
  231. return y * this.body.view.scale + this.body.view.translation.y;
  232. }
  233. /**
  234. *
  235. * @param {object} pos = {x: number, y: number}
  236. * @returns {{x: number, y: number}}
  237. * @constructor
  238. */
  239. canvasToDOM (pos) {
  240. return {x: this._XconvertCanvasToDOM(pos.x), y: this._YconvertCanvasToDOM(pos.y)};
  241. }
  242. /**
  243. *
  244. * @param {object} pos = {x: number, y: number}
  245. * @returns {{x: number, y: number}}
  246. * @constructor
  247. */
  248. DOMtoCanvas (pos) {
  249. return {x: this._XconvertDOMtoCanvas(pos.x), y: this._YconvertDOMtoCanvas(pos.y)};
  250. }
  251. }
  252. export default Canvas;