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

viewport-transform

v0.1.1

Published

A utility for handling coordinate transformations between an original image and a viewport

Readme

viewport-transform

A lightweight, platform-agnostic utility for handling coordinate transformations between an original image and a viewport at different zoom levels and pan positions.

npm version Build Status Coverage Status

Installation

npm install viewport-transform

Features

  • Track the relationship between an original image and a displayed viewport
  • Convert coordinates between image space and viewport space
  • Calculate crop parameters based on the current viewport position
  • Manage zoom and pan operations with boundary constraints
  • Calculate the percentage of the original image visible in the viewport
  • Platform agnostic - works in any JavaScript environment

Usage

Basic Example

const { ViewportTransform } = require('viewport-transform');

// Initialize with image and viewport dimensions
const transformer = new ViewportTransform({
  imageWidth: 1000,  // Original image width
  imageHeight: 800,  // Original image height
  viewportWidth: 500, // Displayed viewport width
  viewportHeight: 400 // Displayed viewport height
});

// Set zoom level (e.g., from pinch gesture)
transformer.setZoom(2.5);

// Set center position (e.g., from pan gesture)
transformer.setCenter(600, 500);

// Get the visible area in original image coordinates
const visibleRect = transformer.getVisibleRect();
console.log(visibleRect);
// { x: 500, y: 420, width: 200, height: 160 }

// Convert viewport coordinates to image coordinates
const imagePoint = transformer.viewportToImage(250, 200);
console.log(imagePoint);
// { x: 600, y: 500 }

// Convert image coordinates to viewport coordinates
const viewportPoint = transformer.imageToViewport(700, 600);
console.log(viewportPoint);
// { x: 500, y: 450 }

// Get crop parameters for the current view
const cropParams = transformer.getCropParameters();
console.log(cropParams);
// { x: 500, y: 420, width: 200, height: 160 }

// Get percentage of image visible in viewport
const visiblePercentage = transformer.getVisiblePercentage();
console.log(visiblePercentage);
// 4% (because we're zoomed in to 2.5x)

Integration with UI Frameworks

This library is framework-agnostic and can be integrated with any UI technology:

React Example

import React, { useState } from 'react';
import { ViewportTransform } from 'viewport-transform';

function ImageEditor({ imageSrc, imageWidth, imageHeight }) {
  const [transformer] = useState(() => new ViewportTransform({
    imageWidth,
    imageHeight,
    viewportWidth: window.innerWidth,
    viewportHeight: window.innerHeight
  }));
  
  // Handle zoom gesture
  const handlePinch = (scale) => {
    transformer.setZoom(scale);
    forceUpdate();
  };
  
  // Handle pan gesture
  const handlePan = (x, y) => {
    const imagePoint = transformer.viewportToImage(x, y);
    transformer.setCenter(imagePoint.x, imagePoint.y);
    forceUpdate();
  };
  
  // Get current crop parameters for display or processing
  const cropParams = transformer.getCropParameters();
  
  return (
    <div>
      {/* Implement UI using the transformer */}
    </div>
  );
}

React Native Example

import React, { useRef } from 'react';
import { View, PanResponder, Image } from 'react-native';
import { ViewportTransform } from 'viewport-transform';

function ImageCropper({ imageUri, imageWidth, imageHeight }) {
  const transformer = useRef(new ViewportTransform({
    imageWidth,
    imageHeight,
    viewportWidth: screenWidth,
    viewportHeight: screenHeight
  })).current;
  
  // Implement pan and pinch gestures using PanResponder
  // ...
  
  // Use transformer to manage coordinates
  // ...
  
  return (
    <View>
      {/* UI implementation */}
    </View>
  );
}

API Reference

Constructor

new ViewportTransform({
  imageWidth,    // Width of original image (pixels)
  imageHeight,   // Height of original image (pixels)
  viewportWidth, // Width of viewport (pixels)
  viewportHeight // Height of viewport (pixels)
})

Methods

Zoom Operations

  • getZoom() - Returns the current zoom level
  • setZoom(zoom) - Sets the zoom level, clamped to min/max values

Pan Operations

  • getCenter() - Returns the current center position as {x, y}
  • setCenter(x, y) - Sets the center position, constrained to valid bounds

Coordinate Conversion

  • viewportToImage(viewportX, viewportY) - Converts viewport coordinates to image coordinates
  • imageToViewport(imageX, imageY) - Converts image coordinates to viewport coordinates

Viewport Information

  • getVisibleRect() - Returns the visible rectangle in image coordinates as {x, y, width, height}
  • getVisiblePercentage() - Returns the percentage of the original image visible in the viewport
  • getCropParameters() - Returns crop parameters based on current viewport as {x, y, width, height}

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.