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
8.2 KiB

  1. let util = require('../../../../util');
  2. class Label {
  3. constructor(body,options) {
  4. this.body = body;
  5. this.pointToSelf = false;
  6. this.baseSize = undefined;
  7. this.fontOptions = {};
  8. this.setOptions(options);
  9. this.size = {top: 0, left: 0, width: 0, height: 0, yLine: 0}; // could be cached
  10. }
  11. setOptions(options, allowDeletion = false) {
  12. this.nodeOptions = options;
  13. // We want to keep the font options seperated from the node options.
  14. // The node options have to mirror the globals when they are not overruled.
  15. this.fontOptions = util.deepExtend({},options.font, true);
  16. if (options.label !== undefined) {
  17. this.labelDirty = true;
  18. }
  19. if (options.font !== undefined) {
  20. Label.parseOptions(this.fontOptions, options, allowDeletion);
  21. if (typeof options.font === 'string') {
  22. this.baseSize = this.fontOptions.size;
  23. }
  24. else if (typeof options.font === 'object') {
  25. if (options.font.size !== undefined) {
  26. this.baseSize = options.font.size;
  27. }
  28. }
  29. }
  30. }
  31. static parseOptions(parentOptions, newOptions, allowDeletion = false) {
  32. if (typeof newOptions.font === 'string') {
  33. let newOptionsArray = newOptions.font.split(" ");
  34. parentOptions.size = newOptionsArray[0].replace("px",'');
  35. parentOptions.face = newOptionsArray[1];
  36. parentOptions.color = newOptionsArray[2];
  37. }
  38. else if (typeof newOptions.font === 'object') {
  39. util.fillIfDefined(parentOptions, newOptions.font, allowDeletion);
  40. }
  41. parentOptions.size = Number(parentOptions.size);
  42. }
  43. /**
  44. * Main function. This is called from anything that wants to draw a label.
  45. * @param ctx
  46. * @param x
  47. * @param y
  48. * @param selected
  49. * @param baseline
  50. */
  51. draw(ctx, x, y, selected, baseline = 'middle') {
  52. // if no label, return
  53. if (this.nodeOptions.label === undefined)
  54. return;
  55. // check if we have to render the label
  56. let viewFontSize = this.fontOptions.size * this.body.view.scale;
  57. if (this.nodeOptions.label && viewFontSize < this.nodeOptions.scaling.label.drawThreshold - 1)
  58. return;
  59. // update the size cache if required
  60. this.calculateLabelSize(ctx, selected, x, y, baseline);
  61. // create the fontfill background
  62. this._drawBackground(ctx);
  63. // draw text
  64. this._drawText(ctx, selected, x, y, baseline);
  65. }
  66. /**
  67. * Draws the label background
  68. * @param {CanvasRenderingContext2D} ctx
  69. * @private
  70. */
  71. _drawBackground(ctx) {
  72. if (this.fontOptions.background !== undefined && this.fontOptions.background !== "none") {
  73. ctx.fillStyle = this.fontOptions.background;
  74. let lineMargin = 2;
  75. switch (this.fontOptions.align) {
  76. case 'middle':
  77. ctx.fillRect(-this.size.width * 0.5, -this.size.height * 0.5, this.size.width, this.size.height);
  78. break;
  79. case 'top':
  80. ctx.fillRect(-this.size.width * 0.5, -(this.size.height + lineMargin), this.size.width, this.size.height);
  81. break;
  82. case 'bottom':
  83. ctx.fillRect(-this.size.width * 0.5, lineMargin, this.size.width, this.size.height);
  84. break;
  85. default:
  86. ctx.fillRect(this.size.left, this.size.top - 0.5*lineMargin, this.size.width, this.size.height);
  87. break;
  88. }
  89. }
  90. }
  91. /**
  92. *
  93. * @param ctx
  94. * @param x
  95. * @param baseline
  96. * @private
  97. */
  98. _drawText(ctx, selected, x, y, baseline = 'middle') {
  99. let fontSize = this.fontOptions.size;
  100. let viewFontSize = fontSize * this.body.view.scale;
  101. // this ensures that there will not be HUGE letters on screen by setting an upper limit on the visible text size (regardless of zoomLevel)
  102. if (viewFontSize >= this.nodeOptions.scaling.label.maxVisible) {
  103. fontSize = Number(this.nodeOptions.scaling.label.maxVisible) / this.body.view.scale;
  104. }
  105. let yLine = this.size.yLine;
  106. let [fontColor, strokeColor] = this._getColor(viewFontSize);
  107. [x, yLine] = this._setAlignment(ctx, x, yLine, baseline);
  108. // configure context for drawing the text
  109. ctx.font = (selected && this.nodeOptions.labelHighlightBold ? 'bold ' : '') + fontSize + "px " + this.fontOptions.face;
  110. ctx.fillStyle = fontColor;
  111. ctx.textAlign = 'center';
  112. // set the strokeWidth
  113. if (this.fontOptions.strokeWidth > 0) {
  114. ctx.lineWidth = this.fontOptions.strokeWidth;
  115. ctx.strokeStyle = strokeColor;
  116. ctx.lineJoin = 'round';
  117. }
  118. // draw the text
  119. for (let i = 0; i < this.lineCount; i++) {
  120. if (this.fontOptions.strokeWidth > 0) {
  121. ctx.strokeText(this.lines[i], x, yLine);
  122. }
  123. ctx.fillText(this.lines[i], x, yLine);
  124. yLine += fontSize;
  125. }
  126. }
  127. _setAlignment(ctx, x, yLine, baseline) {
  128. // check for label alignment (for edges)
  129. // TODO: make alignment for nodes
  130. if (this.fontOptions.align !== 'horizontal' && this.pointToSelf === false) {
  131. x = 0;
  132. yLine = 0;
  133. let lineMargin = 2;
  134. if (this.fontOptions.align === 'top') {
  135. ctx.textBaseline = 'alphabetic';
  136. yLine -= 2 * lineMargin; // distance from edge, required because we use alphabetic. Alphabetic has less difference between browsers
  137. }
  138. else if (this.fontOptions.align === 'bottom') {
  139. ctx.textBaseline = 'hanging';
  140. yLine += 2 * lineMargin;// distance from edge, required because we use hanging. Hanging has less difference between browsers
  141. }
  142. else {
  143. ctx.textBaseline = 'middle';
  144. }
  145. }
  146. else {
  147. ctx.textBaseline = baseline;
  148. }
  149. return [x,yLine];
  150. }
  151. /**
  152. * fade in when relative scale is between threshold and threshold - 1.
  153. * If the relative scale would be smaller than threshold -1 the draw function would have returned before coming here.
  154. *
  155. * @param viewFontSize
  156. * @returns {*[]}
  157. * @private
  158. */
  159. _getColor(viewFontSize) {
  160. let fontColor = this.fontOptions.color || '#000000';
  161. let strokeColor = this.fontOptions.strokeColor || '#ffffff';
  162. if (viewFontSize <= this.nodeOptions.scaling.label.drawThreshold) {
  163. let opacity = Math.max(0, Math.min(1, 1 - (this.nodeOptions.scaling.label.drawThreshold - viewFontSize)));
  164. fontColor = util.overrideOpacity(fontColor, opacity);
  165. strokeColor = util.overrideOpacity(strokeColor, opacity);
  166. }
  167. return [fontColor, strokeColor];
  168. }
  169. /**
  170. *
  171. * @param ctx
  172. * @param selected
  173. * @returns {{width: number, height: number}}
  174. */
  175. getTextSize(ctx, selected = false) {
  176. let size = {
  177. width: this._processLabel(ctx,selected),
  178. height: this.fontOptions.size * this.lineCount,
  179. lineCount: this.lineCount
  180. };
  181. return size;
  182. }
  183. /**
  184. *
  185. * @param ctx
  186. * @param selected
  187. * @param x
  188. * @param y
  189. * @param baseline
  190. */
  191. calculateLabelSize(ctx, selected, x = 0, y = 0, baseline = 'middle') {
  192. if (this.labelDirty === true) {
  193. this.size.width = this._processLabel(ctx,selected);
  194. }
  195. this.size.height = this.fontOptions.size * this.lineCount;
  196. this.size.left = x - this.size.width * 0.5;
  197. this.size.top = y - this.size.height * 0.5;
  198. this.size.yLine = y + (1 - this.lineCount) * 0.5 * this.fontOptions.size;
  199. if (baseline === "hanging") {
  200. this.size.top += 0.5 * this.fontOptions.size;
  201. this.size.top += 4; // distance from node, required because we use hanging. Hanging has less difference between browsers
  202. this.size.yLine += 4; // distance from node
  203. }
  204. this.labelDirty = false;
  205. }
  206. /**
  207. * This calculates the width as well as explodes the label string and calculates the amount of lines.
  208. * @param ctx
  209. * @param selected
  210. * @returns {number}
  211. * @private
  212. */
  213. _processLabel(ctx,selected) {
  214. let width = 0;
  215. let lines = [''];
  216. let lineCount = 0;
  217. if (this.nodeOptions.label !== undefined) {
  218. lines = String(this.nodeOptions.label).split('\n');
  219. lineCount = lines.length;
  220. ctx.font = (selected && this.nodeOptions.labelHighlightBold ? 'bold ' : '') + this.fontOptions.size + "px " + this.fontOptions.face;
  221. width = ctx.measureText(lines[0]).width;
  222. for (let i = 1; i < lineCount; i++) {
  223. let lineWidth = ctx.measureText(lines[i]).width;
  224. width = lineWidth > width ? lineWidth : width;
  225. }
  226. }
  227. this.lines = lines;
  228. this.lineCount = lineCount;
  229. return width;
  230. }
  231. }
  232. export default Label;