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.

265 lines
9.1 KiB

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