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.

84 lines
2.7 KiB

  1. 'use strict';
  2. import CircleImageBase from '../util/CircleImageBase'
  3. class Image extends CircleImageBase {
  4. constructor (options, body, labelModule, imageObj, imageObjAlt) {
  5. super(options, body, labelModule);
  6. this.setImages(imageObj, imageObjAlt);
  7. }
  8. resize(ctx, selected = this.selected, hover = this.hover) {
  9. var imageAbsent = (this.imageObj.src === undefined) ||
  10. (this.imageObj.width === undefined) ||
  11. (this.imageObj.height === undefined);
  12. if (imageAbsent) {
  13. var side = this.options.size * 2;
  14. this.width = side;
  15. this.height = side;
  16. return;
  17. }
  18. if (this.needsRefresh(selected, hover)) {
  19. this._resizeImage();
  20. }
  21. }
  22. draw(ctx, x, y, selected, hover, values) {
  23. this.switchImages(selected);
  24. this.resize();
  25. this.left = x - this.width / 2;
  26. this.top = y - this.height / 2;
  27. if (this.options.shapeProperties.useBorderWithImage === true) {
  28. var neutralborderWidth = this.options.borderWidth;
  29. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  30. var borderWidth = (selected ? selectionLineWidth : neutralborderWidth) / this.body.view.scale;
  31. ctx.lineWidth = Math.min(this.width, borderWidth);
  32. ctx.beginPath();
  33. // setup the line properties.
  34. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  35. // set a fillstyle
  36. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  37. // 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
  38. ctx.rect(this.left - 0.5 * ctx.lineWidth,
  39. this.top - 0.5 * ctx.lineWidth,
  40. this.width + ctx.lineWidth,
  41. this.height + ctx.lineWidth);
  42. ctx.fill();
  43. this.performStroke(ctx, values);
  44. ctx.closePath();
  45. }
  46. this._drawImageAtPosition(ctx, values);
  47. this._drawImageLabel(ctx, x, y, selected, hover);
  48. this.updateBoundingBox(x,y);
  49. }
  50. updateBoundingBox(x,y) {
  51. this.resize();
  52. this._updateBoundingBox(x, y);
  53. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  54. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  55. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  56. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  57. }
  58. }
  59. distanceToBorder(ctx, angle) {
  60. return this._distanceToBorder(ctx,angle);
  61. }
  62. }
  63. export default Image;