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.

125 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. * @class Image
  7. * @extends CircleImageBase
  8. */
  9. class Image extends CircleImageBase {
  10. /**
  11. * @param {Object} options
  12. * @param {Object} body
  13. * @param {Label} labelModule
  14. * @param {Image} imageObj
  15. * @param {Image} imageObjAlt
  16. * @constructor Image
  17. */
  18. constructor (options, body, labelModule, imageObj, imageObjAlt) {
  19. super(options, body, labelModule);
  20. this.setImages(imageObj, imageObjAlt);
  21. }
  22. /**
  23. *
  24. * @param {CanvasRenderingContext2D} ctx - Unused.
  25. * @param {Boolean} [selected]
  26. * @param {Boolean} [hover]
  27. */
  28. resize(ctx, selected = this.selected, hover = this.hover) {
  29. var imageAbsent = (this.imageObj.src === undefined) ||
  30. (this.imageObj.width === undefined) ||
  31. (this.imageObj.height === undefined);
  32. if (imageAbsent) {
  33. var side = this.options.size * 2;
  34. this.width = side;
  35. this.height = side;
  36. return;
  37. }
  38. if (this.needsRefresh(selected, hover)) {
  39. this._resizeImage();
  40. }
  41. }
  42. /**
  43. *
  44. * @param {CanvasRenderingContext2D} ctx
  45. * @param {number} x width
  46. * @param {number} y height
  47. * @param {boolean} selected
  48. * @param {boolean} hover
  49. * @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
  50. */
  51. draw(ctx, x, y, selected, hover, values) {
  52. this.switchImages(selected);
  53. this.resize();
  54. this.left = x - this.width / 2;
  55. this.top = y - this.height / 2;
  56. if (this.options.shapeProperties.useBorderWithImage === true) {
  57. var neutralborderWidth = this.options.borderWidth;
  58. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  59. var borderWidth = (selected ? selectionLineWidth : neutralborderWidth) / this.body.view.scale;
  60. ctx.lineWidth = Math.min(this.width, borderWidth);
  61. ctx.beginPath();
  62. // setup the line properties.
  63. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  64. // set a fillstyle
  65. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  66. // 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
  67. ctx.rect(this.left - 0.5 * ctx.lineWidth,
  68. this.top - 0.5 * ctx.lineWidth,
  69. this.width + ctx.lineWidth,
  70. this.height + ctx.lineWidth);
  71. ctx.fill();
  72. this.performStroke(ctx, values);
  73. ctx.closePath();
  74. }
  75. this._drawImageAtPosition(ctx, values);
  76. this._drawImageLabel(ctx, x, y, selected, hover);
  77. this.updateBoundingBox(x,y);
  78. }
  79. /**
  80. *
  81. * @param {Number} x
  82. * @param {Number} y
  83. */
  84. updateBoundingBox(x, y) {
  85. this.resize();
  86. this._updateBoundingBox(x, y);
  87. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  88. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  89. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  90. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  91. }
  92. }
  93. /**
  94. *
  95. * @param {CanvasRenderingContext2D} ctx
  96. * @param {Number} angle
  97. * @returns {Number}
  98. */
  99. distanceToBorder(ctx, angle) {
  100. return this._distanceToBorder(ctx,angle);
  101. }
  102. }
  103. export default Image;