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.

83 lines
2.8 KiB

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