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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bicycle-codes/image-cropper

v0.0.1

Published

[![types](https://img.shields.io/npm/types/@nichoth/image-cropper?style=flat-square)](README.md) [![module](https://img.shields.io/badge/module-ESM%2FCJS-blue?style=flat-square)](README.md) [![semantic versioning](https://img.shields.io/badge/semver-2.0.0

Downloads

8

Readme

Image Cropper

types module semantic versioning license

An HTML5 image cropping tool. Features a rectangular crop area. The crop area's aspect ratio can be enforced during dragging. The crop image can either be 1:1 or scaled to fit an area. Also supports multitouch on touch supported devices.

fork

This is a fork of AllanBishop/ImageCropper, just packaging and exporting ergonomically for npm consumption.

Thanks Allan Bishop for working in the world of open source.

Screenshot

Screenshot

Live demo

Live demo on JSBin

install

npm i -S @bicycle-codes/image-cropper

Add files

copy

Copy to a location accessible by your web server

cp ./node_modules/@bicycle-codes/image-cropper/dist/index.min.js ./public/image-cropper.js

Add the script to your application.

<script src="/image-cropper.js"></script>

bundle

This is ergonomic to use with a JS bundler such as vite. Just import:

import { ImageCropper } from '@bicycle-codes/image-cropper'

Or import a minified file:

import { ImageCropper } from '@bicycle-codes/image-cropper/min'

example

Given HTML like this

<canvas id="the-canvas" width="600" height="400">
    An alternative text describing what your canvas displays.
</canvas>

In your JS code,

import { ImageCropper } from '@bicycle-codes/image-cropper'

const width = 600
const height = 300
const cropper = new ImageCropper(
    canvas,  // <-- HTML canvas element
    canvas.width / 2 - width / 2,  // <-- left postition of crop area
    canvas.height / 2 - height / 2,  // <-- top position of crop area
    width,  // <-- initial width of the crop area
    height,  // <-- initial height of the crop area
    true  // <-- Keep the aspect ratio to the given width and height
)

const img = document.getElementById('my-image')
cropper.setImage(img)

Public Functions

ImageCropper(canvas, x, y, width, height, keepAspect, touchRadius):void

Constructor function that initializes the image cropper.

| Parameter | Description | | ------ | ----------- | | canvas | The canvas element that the cropping tool will display on.| | x | Optional: The left position of the crop area. | | y | Optional: The top position of the crop area.| | width | Optional: The initial width of the crop area.| | height | Optional: The initial height of the crop area. | | keepAspect | Optional: Enforces that the aspect ratio is kept when dragging the crop area. The aspect ratio is defined by the width and height paramater. | | touchRadius | Optional: The radius for detecting touches/clicks on the corner drag markers and the centre drag marker. |

getCroppedImage(fillWidth, fillHeight):Image

Returns an image that is cropped from the source image based on the crop area. If no fillWidth and fillHeight is set the image will be a 1:1 crop. If fillWidth and fillHeight are set the cropped image will scale to fit. It is recommended to ensure the fillWidth and fillHeight are set to the same aspect ratio as the crop area to prevent distortion.

| Parameter | Description | | ------ | ----------- | | fillWidth| Optional: The fill width that the cropped area will map to.| | fillHeight| Optional: The fill height that the cropped area will map to. |

setImage(image)

Set the image for the image cropper.

| Parameter | Description | | ------ | ----------- | | image| The image that will be used for cropping.

isImageSet():boolean

Checks to see if an image has been set.

getCropBounds():Bounds

Returns the bounds (left, right, top, bottom) of the crop area relative to the original source image.

Example code

<!DOCTYPE html>
<html>
<head lang="en">
		<meta charset="UTF-8">
		<title>Image Cropper Test</title>
	</head>
	<body>
		<div>
			Select image to crop: <input type="file" id="fileInput" name="file" multiple="" />
		</div>
		<div>
			<canvas id="imageCanvas" width="600" height="400" style="border:0px solid #000000;">
			</canvas>
		</div>

		<div>
            Cropped image:
		</div>

		<div id="preview"></div>

		<script src="ImageCropperTest.js"></script>
		<script src="ImageCropper.js"></script>
	</body>
</html>

ImageCropperTest.js

let crop;
window.onload = function () {
    const canvas = document.getElementById("imageCanvas");
    const width = 600;
    const height = 300;

    crop = new ImageCropper(
        canvas,
        canvas.width / 2 - width / 2,
        canvas.height / 2 - height / 2,
        width,
        height,
        true
    );

    window.addEventListener('mouseup', preview);
};

function preview () {
    if (crop.isImageSet()) {
        const img = crop.getCroppedImage(400, 200);
        img.onload = (function () { return previewLoaded(img); });
    }
}

function previewLoaded (img) {
    if (img) {
        document.getElementById("preview").appendChild(img);
    }
}

function handleFileSelect (evt) {
    const file = evt.target.files[0];
    const reader = new FileReader();
    const img = new Image();

    //listener required for FireFox
    img.addEventListener("load", function () {
        crop.setImage(img);
        preview();
    }, false);

    reader.onload = function () {
        img.src = reader.result;
    };

    if (file) {
        reader.readAsDataURL(file);
    }
}

develop

Start a local server of the example directory:

npm start

License

MIT license. See the LICENSE file.