Cropper.js

repository·main·Indexed 12 days ago

https://github.com/fengyuanchen/cropperjs

A JavaScript-based image cropping library. Version 2.1.1 features a modular architecture with custom elements including @cropper/element-canvas, @cropper/element-crosshair, @cropper/element-gird, @cropper/element-handle, @cropper/element-image, @cropper/element-selection, @cropper/element-shade, and @cropper/element-viewer.

Tokens
27.4K
Snippets
95
Records
135
Agent score
95%

What's inside Cropper.js

  1. Use CropperCrosshair

    main

    The CropperCrosshair element is used to display a crosshair overlay within a cropper interface. It can be used as a standalone element or nested within other components like CropperCanvas or CropperSelection.

    Basic Usage

    To render a default crosshair, use the <cropper-crosshair> tag.

    Customizing Color

    You can change the crosshair color using the theme-color attribute.

    Positioning within Components

    • Within CropperCanvas: Use the centered property to ensure the crosshair is centered relative to the canvas.
    • Within CropperSelection: Use the centered property to ensure the crosshair is centered relative to the selection area.
    <!-- Basic -->
    <cropper-crosshair></cropper-crosshair>
    
    <!-- Custom color -->
    <cropper-crosshair theme-color="#39f"></cropper-crosshair>
    
    <!-- Within CropperCanvas -->
    <cropper-canvas style="background-color: #39f;">
      <cropper-crosshair centered></cropper-crosshair>
    </cropper-canvas>
    
    <!-- Within CropperSelection -->
    <cropper-selection width="160" height="90" style="background-color: #39f;">
      <cropper-crosshair centered></cropper-crosshair>
    </cropper-selection>
  2. Use Cropper.js as Web Components (DOM)

    main

    Cropper.js 2.0 is a series of web components. You can use them in the DOM by importing the cropperjs package, which automatically defines all custom elements.

    import 'cropperjs';

    Once imported, you can use elements like <cropper-canvas>, <cropper-image>, <cropper-selection>, etc., directly in your HTML.

  3. Extend CropperElement to create custom Cropper elements

    main

    To create a custom Cropper element, extend the CropperElement class. You can define properties and override observedAttributes to react to attribute changes. Finally, call the $define() method to register your custom element with the browser.

    import { CropperElement } from 'cropperjs';
    // Or
    // import CropperElement from '@cropper/element';
    
    class MyCropperElement extends CropperElement {
      myStringProperty = '';
      myNumberProperty = NaN;
      myBooleanProperty = false;
    
      static get observedAttributes(): string[] {
        return super.observedAttributes.concat([
          'my-boolean-property',
          'my-number-property',
          'my-string-property',
        ]);
      }
    
      // ...
    }
    
    MyCropperElement.$define();
  4. Register @cropper/elements custom elements

    main

    To use the custom elements in your application, you must import the necessary classes and call their $define() method to register them with the browser's Custom Element Registry.

    Commonly used elements include:

    • CropperElement: The base element.
    • CropperCanvas: The canvas element.
    • CropperImage: The image element.
    import { CropperElement, CropperCanvas, CropperImage } from '@cropper/elements';
    
    // You can extend the base element to create your own implementation
    class MyCropperElement extends CropperElement {}
    
    // Register the elements
    MyCropperElement.$define();
    CropperCanvas.$define();
    CropperImage.$define();
  5. Use the CropperHandle element

    main

    The <cropper-handle> element is used to provide interactive handles for manipulation within a cropper environment. By default, the element has a width and height of 0 and is invisible unless configured or placed within a specific context.

    Common usage patterns include:

    • Standalone: Using it as a basic element.
    • Within CropperCanvas: Placing a handle directly in the canvas to perform actions like moving the image.
    • Within CropperSelection: Placing handles inside a selection area to allow resizing or moving the selection itself.
    <!-- Basic usage -->
    <cropper-handle></cropper-handle>
    
    <!-- Within CropperCanvas to move the image -->
    <cropper-canvas background>
      <cropper-image src="/cropperjs/picture.jpg" alt="Picture" rotatable scalable skewable translatable></cropper-image>
      <cropper-handle action="move"></cropper-handle>
    </cropper-canvas>
    
    <!-- Within CropperSelection to move the selection -->
    <cropper-canvas background>
      <cropper-image src="/cropperjs/picture.jpg" alt="Picture" rotatable scalable skewable translatable></cropper-image>
      <cropper-selection width="100" height="100" movable>
        <cropper-handle action="move"></cropper-handle>
      </cropper-selection>
    </cropper-canvas>