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.

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