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

svelte-crop

v0.1.0

Published

A Svelte component to crop images with easy interactions

Readme

svelte-crop

A Svelte component to crop images with easy interactions

This is a rewrite of react-easy-crop (https://github.com/valentinh/react-easy-crop).

ℹ️ The minimum supported Svelte version is v5. If you are still on an older version, you need to use the 3.x.x versions of svelte-crop

npm version Monthly downloads gzip size License: GLWTS PRs welcome

svelte-crop Demo

Demo

Features

  • Supports drag and zoom interactions
  • Supports pinch zoom, two-finger rotation, and video sources
  • Supports contain and cover object fits (including horizontal-cover and vertical-cover)
  • Supports arrow-key cropping and restoring saved crops
  • Recalculates the crop when its container resizes
  • Provides crop dimensions as pixels and percentages
  • Supports any images format (JPEG, PNG, even GIF) as url or base 64 string
  • Emits live crop area updates while cropping
  • Mobile friendly

Installation

yarn add svelte-crop

or

npm install svelte-crop --save

Basic usage

The Cropper is styled with position: absolute to take the full space of its parent. Thus, you need to wrap it with an element that uses position: relative or the Cropper will fill the whole page.

<script>
  import Cropper from 'svelte-crop'

  let image = '/images/dog.jpeg'
  let crop = $state({ x: 0, y: 0 });
  let zoom = $state(1);
</script>

<Cropper
  {image}
  bind:crop
  bind:zoom
  oncropcomplete={(e) => console.log(e)}
/>

Props

| Prop | Type | Required | Description | | :----------------- | :---------------------------------- | :------: | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | image | string | one of the two | Image URL or data URL. Provide exactly one of image or video. | | video | string \| { src: string, type?: string }[] | one of the two | Video URL or source list. Video playback is muted, looping, and inline. | | crop | { x: number, y: number } | | Position of the media. { x: 0, y: 0 } centers it under the crop area. | | zoom | number | | Zoom of the media between minZoom and maxZoom. Defaults to 1. | | rotation | number | | Rotation in degrees. Defaults to 0. Use bind:rotation to receive changes from touch and trackpad rotation gestures. | | keyboardStep | number | | Distance in CSS pixels moved by each arrow-key press. Defaults to 1. Hold Shift for a step one fifth as large. | | tabindex | number | | Defaults to 0 so the cropper can receive keyboard focus. Use -1 to remove it from sequential keyboard navigation. | | initialCroppedAreaPercentages | CropArea | | Restores a saved percentage crop when media loads. Takes precedence over initialCroppedAreaPixels. | | initialCroppedAreaPixels | CropArea | | Restores a saved pixel crop when media loads. Percentage crops avoid drift caused by rounded pixel output. | | aspect | number | | Aspect of the cropper. The value is the ratio between its width and its height. The default value is 4/3. | | minZoom | number | | Minimum zoom of the media. Defaults to 1. | | maxZoom | number | | Maximum zoom of the media. Defaults to 3. | | cropShape | 'rect' | 'round' | | Shape of the crop area. Defaults to 'rect'. | | objectFit | 'contain' | 'cover' | 'horizontal-cover' | 'vertical-cover' | | How the media is fitted inside the cropper. contain keeps it fully visible. horizontal-cover fills the cropper width, vertical-cover fills its height, and cover picks whichever keeps the media fully visible. Defaults to contain. | | cropSize | { width: number, height: number } | | Size of the crop area (in pixels). If you don't provide it, it is computed from aspect, the media, and its rotation. | | roundCropAreaPixels | boolean | | Whether to round the crop area dimensions to integer pixels. Defaults to false. | | mediaProps | object | | Attributes spread onto the media element (<img> or <video>), e.g. loading, draggable, referrerPolicy. Internal handlers, classes, and styles always win on conflict. | | classes | { containerClassName?, mediaClassName?, cropAreaClassName? } | | CSS classes merged onto the cropper container, the media, and the crop area. | | showGrid | boolean | | Whether to show or not the grid (third-lines). Defaults to true. | | zoomSpeed | number | | Multiplies the value by which the zoom changes. Defaults to 1. | | zoomWithScroll | boolean | | Whether the mouse wheel zooms the media. Defaults to true. Set to false to let the wheel scroll the page over the cropper. | | crossOrigin | string | | Allows setting the crossOrigin attribute on the image or video. | | restrictPosition | boolean | | Whether the position of the media should be restricted to the boundaries of the cropper. | | oncropcomplete | function(details) | | Called when media loads and after an interaction settles. Use this callback to save the cropped area. | | oncropareachange | function(details) | | Called whenever crop, zoom, rotation, or media dimensions change. It has the same { percent, pixels } payload as oncropcomplete. | | onMediaLoaded | function(mediaSize) | | Called once per media source after it loads and its rendered size is measured. mediaSize is { width, height, naturalWidth, naturalHeight } where width/height are the rendered (object-fit dependent) bounds and natural* the source pixels. Useful for a custom zoom or crop strategy based on media size. |

Restoring a saved crop

Save the percent value from oncropcomplete and pass it back when reopening the editor. Save rotation separately and supply it with the saved area.

<Cropper
  {image}
  bind:crop
  bind:zoom
  bind:rotation
  initialCroppedAreaPercentages={savedArea}
/>

Initial crop props are applied when the media loads. Changing these props alone does not reset an active edit. A container resize preserves the current selection where the zoom limits and position restrictions allow it, without reapplying the initial crop.

For an explicit restore during editing, the package exports getInitialCropFromCroppedAreaPercentages and getInitialCropFromCroppedAreaPixels. Both accept an options object and return { crop, zoom }:

import { getInitialCropFromCroppedAreaPercentages } from 'svelte-crop'

const restored = getInitialCropFromCroppedAreaPercentages({
  croppedAreaPercentages: savedArea,
  mediaSize, // { width, height, naturalWidth, naturalHeight }
  cropSize, // { width, height }
  rotation,
  minZoom: 1,
  maxZoom: 3,
})

crop = restored.crop
zoom = restored.zoom

The pixel helper uses croppedAreaPixels instead. The helpers default to rotation 0 and zoom limits 1–3. They reject non-finite values, non-positive dimensions, and invalid zoom limits with RangeError.

Callbacks

oncropareachange

Use this callback for a live preview of the crop coordinates. Keep oncropcomplete for saving a crop after an interaction settles.

<Cropper
  {image}
  bind:crop
  bind:zoom
  bind:rotation
  oncropareachange={(area) => (liveCrop = area)}
/>

oncropcomplete

<Cropper
  {image}
  bind:crop
  bind:zoom
  oncropcomplete={(e) => console.log(e)}
/>

The detail property is an object with 2 values:

  1. percent: coordinates and dimensions of the cropped area in percentage of the image dimension
  2. pixels: coordinates and dimensions of the cropped area in pixels.

Both arguments have the following shape:

const area = {
  x: number, // x/y are the coordinates of the top/left corner of the cropped area
  y: number,
  width: number, // width of the cropped area
  height: number, // height of the cropped area
}

onMediaLoaded

Called once per media source with its rendered size. Use it to adapt zoom or crop from the measured media, for example to always fit a fixed height:

<Cropper
  {image}
  bind:crop
  bind:zoom
  onMediaLoaded={({ height }) => (zoom = CONTAINER_HEIGHT / height)}
/>

Development

bun install
bun run dev

Open http://localhost:3000 to use the demo. Install the Playwright browser once with bunx playwright install chromium, then run browser tests with bun run e2e. Run bun run unit for helper tests, bun run lint for ESLint and dprint, and bun run check for Svelte and TypeScript diagnostics.

License

GLWTS