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.

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