# all files / Github/diagram-js/lib/features/mouse-tracking/ MouseTracking.js

76.19% Statements 16/21
100% Branches 0/0
50% Functions 3/6
76.19% Lines 16/21

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56

    80×
    80×

    80×

    1×

    1×

    1×
    80×
    80×

    80×

    80×

    80×

    80×
    51×

    80×

    1×

    1×

     import { bind } from 'min-dash';

    export default function MouseTracking(eventBus, canvas) {
      this._eventBus = eventBus;
      this._canvas = canvas;

      this._init();
    }

    MouseTracking.$inject = [
      'eventBus',
      'canvas'
    ];

    MouseTracking.prototype.getHoverContext = function() {
      var viewbox = this._canvas.viewbox();

      return {
        element: this._hoverElement,
        point: {
          x: viewbox.x + Math.round(this._mouseX / viewbox.scale),
          y: viewbox.y + Math.round(this._mouseY / viewbox.scale)
        }
      };
    };

    MouseTracking.prototype._init = function() {
      var eventBus = this._eventBus,
          canvas = this._canvas;

      var container = canvas.getContainer();

      this._setMousePosition = bind(this._setMousePosition, this);

      container.addEventListener('mousemove', this._setMousePosition);

      eventBus.on('diagram.destroy', function() {
        container.removeEventListener('mousemove', this._setMousePosition);
      }, this);

      eventBus.on('element.hover', this._setHoverElement, this);
    };

    MouseTracking.prototype._setHoverElement = function(event) {
      this._hoverElement = event.element;
    };

    MouseTracking.prototype._setMousePosition = function(event) {
      this._mouseX = event.layerX;
      this._mouseY = event.layerY;
    };