Magnifier.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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.Magnifier"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.image.Magnifier"] = true;
  8. dojo.provide("dojox.image.Magnifier");
  9. dojo.require("dojox.gfx");
  10. dojo.require("dojox.image.MagnifierLite");
  11. dojo.declare("dojox.image.Magnifier",
  12. dojox.image.MagnifierLite,{
  13. // summary:
  14. // Adds magnification on a portion of an image element, using `dojox.gfx`
  15. //
  16. // description:
  17. // An unobtrusive way to add an unstyled overlay
  18. // above the srcNode image element. The overlay/glass is a
  19. // scaled version of the src image (so larger images sized down
  20. // are clearer).
  21. //
  22. // over-ride the _createGlass method to create your custom surface,
  23. // being sure to create an img node on that surface.
  24. _createGlass: function(){
  25. // summary: create the glassNode, and an img on a dojox.gfx surface
  26. // images are hard to make into workable templates, so just add outer overlay
  27. // and skip using dijit._Templated
  28. this.glassNode = dojo.create('div', {
  29. style:{
  30. height: this.glassSize + "px",
  31. width: this.glassSize + "px"
  32. },
  33. "className":"glassNode"
  34. }, dojo.body());
  35. this.surfaceNode = dojo.create('div', null, this.glassNode);
  36. this.surface = dojox.gfx.createSurface(this.surfaceNode, this.glassSize, this.glassSize);
  37. this.img = this.surface.createImage({
  38. src: this.domNode.src,
  39. width: this._zoomSize.w,
  40. height: this._zoomSize.h
  41. });
  42. },
  43. _placeGlass: function(e){
  44. // summary: position the overlay centered under the cursor
  45. var x = e.pageX - 2,
  46. y = e.pageY - 2,
  47. xMax = this.offset.x + this.offset.w + 2,
  48. yMax = this.offset.y + this.offset.h + 2
  49. ;
  50. // with svg, our mouseout connection to the image surface doesn't
  51. // fire, so we'r have to manually calculate offsets
  52. if(x < this.offset.x || y < this.offset.y || x > xMax || y > yMax){
  53. this._hideGlass();
  54. }else{
  55. this.inherited(arguments);
  56. }
  57. },
  58. _setImage: function(e){
  59. // summary: set the image's offset in the clipping window relative to the mouse position
  60. var xOff = (e.pageX - this.offset.l) / this.offset.w,
  61. yOff = (e.pageY - this.offset.t) / this.offset.h,
  62. x = (this._zoomSize.w * xOff * -1)+(this.glassSize*xOff),
  63. y = (this._zoomSize.h * yOff * -1)+(this.glassSize*yOff)
  64. ;
  65. // set the image offset
  66. this.img.setShape({ x: x, y: y });
  67. }
  68. });
  69. }