v8.89.6

Crop plugin

Use the Crop plugin to manipulate the crop selection and image orientation.

The crop plugin handles the following functionality:

  • Manipulating the crop selection
  • Free rotating the image
  • Exposing crop selection presets
  • Zooming the image
  • Turning the image left and right
  • Flipping the image
  • Showing current crop size

All functionality can be toggled and configured with the properties below:

Property Default value Description
cropMaskOpacity
.85
Adjust the opacity of the mask outside of the crop area.
cropSelectPresetOptions
[]
A list of crop presets.
cropSelectPresetFilter
false
An orientation filter to apply to the crop presets list. Set to 'landscape' to enable orientation tabs in the preset dropdown and show landscape aspect ratios.
cropEnableFilterMatchAspectRatio
true
Automatically switch crop aspect ratio when the preset filter is changed.
cropImageSelectionCornerStyle
'circle'
Determines style of crop selection corners, either 'circle', 'hook', or 'invisible', defaults to 'circle'.
cropWillRenderImageSelectionGuides
undefined
Determines if and how many image selection guides are rendered.
cropWillRenderTools
undefined
Can be used to adjust the items in the crop toolbar.
cropEnableButtonFlipHorizontal
true
Toggle flip horizontal button.
cropEnableButtonFlipVertical
false
Toggle flip vertical button.
cropEnableButtonRotateLeft
true
Toggle rotate left button.
cropEnableButtonRotateRight
false
Toggle Rotate right button.
cropEnableButtonToggleCropLimit
false
Toggle crop limit button.
cropEnableImageSelection
true
Enable or disable the crop selection controls.
cropEnableInfoIndicator
false
Show image size indicator.
cropWillRenderInfoIndicator
undefined
Called before rendering the info indicator, receives crop, and size, should return a string.
cropEnableLimitWheelInputToCropSelection
true
Only captures mouse wheel interactions when the mouse is inside the crop selection.
cropEnableRotationInput
true
Toggle the image rotation input on and off.
cropEnableSelectPreset
true
If crop presets have been defined this will show the preset dropdown in the toolbar.
cropEnableZoom
true
Enable generic zooming of the image. If set to false all zoom related input will be disabled.
cropEnableZoomInput
true
Enable the zoom input control at the bottom of the crop tool. This control will automatically hide if there's not enough vertical space.
cropEnableZoomAutoHide
true
When set to true the zoom control is automatically hidden to preserve space in layouts with low vertical space.
cropActiveTransformTool
'rotation'
Set to 'zoom' to activate the zoom tool instead of the rotation tool.
cropEnableZoomMatchImageAspectRatio
true
Will automatically adjust the crop aspect ratio when zooming out, only works if aspect ratio is set to undefined
cropEnableRotateMatchImageAspectRatio
'never'
Will automatically rotate the crop rectangle with the image if set to 'custom', or 'always'.
cropEnableZoomTowardsWheelPosition
true
Will zoom in towards the mouse position within the crop.
cropEnableCenterImageSelection
true
Toggle the center selection button on and off.
cropAutoCenterImageSelectionTimeout
undefined
Timeout in milliseconds after which the crop automatically re-centers.
cropMinimizeToolbar
'auto'
Set to 'always' to force propagating the toolbar items to the main toolbar instead of showing in separate row.
cropInteractionFocus
'image'
The focus of interaction, defaults to 'image' meaning the user interacts with the image instead of the crop selection. Change to 'selection' to lock the image in place and interact with the selection instead.
cropRotationRange
Math.PI / 4
The negative and positive range the rotation control can move. The default value Math.PI / 4 results in a range from -Math.PI / 4 to Math.PI / 4.

cropMaskOpacity

Adjust the opacity of the mask outside of the crop area, by default is set to .85

cropSelectPresetOptions

A flat or a grouped array of crop presets.

// An example of a flat list of crop aspect ratio presets.
[
    [undefined, 'Custom'],
    [1, 'Square'],
    [4 / 3, 'Landscape'],
    [3 / 4, 'Portrait'],
];

If we don't supply the 'Custom' crop options with value undefined we have to make sure we set the imageCropAspectRatio property to one of the values in the list otherwise the editor won't know which value to select.

// An example of a grouped list of presets.
[
    [
        'Crop Aspect Ratios',
        [
            [undefined, 'Custom'],
            [1, 'Square'],
            [4 / 3, 'Landscape'],
            [3 / 4, 'Portrait'],
        ],
    ],
    [
        'Crop sizes',
        [
            [[180, 180], 'Profile Picture'],
            [[1200, 600], 'Header Image'],
            [[800, 400], 'Timeline Photo'],
        ],
    ],
];

As we could see in the previous code snippets we can describe a crop with [aspectRatio, 'label'] and a size preset with [[width, height], 'label'].

Pintura Image Editor will apply the aspectRatio value to the imageCropAspectRatio prop. When a size is selected it will apply the matching aspect ratio and apply the size to the imageTargetSize prop.

<!DOCTYPE html>

<head>
    <link rel="stylesheet" href="./pintura.css" />
</head>

<style>
    .pintura-editor {
        height: 600px;
    }
</style>

<div id="editor"></div>

<script type="module">
    import { appendDefaultEditor } from './pintura.js';

    const editor = appendDefaultEditor('#editor', {
        src: 'image.jpeg',
        cropSelectPresetOptions: [
            [
                'Crop',
                [
                    [undefined, 'Custom'],
                    [1, 'Square'],
                    [4 / 3, 'Landscape'],
                    [3 / 4, 'Portrait'],
                ],
            ],
            [
                'Size',
                [
                    [[180, 180], 'Profile Picture'],
                    [[1200, 600], 'Header Image'],
                    [[800, 400], 'Timeline Photo'],
                ],
            ],
        ],
    });
</script>

cropSelectPresetFilter

If we want to list a lot of crop aspect ratios the dropdown might get a bit long. The cropSelectPresetFilter option enables us to filter the list of options.

Set to 'landscape' to enable orientation tabs in the preset dropdown and show landscape aspect ratios.

A aspect ratio value of 1 or undefined will always be shown.

<!DOCTYPE html>

<head>
    <link rel="stylesheet" href="./pintura.css" />
</head>

<style>
    .pintura-editor {
        height: 600px;
    }
</style>

<div id="editor"></div>

<script type="module">
    import { appendDefaultEditor } from './pintura.js';

    const editor = appendDefaultEditor('#editor', {
        src: 'image.jpeg',
        cropSelectPresetFilter: 'landscape',
        cropSelectPresetOptions: [
            [undefined, 'Custom'],
            [1, 'Square'],

            // shown when cropSelectPresetFilter is set to 'landscape'
            [2 / 1, '2:1'],
            [3 / 2, '3:2'],
            [4 / 3, '4:3'],
            [16 / 10, '16:10'],
            [16 / 9, '16:9'],

            // shown when cropSelectPresetFilter is set to 'portrait'
            [1 / 2, '1:2'],
            [2 / 3, '2:3'],
            [3 / 4, '3:4'],
            [10 / 16, '10:16'],
            [9 / 16, '9:16'],
        ],
    });
</script>

cropWillRenderImageSelectionGuides

Set opacity to .25 to always render the image selection guides.

interactionFraction tweens from 0 to 1 when the user interacts with the editor UI this allows us to fade in and out the image selection guides.

<!DOCTYPE html>

<head>
    <link rel="stylesheet" href="./pintura.css" />
</head>

<style>
    .pintura-editor {
        height: 600px;
    }
</style>

<div id="editor"></div>

<script type="module">
    import { appendDefaultEditor } from './pintura.js';

    const editor = appendDefaultEditor('#editor', {
        src: 'image.jpeg',
        cropWillRenderImageSelectionGuides: (
            interaction,
            interactionFraction
        ) => {
            const isRotating = interaction === 'rotate';
            return {
                rows: isRotating ? 5 : 3,
                cols: isRotating ? 5 : 3,
                opacity: interactionFraction * 0.25,
            };
        },
    });
</script>

cropWillRenderTools

The cropWillRenderTools hook receives the current tools definition and editor environment parameters. The hook allows injecting custom controls into the tools list using the createNode helper function.

The third parameter of the hook is a redraw function, you can call this function to redraw the UI. Only needed when adjusting external parameters.

<!DOCTYPE html>

<head>
    <link rel="stylesheet" href="./pintura.css" />
</head>

<style>
    .pintura-editor {
        height: 600px;
    }
</style>

<div id="editor"></div>

<script type="module">
    import { appendDefaultEditor, createNode } from './pintura.js';

    const editor = appendDefaultEditor('#editor', {
        src: 'image.jpeg',
        cropWillRenderTools: (tools, env) => {
            console.log(tools);
            // logs: [ Array(4), Array(4), Array(4) ]

            console.log(env);
            // logs: { orientation: "landscape", verticalSpace: "short", … }

            // insert your item
            return [
                createNode('div', 'my-div', { textContent: 'Hello world' }),
                ...tools,
            ];
        },
    });
</script>

cropEnableButtonFlipHorizontal

Toggle flip horizontal button, defaults to true.

cropEnableButtonFlipVertical

Toggle flip vertical button, defaults to false.

cropEnableButtonRotateLeft

Toggle rotate left button, defaults to true.

cropEnableButtonRotateRight

Toggle Rotate right button, defaults to false.

cropEnableButtonToggleCropLimit

Toggle crop limit button, defaults to false.

cropEnableImageSelection

Enable or disable the crop selection controls, defaults to true.

cropEnableInfoIndicator

Show image size indicator, defaults to false.

cropWillRenderInfoIndicator

Called before rendering the info indicator, receives crop, and size, should return a string.

<!DOCTYPE html>

<head>
    <link rel="stylesheet" href="./pintura.css" />
</head>

<style>
    .pintura-editor {
        height: 600px;
    }
</style>

<div id="editor"></div>

<script type="module">
    import { appendDefaultEditor } from './pintura.js';

    const editor = appendDefaultEditor('#editor', {
        src: 'image.jpeg',

        // Have to enable info indicator
        cropEnableInfoIndicator: true,

        // Format custom image info
        cropWillRenderInfoIndicator: (crop, size) => {
            // log params
            console.log(crop, size);

            // build string
            return (
                Math.round(crop.width) + ' &times; ' + Math.round(crop.height)
            );
        },
    });
</script>

cropEnableLimitWheelInputToCropSelection

Only captures mouse wheel interactions when the mouse is inside the crop selection, defaults to true.

cropEnableRotationInput

Toggle the image rotation input on and off, defaults to true.

cropEnableSelectPreset

If crop presets have been defined this will show the preset dropdown in the toolbar, defaults to true.

cropEnableZoom

Enable generic zooming of the image. If set to false all zoom related input will be disabled. Defaults to true.

cropEnableZoomInput

Enable the zoom input control at the bottom of the crop tool. This control will automatically hide if there's not enough vertical space. Defaults to true.

cropEnableZoomAutoHide

When set to true the zoom control is automatically hidden to preserve space in layouts with low vertical space. Defaults to true.

cropActiveTransformTool

Set to 'zoom' to activate the zoom tool instead of the rotation tool. Defaults to 'rotation'.

cropEnableZoomMatchImageAspectRatio

Will automatically adjust the crop aspect ratio when zooming out, only works if aspect ratio is set to undefined. Defaults to true.

cropEnableRotateMatchImageAspectRatio

When set to 'always' or 'custom' this will automatically rotate the crop rectangle with the image is the crop rectangle is fully zoomed out and centered.

Value Description
'never'
Don't rotate the crop when the image is rotated.
'custom'
Only rotate the crop with the image if the crop aspect ratio is set to custom imageCropAspectRatio === undefined
'always'
Rotate the crop rectangle with the image if the crop aspect ratio is custom or a rotated crop aspect ratio can be found in the preset list. For example 4:3 to 3:4.

In this example the crop will automatically switch between 4:3 and 3:4 when the image is rotated while fully zoomed out.

<!DOCTYPE html>

<head>
    <link rel="stylesheet" href="./pintura.css" />
</head>

<style>
    .pintura-editor {
        height: 600px;
    }
</style>

<div id="editor"></div>

<script type="module">
    import { appendDefaultEditor } from './pintura.js';

    const editor = appendDefaultEditor('#editor', {
        src: 'image.jpeg',
        cropEnableRotateMatchImageAspectRatio: 'always',
        cropSelectPresetOptions: [
            [undefined, 'Custom'],
            [1, 'Square'],
            [4 / 3, 'Landscape'],
            [3 / 4, 'Portrait'],
        ],
    });
</script>

cropInteractionFocus

The focus of interaction. Defaults to 'image' meaning the user moves the image and not the crop selection.

Change to 'selection' to lock the image in place and allow the user to move the crop selection instead of the image.

cropRotationRange

The negative and positive range the rotation control can move. The default value Math.PI / 4 results in a range from -Math.PI / 4 to Math.PI / 4.

Crop plugin exports

The core editor module exports the following Crop plugin objects.

Export Description
plugin_crop The crop util view.
plugin_crop_locale_en_gb The crop util locales.