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.

123 lines
4.4 KiB

  1. 'use strict';
  2. import CircleImageBase from '../util/CircleImageBase'
  3. /**
  4. * An image-based replacement for the default Node shape.
  5. *
  6. * @extends CircleImageBase
  7. */
  8. class Image extends CircleImageBase {
  9. /**
  10. * @param {Object} options
  11. * @param {Object} body
  12. * @param {Label} labelModule
  13. * @param {Image} imageObj
  14. * @param {Image} imageObjAlt
  15. */
  16. constructor (options, body, labelModule, imageObj, imageObjAlt) {
  17. super(options, body, labelModule);
  18. this.setImages(imageObj, imageObjAlt);
  19. }
  20. /**
  21. *
  22. * @param {CanvasRenderingContext2D} ctx - Unused.
  23. * @param {boolean} [selected]
  24. * @param {boolean} [hover]
  25. */
  26. resize(ctx, selected = this.selected, hover = this.hover) {
  27. var imageAbsent = (this.imageObj.src === undefined) ||
  28. (this.imageObj.width === undefined) ||
  29. (this.imageObj.height === undefined);
  30. if (imageAbsent) {
  31. var side = this.options.size * 2;
  32. this.width = side;
  33. this.height = side;
  34. return;
  35. }
  36. if (this.needsRefresh(selected, hover)) {
  37. this._resizeImage();
  38. }
  39. }
  40. /**
  41. *
  42. * @param {CanvasRenderingContext2D} ctx
  43. * @param {number} x width
  44. * @param {number} y height
  45. * @param {boolean} selected
  46. * @param {boolean} hover
  47. * @param {{toArrow: boolean, toArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), toArrowType: *, middleArrow: boolean, middleArrowScale: (number|allOptions.edges.arrows.middle.scaleFactor|{number}|Array), middleArrowType: (allOptions.edges.arrows.middle.type|{string}|string|*), fromArrow: boolean, fromArrowScale: (allOptions.edges.arrows.to.scaleFactor|{number}|allOptions.edges.arrows.middle.scaleFactor|allOptions.edges.arrows.from.scaleFactor|Array|number), fromArrowType: *, arrowStrikethrough: (*|boolean|allOptions.edges.arrowStrikethrough|{boolean}), color: undefined, inheritsColor: (string|string|string|allOptions.edges.color.inherit|{string, boolean}|Array|*), opacity: *, hidden: *, length: *, shadow: *, shadowColor: *, shadowSize: *, shadowX: *, shadowY: *, dashes: (*|boolean|Array|allOptions.edges.dashes|{boolean, array}), width: *}} values
  48. */
  49. draw(ctx, x, y, selected, hover, values) {
  50. this.switchImages(selected);
  51. this.resize();
  52. this.left = x - this.width / 2;
  53. this.top = y - this.height / 2;
  54. if (this.options.shapeProperties.useBorderWithImage === true) {
  55. var neutralborderWidth = this.options.borderWidth;
  56. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  57. var borderWidth = (selected ? selectionLineWidth : neutralborderWidth) / this.body.view.scale;
  58. ctx.lineWidth = Math.min(this.width, borderWidth);
  59. ctx.beginPath();
  60. // setup the line properties.
  61. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  62. // set a fillstyle
  63. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  64. // draw a rectangle to form the border around. This rectangle is filled so the opacity of a picture (in future vis releases?) can be used to tint the image
  65. ctx.rect(this.left - 0.5 * ctx.lineWidth,
  66. this.top - 0.5 * ctx.lineWidth,
  67. this.width + ctx.lineWidth,
  68. this.height + ctx.lineWidth);
  69. ctx.fill();
  70. this.performStroke(ctx, values);
  71. ctx.closePath();
  72. }
  73. this._drawImageAtPosition(ctx, values);
  74. this._drawImageLabel(ctx, x, y, selected, hover);
  75. this.updateBoundingBox(x,y);
  76. }
  77. /**
  78. *
  79. * @param {number} x
  80. * @param {number} y
  81. */
  82. updateBoundingBox(x, y) {
  83. this.resize();
  84. this._updateBoundingBox(x, y);
  85. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  86. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  87. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  88. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  89. }
  90. }
  91. /**
  92. *
  93. * @param {CanvasRenderingContext2D} ctx
  94. * @param {number} angle
  95. * @returns {number}
  96. */
  97. distanceToBorder(ctx, angle) {
  98. return this._distanceToBorder(ctx,angle);
  99. }
  100. }
  101. export default Image;