npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

z-image-preview

v3.1.4

Published

A TypeScript image preview package.

Readme

z-image-preview

A TypeScript image preview package.

Install

npm install z-image-preview

Usage

import { ImagePreview } from 'z-image-preview'

const preview = new ImagePreview({
  container: document.querySelector<HTMLDivElement>('#preview')!,
  url: ['/image-1.jpg', '/image-2.jpg'],
  zoom: true,
  move: true,
  fit: 'cover',
  minScale: 0.5,
  maxScale: 6
})

preview.on('load', matrix => console.log('图片首次绘制完成', matrix))
preview.on('change', (index, url) => console.log('切换图片', index, url))
preview.on('zoom', matrix => console.log('缩放', matrix))
preview.on('drag', matrix => console.log('拖拽', matrix))

preview.rotate(90)

rotate(degrees?: number): number:传入 degrees 时设置角度,始终返回当前角度。 角度沿用现有规则归一化到 [0, 360);不传参数时仅读取,不触发重绘。

preview.rotate(90) // 设置并返回 90
preview.rotate() // 返回当前角度 90

Options

interface ImagePreviewOptions {
  container: HTMLDivElement
  url?: ImagePreviewSource | ImagePreviewSource[]
  zoom?: boolean
  move?: boolean
  fit?: 'contain' | 'cover'
  minScale?: number
  maxScale?: number
  index?: number
}

zoom and move default to true. fit defaults to cover. minScale defaults to 0.2, and maxScale defaults to 8.

ImagePreviewSource is exported as string | Blob | ArrayBuffer | ArrayBufferView. Both url and setUrl() accept URLs, Data URLs, File, Blob, ArrayBuffer, TypedArrays (including Uint8Array), DataView, or a mixed array of these sources. Binary inputs must contain encoded image data supported by the browser, rather than raw pixels. preview.url and the change event return the original source. Internal object URLs are released after loading, failure, or cancellation; caller-provided URLs remain caller-managed.

await preview.setUrl(file)
await preview.setUrl(new Uint8Array(buffer))
await preview.setUrl(['/image.jpg', file, blob, buffer])

setUrl() 返回 Promise<void>,等待当前图片加载和本次绘制处理结束;数组只加载当前索引对应的图片。 change 仍同步触发。加载失败仍通过 error 事件通知,Promise 正常结束;清空或取消加载也会结束等待。 容器宽高为零时,Promise 不等待容器恢复尺寸,实际首次绘制完成请监听 load。

Events

事件通过实例的 on(event, fn) 注册,构造参数不再接收事件回调。支持同一事件注册多个监听, 同一事件重复注册相同函数只保留一次;on() 和 off() 均返回当前实例,支持链式调用。

import type { ImageMatrix } from 'z-image-preview'

const handleLoad = (matrix: ImageMatrix) => console.log('图片首次绘制完成', matrix)

preview.on('load', handleLoad)
preview.on('render', matrix => console.log('绘制', matrix))
preview.on('error', error => console.error(error))

preview.off('load', handleLoad) // 去除指定回调函数,需传入注册时的同一函数引用
preview.off('load') // 去除该事件所有回调函数
preview.off() // 去除所有事件所有回调函数

事件名称及参数类型:

interface ImagePreviewEventMap {
  load: [matrix: ImageMatrix]
  render: [matrix: ImageMatrix]
  error: [error: unknown]
  change: [index: number, url: ImagePreviewSource | undefined]
  zoom: [matrix: ImageMatrix]
  drag: [matrix: ImageMatrix]
}

ImagePreviewEventMap 和 ImagePreviewEventListener<Event> 均可从包中导入。 监听按注册顺序同步执行;监听中新增的回调从下一次事件开始生效,已移除的回调本轮也不再执行。 destroy() 会清空全部监听,销毁后注册监听无效。初始化图片的加载是异步的, 在构造实例后立即注册 load 监听即可接收首次绘制通知;事件不会补发注册前的通知。

load 在每次图片加载后首次完成画布绘制时触发一次,参数为该次绘制的 ImageMatrix。初始化、切换图片或调用 setUrl() 加载图片后均会触发;缩放、拖拽、旋转、 重置和后续容器尺寸变化不会重复触发。容器宽高为零时,等待容器恢复尺寸并完成绘制后再触发; 空图片、加载失败或已取消的加载不触发。回调内可以读取 getOriginalSize() 和 getMatrix()。

render 在图片画面完成画布绘制后同步触发,参数为当前 ImageMatrix;包括图片加载、缩放、 拖拽、旋转、重置和容器尺寸变化,空画面不触发。error 在当前图片加载失败时接收错误,已取消的加载不触发。

change 在 show()、setIndex()、next()、prev() 或 setUrl() 改变当前索引或 URL 后触发,不等待图片加载,初始化不触发;清空 URL 时, url 为 undefined。zoom 在滚轮或 setScale() 实际改变缩放后触发,达到缩放边界时不触发。 drag 在拖拽实际移动时触发。reset() 不触发缩放或拖拽事件。 ImageMatrix 可从包中导入,包含 x、y、width、height、scale、rotate, 与 getMatrix() 的返回值一致。

原图尺寸与交互开关

// 图片完成绘制后读取,返回 { width, height },单位为像素。
const size = preview.getOriginalSize()

// 分别设置拖拽、滚轮缩放;未传入的选项保持原值。
preview.setInteraction({ move: false, zoom: false })
preview.setInteraction({ move: true })
preview.setInteraction({ zoom: true })

getOriginalSize() 返回当前图片的 naturalWidth 和 naturalHeight,不受缩放、旋转或容器尺寸影响。 图片尚未加载、加载失败或 URL 为空时返回 undefined;可以在 load 或 render 监听中读取。 关闭拖拽会立即终止当前拖动并释放指针捕获。交互开关在切换图片后继续生效, zoom 仅控制滚轮交互,主动调用 setScale() 仍然有效。

缩放

preview.setScale('enlarge') // 放大 0.05
preview.setScale('reduce') // 缩小 0.05
preview.setScale(2) // 设置为初始适配尺寸的 2 倍

setScale(data: 'enlarge' | 'reduce' | number): void 以当前图片中心缩放, 倍率受 minScale、maxScale 限制,非有限数字会被忽略。

Publish

pnpm run build
npm publish