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