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.

272 lines
9.2 KiB

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