Magnifier.js 2.3 KB

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