• Stars
    star
    1,348
  • Rank 34,849 (Top 0.7 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Fork of fat/zoom.js with improvements

zoom.js

An image zooming plugin, as seen on older versions of medium.com. This project is a port of fat/zoom.js but has no jQuery or Bootstrap dependencies.

Version 4 is written in TypeScript, has a new API, includes typings, and has no dependencies.

npm package: https://www.npmjs.com/package/@nishanths/zoom.js

Branches

  • v4: The default branch. It contains code for version 4, which is the current major version.
  • master: Frozen and no longer maintained. The final version on this branch is 3.1.0.

No API backwards compatibility guarantees even within the same major version, so, if necessary, pin an exact version and upgrade manually.

Demo

https://nishanths.github.io/zoom.js

Zoom on an image by clicking on it.

Dismiss the zoom by either clicking again on the image, clicking the overlay around the image, scrolling away, or hitting the esc key.

Usage

Install the package:

npm i @nishanths/zoom.js

Link the src/zoom.css file in your application:

<link href="zoom.css" rel="stylesheet">

Import and use symbols from the package:

import { zoom } from "@nishanths/zoom.js"

The js files in the dist directory are ES modules.

Note that the package.json for the package specifies the module property but not the main property. You may need a module-aware tool to correctly include the package in your bundle. For further reading, see this Stack Overflow answer as a starting point.

Building locally

To build the package locally, clone the repo, then run the following from the root directory:

% make deps
% make build

This should write files into the dist directory.

Documentation

API

// Config is the configuration provided to the zoom function.
export type Config = {
	// padding defines the horizontal space and the vertical space around
	// the zoomed image.
	padding: number

	// paddingNarrow is similar to the padding property, except that it is
	// used if the viewport width is too narrow, such that the use of the
	// larger padding property may produce poor results.
	//
	// paddingNarrow should be <= padding, however this is not validated.
	paddingNarrow: number

	// dismissScrollDelta defines the vertical scrolling threshold at which
	// the zoomed image is dismissed by user interaction. The value is the
	// pixel difference between the original vertical scroll position and
	// the subsequent vertical scroll positions.
	dismissScrollDelta: number

	// dismissTouchDelta defines the vertical touch movement threshold at
	// which the zoomed image is dismissed by user interaction. The value is
	// the pixel difference between the initial vertical touch position and
	// subsequent vertical touch movements.
	dismissTouchDelta: number
}

// zoom zooms the specified image.
//
// The image will not be zoomed if its naturalWidth or naturalHeight property
// is 0 (usually because the values are unavailable).
export function zoom(img: HTMLImageElement, cfg: Config = defaultConfig): void

// dismissZoom programmatically dismisses the presently active zoom. It is a
// no-op if there is no zoom active at the time of the call.
export function dismissZoom(): void

Examples

The following TypeScript program makes all existing <img> elements on the page zoomable. Images are zoomed when they are clicked.

import { zoom } from "@nishanths/zoom.js"

function setup(img: HTMLImageElement) {
	img.classList.add("zoom-cursor") // use "cursor: zoom-in" style on hover
	img.addEventListener("click", () => { zoom(img) })
}

const imgs = [...document.querySelectorAll("img")]
imgs.forEach(img => { setup(img) })

The following TypeScript program customizes only certain properties of a Config, keeping the defaults for the other properties.

import { Config, defaultConfig } from "@nishanths/zoom.js"

const customConfig: Config = {
	...defaultConfig,
	padding: 30,
}

Notes

All CSS class names used by the package are prefixed with zoom-.

Add the class name zoom-cursor to a zoomable <img> element to use an zoom-in cursor instead of the default cursor for the image.

The program appends the DOM node for the overlay element, which appears when an image is zoomed, to the end of document.body.

While an image is zoomed, the program listens for click events on document.body with useCapture set to true, and the handler function calls e.stopPropagation(). This may interfere with other click event handlers on the page. The event listener is removed when the zoom is dismissed.

When an image is zoomed, its transform style is replaced with a new value that is necessary for zooming. The old transform is restored when the zoom is dismissed.

Browser compatibility

Popular web browser versions released after 2016 should be supported by this package. Please read the source code for exact details.

License

The software in this repository is based on the original fat/zoom.js project. The copyright notices and license notices from the original project are present in the LICENSE.original file at the root of this repository.

New source code and modifications in this repository are licensed under an MIT license. See the LICENSE file at the root of the repository.

More Repositories

1

license

Command line license text generator.
Go
664
star
2

cocoa-hugo-theme

Responsive Hugo blog theme (note: not actively worked on)
CSS
326
star
3

exhaustive

Check exhaustiveness of switch statements of enum-like constants in Go source code.
Go
296
star
4

go-xkcd

xkcd.com API client in Go
Go
51
star
5

lyft

Create and manage Lyft rides from the command line
Go
36
star
6

predeclared

Find declarations in Go source code that shadow predeclared identifiers
Go
28
star
7

loupe-js

Image magnifier written in TypeScript; works with touch events and supports React
TypeScript
20
star
8

slack-texts

SMS notifications for Slack groups
JavaScript
19
star
9

dedupimport

Deduplicate named and unnamed imports of the same import path in Go files
Go
18
star
10

youtube-pause-chrome

Properly pause YouTube with spacebar
JavaScript
16
star
11

YWeatherAPI

Yahoo Weather API wrapper for iOS and Mac
Objective-C
16
star
12

rust-haikunator

Rust
16
star
13

scrobble

Apple Music scrobbling with beautiful album artwork.
Go
10
star
14

what-s-playing

Firefox extension: Desktop notifications for music websites
JavaScript
7
star
15

block-urls

Firefox WebExtension to block exact URLs
JavaScript
4
star
16

QuickCal

Chrome extension: Single-click event adding to Google Calendar
JavaScript
4
star
17

nishanths.github.io

Website + writing with Jekyll (see 'source' branch for old code).
CSS
4
star
18

lyft-go

Go client for Lyft's v1 HTTP API
Go
3
star
19

metaimport

Generate HTML pages with <meta> tags for custom Go import paths
Go
3
star
20

print-instacart

Make a decent PDF of an Instacart order
TypeScript
2
star
21

redditgold

Gold features such as new comment highlighting
JavaScript
2
star
22

godoc-goos-goarch

Easily switch GOOS and GOARCH params on godoc.org
HTML
2
star
23

fullstory

Go client for the FullStory API
Go
1
star
24

albumday

“Album Birthdays!” app source code
Go
1
star
25

unusedargs

See instead: https://github.com/mvdan/unparam
Go
1
star
26

fmap

Generate a map[string][]byte of directory contents
Go
1
star
27

iterators.js

Iterators for node and the browser
JavaScript
1
star
28

spitfire-live

A real-time, personal lyrics assistant (winning project @HackTX 2015)
HTML
1
star
29

auto-browser

Automate the browser based on natural actions – Top 10 @HackTX 2014
JavaScript
1
star
30

impl

Find types that implement an interface
Go
1
star
31

chp

Common Go channel patterns implemented with type parameters.
Go
1
star