48790aa125dae0ace9916315ada14e8bba4d6acd73517b81ef0973073c8651d41a1ce5bd9d61d4b4c87bb68b2ef13bfbb2523f2c70bbecf89c98dc8e175345 933 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. import Chart from 'chart.js';
  3. var helpers = Chart.helpers;
  4. var HitBox = function() {
  5. this._rect = null;
  6. this._rotation = 0;
  7. };
  8. helpers.extend(HitBox.prototype, {
  9. update: function(center, rect, rotation) {
  10. var margin = 1;
  11. var cx = center.x;
  12. var cy = center.y;
  13. var x = cx + rect.x;
  14. var y = cy + rect.y;
  15. this._rotation = rotation;
  16. this._rect = {
  17. x0: x - margin,
  18. y0: y - margin,
  19. x1: x + rect.w + margin * 2,
  20. y1: y + rect.h + margin * 2,
  21. cx: cx,
  22. cy: cy,
  23. };
  24. },
  25. contains: function(x, y) {
  26. var me = this;
  27. var rect = me._rect;
  28. var cx, cy, r, rx, ry;
  29. if (!rect) {
  30. return false;
  31. }
  32. cx = rect.cx;
  33. cy = rect.cy;
  34. r = me._rotation;
  35. rx = cx + (x - cx) * Math.cos(r) + (y - cy) * Math.sin(r);
  36. ry = cy - (x - cx) * Math.sin(r) + (y - cy) * Math.cos(r);
  37. return !(rx < rect.x0
  38. || ry < rect.y0
  39. || rx > rect.x1
  40. || ry > rect.y1);
  41. }
  42. });
  43. export default HitBox;