Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate magnifying glass on Web-page image (Javascript)?

Google has the coolest effects - once it was a Pac-man game, today is apparently the 160th anniversary of the first World Fair, and Google's logo has an image of it. They also turn the mouse into a magnifying glass that can sweep over the picture (the gold ring). enter image description here

I'm wondering how they do that. It's obviously Javascript, and I looked at the page source, but it's not especially readable (no surprise).

like image 833
John C Avatar asked May 01 '11 15:05

John C


People also ask

How do you make a magnifying glass in HTML?

To create image magnifier glass , you must add attribute data-magnifier-mode="glass" . You can use attribute data-lens-type to set type of magnifier lens . To set circle magnifier lens, use value circle . To change size of the lens, use attribute data-lens-size .

How do you make a picture magnifier glass?

function magnify(imgID, zoom) { var img, glass, w, h, bw; img = document. getElementById(imgID); /*create magnifier glass:*/ glass = document. createElement("DIV"); glass. setAttribute("class", "img-magnifier-glass"); /*insert magnifier glass:*/ img.

How do I add a magnifier to a photo?

Tap the three-dot Menu button found in the top-right corner of the screen. From the share sheet, select the “Markup” option. Now, tap the “+” button and choose the “Magnifier” option. You'll now see a magnifying circle in the middle of the image.

What is image magnifier?

They draw an extra layer just on top of your product title, make it invisible, and force the customer to look at somewhere else instead of the cursor area. Image Magnifier is simple and neat, it works like a real magnifying glass. Set Up In Seconds, No Coding.


2 Answers

Looking at their source code, it seems they are using rather basic techniques to achieve this.

Ignoring all the embedded nifty animated gif's, there are basically two images - large, and small of the entire scene. The larger image is repeated thrice in the document. Look at the annotated image below to get a better idea of how the zoom works. annotated zoom

The portion inside the magnifying circle is split up in three div's - top, mid, and bottom. The overflow for each div should be hidden. Each div is relatively positioned inside the zoom circle. On mouse move, change the absolute position of the zoom circle to the mouse coordinates. Their example also uses CSS3 for the scaling and adding some animation delays.

Here's a sorta minimal reconstructed example.

Another example where we don't hide the div overflow to reveal the entire thing as a square.

like image 200
Anurag Avatar answered Oct 22 '22 00:10

Anurag


Well, firstly, for anyone who wants to use such an effect, there are loads of jQuery plugins. Here are just a few:

  1. Power Zoomer
  2. Featured Image Zoomer
  3. Cloud Zoom

Secondly, it's quite easy to achieve. Just load the full-size image, but give it a width smaller than it's actual width, so it is scaled by CSS. Then, use JavaScript+CSS to create a Div (the magnifying glass) with the same image as background, but change the background-position property to the corresponding scaled x,y coordinate that the user's mouse is currently on.

There are other ways of doing it I suppose, and Google might be doing it differently, but this is the most obvious way for me that comes to mind.

like image 31
Herman Schaaf Avatar answered Oct 22 '22 00:10

Herman Schaaf