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.

95 lines
3.2 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. if (this.needsRefresh(selected, hover)) {
  10. this._resizeImage();
  11. }
  12. }
  13. draw(ctx, x, y, selected, hover, values) {
  14. // switch images depending on 'selected' if imageObjAlt exists
  15. if (this.imageObjAlt) {
  16. this.switchImages(selected);
  17. }
  18. this.selected = selected;
  19. this.resize(ctx, selected, hover);
  20. this.left = x - this.width / 2;
  21. this.top = y - this.height / 2;
  22. if (this.options.shapeProperties.useBorderWithImage === true) {
  23. var neutralborderWidth = this.options.borderWidth;
  24. var selectionLineWidth = this.options.borderWidthSelected || 2 * this.options.borderWidth;
  25. var borderWidth = (selected ? selectionLineWidth : neutralborderWidth) / this.body.view.scale;
  26. ctx.lineWidth = Math.min(this.width, borderWidth);
  27. ctx.beginPath();
  28. // setup the line properties.
  29. ctx.strokeStyle = selected ? this.options.color.highlight.border : hover ? this.options.color.hover.border : this.options.color.border;
  30. // set a fillstyle
  31. ctx.fillStyle = selected ? this.options.color.highlight.background : hover ? this.options.color.hover.background : this.options.color.background;
  32. // 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
  33. ctx.rect(this.left - 0.5 * ctx.lineWidth,
  34. this.top - 0.5 * ctx.lineWidth,
  35. this.width + ctx.lineWidth,
  36. this.height + ctx.lineWidth);
  37. ctx.fill();
  38. //draw dashed border if enabled, save and restore is required for firefox not to crash on unix.
  39. ctx.save();
  40. // if borders are zero width, they will be drawn with width 1 by default. This prevents that
  41. if (borderWidth > 0) {
  42. this.enableBorderDashes(ctx, values);
  43. //draw the border
  44. ctx.stroke();
  45. //disable dashed border for other elements
  46. this.disableBorderDashes(ctx, values);
  47. }
  48. ctx.restore();
  49. ctx.closePath();
  50. }
  51. this._drawImageAtPosition(ctx, values);
  52. this._drawImageLabel(ctx, x, y, selected, hover);
  53. this.updateBoundingBox(x,y);
  54. }
  55. updateBoundingBox(x,y) {
  56. this.resize();
  57. this.left = x - this.width / 2;
  58. this.top = y - this.height / 2;
  59. this.boundingBox.top = this.top;
  60. this.boundingBox.left = this.left;
  61. this.boundingBox.right = this.left + this.width;
  62. this.boundingBox.bottom = this.top + this.height;
  63. if (this.options.label !== undefined && this.labelModule.size.width > 0) {
  64. this.boundingBox.left = Math.min(this.boundingBox.left, this.labelModule.size.left);
  65. this.boundingBox.right = Math.max(this.boundingBox.right, this.labelModule.size.left + this.labelModule.size.width);
  66. this.boundingBox.bottom = Math.max(this.boundingBox.bottom, this.boundingBox.bottom + this.labelOffset);
  67. }
  68. }
  69. distanceToBorder(ctx, angle) {
  70. return this._distanceToBorder(ctx,angle);
  71. }
  72. }
  73. export default Image;