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

crop-rotate-resize-in-browser

v0.0.1

Published

Library for rotating, cropping, and resizing images within browser

Downloads

2

Readme

Image Manipulation Methods

This library exposes a simple API for retrieving, sending, and manipulating images in the browser.

The demo allows the user to select a photo from his or her computer, crop and rotate the image, and upload directly to Google Cloud Storage using the gcs-signed-urls NPM module.

See demo: http://floating-spire-3371.herokuapp.com/

Features

  • Hermite sampling is used to resize images rather than canvas's drawImage. This results in a much better quality photo after resizing. See this stackoverflow.
  • The library parses EXIF meta data to always provide the correct orientation. Photos taken with older cameras rely on EXIF metadata and the browser does not take this metadata into account. See this article.

Install

The library is available on bower and npm.

bower install image-manipulation

OR

npm install image-manipulation

You can access via "window.ImageMethods"

Examples

Manipulators

A canvas can be changed using the manipulator methods: rotate, resize and crop. One can use the static methods.

var canvas = document.querySelector("canvas");

var resizedCanvas = ImageMethods.resize(canvas, 100, 100);

var rotatedCanvas = ImageMethods.rotate(resizedCanvas, 90);

document.body.append(rotatedCanvas);

or one can also make a manipulator instance and chain these methods.

var canvas = document.querySelector("canvas");

var manipulator = new ImageMethods(canvas);

manipulator.resize(100, 100).rotate(90);

document.body.append(manipulator.canvas);

Retrieving images to manipulate

Grab an image from the DOM and flip it upside down

var img = document.querySelector("img"),
	canvas = ImageMethods.getCanvasFromImage(img);

img.src = ImageMethods.rotate(canvas, 180).toDataURL();

Grab an image from an input element (<input type="file" accept="image/*">), create a thumbnail at 200px width and add it to the screen.

document.querySelector("input[type=file]").onchange = function(e) {
	ImageMethods.getCanvasFromFile(e.files[0], function(canvas) {
		
		var manipulator = new ImageMethods(canvas);
		
		manipulator.resize(200);
		
		// Add our resized canvas to the screen
		document.body.appendChild(manipulator.canvas)
		
	});
};

Download an image from the server, cut it into 2 pieces, and upload the pieces back to the server via xhr2

ImageMethods.getCanvasFromUrl("/path/to/image.jpg", function(canvas, file) {
	
	var manipulator = new ImageMethods(canvas);
	
	var piece1Canvas = ImageMethods.crop(0, 0, canvas.width/2, canvas.height),
		piece2Canvas = ImageMethods.crop(canvas.width/2, 0, canvas.width/2, canvas.height),
	
	// Put together FormData for submission
	var formData = new FormData();
	formData.append("images[]", ImageMethods.toBlob(piece1Canvas), file.name);
	formData.append("images[]", ImageMethods.toBlob(piece2Canvas), file.name);
	
	// Post to server
	var xhr = new XMLHttpRequest();
	xhr.open("POST", "/my/upload-handler", true);
	
});

Reference

Chainable instance methods

// Create an manipulator instance likeso:
var instance = new ImageMethods(canvas)

// The canvas element can be exposed
instance.canvas

// You can convert your instance into a blob 
var blob = instance.toBlob();
rotate(degrees)
crop(x, y, width, height)
resize(width, height)

Static methods

These methods follow this form except getCanvasFromImage which simply returns a canvas element.

ImageMethods.getOrientationFromFile(file, function(canvas) {
	// Get access to the canvas element here
});
getCanvasFromImage(img)
getOrientationFromFile(file, callback)
getCanvasFromUrl(url, callback)
getCanvasFromFile(file, callback)