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.

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