Code Monkey home page Code Monkey logo

vanilla-image-cropper's Introduction

Lightweight Javascript ImageCropper

Build Status npm npm npm Buy me a beer

A lightweight javascript imagecropper written in vanilla js with zero-dependency injection that builds itself into an object.

For a live preview and some example code, visit http://peterver.github.io/vanilla-image-cropper/

Getting Started

var img_c = new ImageCropper(parent, image, options);

parent

The parent can be one of two things. It's either an HTML5 css selector (such as '#myTestDiv' or '.myclass .mysubclass'), or a dom node.

It should point to the element where you want the imagecropper to be located.

image

The image can either be a javascript Image object loaded through a FileReader, this can be done like so

var reader = new FileReader();

reader.onload = function (evt) {
  var img_c = new ImageCropper(..., evt.target.result, ...);
};

reader.readAsDataURL(...myfile...);

Or you can simply pass an existing url, for example

var img_c = new ImageCropper(..., '../assets/my_img.jpg', ...);

options

There are several possible options defined for the image cropper

  • max_width
    Sets the maximum width that the imagecropper can become

  • max_height
    Sets the maximum height for the imagecropper

  • min_crop_width
    The minimum width that the cropped image can be

  • min_crop_height
    The miminum height that the cropped image can be

  • create_cb
    A callback function that is called when the imagecropper has finished creating, this will pass an object containing the dimensions of the imagecropper ( for styling or positioning purposes )

  • update_cb
    Callback function that is called everytime a move/change happens

  • destroy_cb
    A callback function that is called when the imagecropper has finished destroying itself

  • fixed_size
    A boolean ( true | false ), that tells the image cropper if it should constrain the size of the cropped area to be fixed or not ?

  • mode (default = 'square')
    Sets the type of preview you should see when using the image cropper, possible options are
    • 'square'
    • 'circular'

Cropping an image (mime_type, quality)

When you're all done with your changes, you can crop the image by calling the crop function.

This will return a base64 string that you can then do some funky stuff with.

var img_b64_str = img_c.crop(mime_type, quality);
... // do some funky stuff here

mime_type ( default = 'image/jpeg')

The following mime_types are currently supported in this build, they need to be passed as a string value.

  • image/jpeg
  • image/png

Quality

The quality is a numeric value between 0 and 1. Where 1 is the highest quality setting, and 0 is the lowest quality setting.

Cleaning up an imagecropper instance

img_c = new ImageCropper('#test-imagecrop', 'img.jpg');
img_c.destroy();

or you can just delete the dom node that the image cropper was created in ( or its parent ), and the imagecropper instance will destroy itself :]

Example

For an example, check out the example folder in the repository

Author

Contributors

vanilla-image-cropper's People

Contributors

marcelopc avatar peterver avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vanilla-image-cropper's Issues

Add initialized option

Add an option to execute a callback function when the image cropper has been initialized

Allow circular preview

Add an extra handle cb and option to provide developers with the possibility to provide a circular preview.

This is much used to preview avatar images ( as they are shown in a circular form most of the time.

mobile issues

Hi and many thanks for this project.
Although it works perfectly on desktop brosers I'm having issues with Safari on iOS. I'm unable to try with Chrome on Android. In a few words: i'm unable to drag the handles or move the cropping box. When I do it the entire page scroll/moves but the handles remains in place.
I've tried without success replacing the mouse envents with the pointerevents like, for example:

...
this.$$view.addEventListener('pointerdown', function (e) {
      e.stopPropagation(),
      document.addEventListener('pointerup', o),
      document.addEventListener('pointermove', r)
})
...

I can drag the handles and the box but it's barely usable.
Could you help me find where I'm wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.