MagnifierLite.js 3.7 KB

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