MagnifierLite.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.image.MagnifierLite"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.image.MagnifierLite"] = true;
  8. dojo.provide("dojox.image.MagnifierLite");
  9. dojo.experimental("dojox.image.MagnifierLite");
  10. dojo.require("dijit._Widget");
  11. dojo.declare("dojox.image.MagnifierLite", dijit._Widget,
  12. {
  13. // summary: Adds magnification on a portion of an image element
  14. //
  15. // description: An unobtrusive way to add an unstyled overlay
  16. // above the srcNode image element. The overlay/glass is a
  17. // scaled version of the src image (so larger images sized down
  18. // are clearer).
  19. //
  20. // The logic behind requiring the src image to be large is
  21. // "it's going to be downloaded, anyway" so this method avoids
  22. // having to make thumbnails and 2 http requests among other things.
  23. //
  24. // glassSize: Int
  25. // the width and height of the bounding box
  26. glassSize: 125,
  27. // scale: Decimal
  28. // the multiplier of the Mangification.
  29. scale: 6,
  30. postCreate: function(){
  31. this.inherited(arguments);
  32. // images are hard to make into workable templates, so just add outer overlay
  33. // and skip using dijit._Templated
  34. this._adjustScale();
  35. this._createGlass();
  36. this.connect(this.domNode,"onmouseenter","_showGlass");
  37. this.connect(this.glassNode,"onmousemove","_placeGlass");
  38. this.connect(this.img,"onmouseout","_hideGlass");
  39. // when position of domNode changes, _adjustScale needs to run.
  40. // window.resize isn't it always, FIXME:
  41. this.connect(window,"onresize","_adjustScale");
  42. },
  43. _createGlass: function(){
  44. // summary: make img and glassNode elements as children of the body
  45. var node = this.glassNode = dojo.create('div', {
  46. style:{
  47. height: this.glassSize + "px",
  48. width: this.glassSize + "px"
  49. },
  50. className:"glassNode"
  51. }, dojo.body());
  52. this.surfaceNode = node.appendChild(dojo.create('div'));
  53. this.img = dojo.place(dojo.clone(this.domNode), node);
  54. // float the image around inside the .glassNode
  55. dojo.style(this.img, {
  56. position: "relative",
  57. top: 0, left: 0,
  58. width: this._zoomSize.w + "px",
  59. height: this._zoomSize.h + "px"
  60. });
  61. },
  62. _adjustScale: function(){
  63. // summary: update the calculations should this.scale change
  64. this.offset = dojo.coords(this.domNode, true);
  65. this._imageSize = { w: this.offset.w, h:this.offset.h };
  66. this._zoomSize = {
  67. w: this._imageSize.w * this.scale,
  68. h: this._imageSize.h * this.scale
  69. };
  70. },
  71. _showGlass: function(e){
  72. // summary: show the overlay
  73. this._placeGlass(e);
  74. dojo.style(this.glassNode, {
  75. visibility: "visible",
  76. display:""
  77. });
  78. },
  79. _hideGlass: function(e){
  80. // summary: hide the overlay
  81. dojo.style(this.glassNode, {
  82. visibility: "hidden",
  83. display:"none"
  84. });
  85. },
  86. _placeGlass: function(e){
  87. // summary: position the overlay centered under the cursor
  88. this._setImage(e);
  89. var sub = Math.floor(this.glassSize / 2);
  90. dojo.style(this.glassNode,{
  91. top: Math.floor(e.pageY - sub) + "px",
  92. left:Math.floor(e.pageX - sub) + "px"
  93. });
  94. },
  95. _setImage: function(e){
  96. // summary: set the image's offset in the clipping window relative to the mouse position
  97. var xOff = (e.pageX - this.offset.l) / this.offset.w,
  98. yOff = (e.pageY - this.offset.t) / this.offset.h,
  99. x = (this._zoomSize.w * xOff * -1) + (this.glassSize * xOff),
  100. y = (this._zoomSize.h * yOff * -1) + (this.glassSize * yOff);
  101. dojo.style(this.img, {
  102. top: y + "px",
  103. left: x + "px"
  104. });
  105. },
  106. destroy: function(finalize){
  107. dojo.destroy(this.glassNode);
  108. this.inherited(arguments);
  109. }
  110. });
  111. }