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.

229 lines
7.7 KiB

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