openseadragon-html-overlay.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // OpenSeadragon HTML Overlay plugin 0.0.2
  2. (function() {
  3. var $ = window.OpenSeadragon;
  4. if (!$) {
  5. $ = require('openseadragon');
  6. if (!$) {
  7. throw new Error('OpenSeadragon is missing.');
  8. }
  9. }
  10. // ----------
  11. $.Viewer.prototype.htmlOverlay = function (options) {
  12. options = options || {};
  13. if (this._htmlOverlayInfo) {
  14. return this._htmlOverlayInfo;
  15. }
  16. this._htmlOverlayInfo = new Overlay(this);
  17. if (options.scale) {
  18. this._htmlOverlayInfo._scale = options.scale; // arbitrary scale for created overlay element
  19. }
  20. else {
  21. this._htmlOverlayInfo._scale = 1;
  22. }
  23. return this._htmlOverlayInfo;
  24. };
  25. // ----------
  26. var Overlay = function(viewer) {
  27. var self = this;
  28. this._viewer = viewer;
  29. this._element = document.createElement('div');
  30. this._element.style.position = 'absolute';
  31. this._element.style.left = 0;
  32. this._element.style.top = 0;
  33. this._element.style.width = '100%';
  34. this._element.style.height = '100%';
  35. this._element.style.transformOrigin = '0 0';
  36. this._viewer.canvas.appendChild(this._element);
  37. // OpenSeadragon blocks the normal click event action, so we have to reestablish it for links here
  38. new OpenSeadragon.MouseTracker({
  39. element: this._element,
  40. clickHandler: function(event) {
  41. var clickTarget = event.originalEvent.target;
  42. if (/a/i.test(clickTarget.nodeName)) {
  43. if (clickTarget.target === '_blank') {
  44. window.open(clickTarget.href);
  45. } else {
  46. location.href = clickTarget.href;
  47. }
  48. }
  49. }
  50. });
  51. this._viewer.addHandler('animation', function() {
  52. self.resize();
  53. });
  54. this._viewer.addHandler('open', function() {
  55. self.resize();
  56. });
  57. this._viewer.addHandler('rotate', function(evt) {
  58. self.resize();
  59. });
  60. this._viewer.addHandler('resize', function() {
  61. self.resize();
  62. });
  63. this.resize();
  64. };
  65. // ----------
  66. Overlay.prototype = {
  67. // ----------
  68. element: function() {
  69. return this._element;
  70. },
  71. // ----------
  72. resize: function() {
  73. var p = this._viewer.viewport.pixelFromPoint(new $.Point(0, 0), true);
  74. var zoom = this._viewer.viewport.getZoom(true);
  75. var rotation = this._viewer.viewport.getRotation();
  76. // TODO: Expose an accessor for _containerInnerSize in the OSD API so we don't have to use the private variable.
  77. var scale = this._viewer.viewport._containerInnerSize.x * zoom / this._scale;
  78. this._element.style.transform =
  79. 'translate(' + p.x + 'px,' + p.y + 'px) scale(' + scale + ') rotate(' + rotation + ')';
  80. },
  81. // ----------
  82. onClick: function(element, handler) {
  83. // TODO: Fast click for mobile browsers
  84. new $.MouseTracker({
  85. element: element,
  86. clickHandler: handler
  87. }).setTracking(true);
  88. }
  89. };
  90. })();