source: Dev/branches/rest-dojo-ui/client/dojox/image/Magnifier.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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