# ravindrakhokharia/cropperjs architecture

> Architecture diagram of the GitHub repository ravindrakhokharia/cropperjs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ravindrakhokharia/cropperjs
- Repository: https://github.com/ravindrakhokharia/cropperjs

## Overview

1. **What this repo is**
   - `cropperjs` is a browser-side image cropping library, not an application backend.
   - The core runtime manipulates DOM elements, CSS transforms, and HTML canvas to let users position, resize, rotate, scale, and export a crop region.

2. **Public entry point and API boundary**
   - `src/index.js` is the main entry into the library and exposes the `Cropper` class used by consumers as `new Cropper(element, options)`.
   - The public surface is centered on instance methods and custom DOM events; the rest of the source tree is internal support code.

3. **Core object model**
   - `src/js/cropper.js` is the central orchestrator. It likely owns instance state, wires together the rendering and interaction subsystems, and routes method calls.
   - `src/js/defaults.js` and `src/js/constants.js` define configuration defaults and shared symbols/flags that shape behavior across the modules.

4. **Rendering and DOM structure**
   - `src/js/template.js` defines the DOM scaffold for the cropper UI: container, canvas/image wrapper, crop box, and overlay layers.
   - `src/js/render.js` applies state to the DOM and keeps geometry in sync with the current image/crop-box state.
   - The visual style is split out into `src/css/cropper.scss` / `src/css/cropper.css` and bundled into `dist/cropper.css`.

5. **Interaction and state transitions**
   - `src/js/handlers.js` is the input layer for mouse/touch/pointer gestures such as drag, resize, move, and zoom.
   - `src/js/events.js` translates those interactions into the library’s custom event model (`ready`, `cropstart`, `cropmove`, `cropend`, `crop`, `zoom`).
   - `src/js/change.js` appears to centralize state mutations, likely turning gesture deltas and API calls into updated cropper state.

6. **Imperative API methods**
   - `src/js/methods.js` contains the public operations described in the README: move, zoom, rotate, scale, reset, clear, replace, enable/disable, get/set data, set aspect ratio, and export cropped canvas.
   - These methods are the main bridge between external callers and the internal render/state pipeline.

7. **Preview synchronization**
   - `src/js/preview.js` supports preview containers listed in the `preview` option.
   - The data flow here is from cropper state to one or more external preview elements, keeping thumbnails or mirrored views updated whenever the crop box changes.

8. **Geometry and utility layer**
   - `src/js/utilities.js` contains the math and DOM helpers that all other modules rely on for calculations such as sizes, offsets, transforms, and normalization.
   - This layer is crucial because the library’s correctness depends on consistent coordinate conversion between container space, image space, and natural image dimensions.

9. **Runtime data flow**
   - The typical flow is: target image/canvas is initialized → template is inserted into the wrapper → the image is loaded and optionally orientation-adjusted → user gestures or API calls mutate state → render/update modules recalculate layout → custom events fire and previews sync → `getCroppedCanvas()` can export the final crop using browser canvas APIs.
   - The main architectural boundary is between browser DOM interaction and derived crop data, which is what can be consumed by server-side processing or saved for later.

10. **Browser-specific concerns**
   - The README’s cross-origin and EXIF orientation options imply that loading an image may involve extra browser requests and typed-array parsing before the cropper becomes ready.
   - `getCroppedCanvas()` is the export boundary: it turns the cropper’s internal geometry into an actual `HTMLCanvasElement`, which can then be serialized or uploaded.

11. **Distribution/build artifacts**
   - `dist/` contains the shipped bundles: CommonJS, ES module, UMD JavaScript, and compiled CSS.
   - `rollup.config.js`, `postcss.config.js`, and Babel configuration indicate a build pipeline that compiles source modules into browser-ready packages.

12. **Docs, examples, and test surface**
   - `docs/` and `examples/` are reference integrations showing specific behaviors like modal use, cross-origin images, canvas cropping, and upload flows.
   - `test/specs/` is organized by behavior area: events, methods, and options. This mirrors the library’s public architecture and shows that the contract is validated at the API and event level.
   - `types/index.d.ts` provides TypeScript definitions as an external-facing compatibility layer.

13. **How to draw the architecture**
   - A useful diagram would show the user’s image/canvas entering the core `Cropper` instance, with arrows into template/render for DOM construction, handlers/events for interaction, methods for API calls, utilities/constants for shared logic, and preview/export branches for output.
   - Keep CSS, distribution bundles, tests, and docs outside the runtime path as supporting subsystems around the core browser library.

Main entry points: [`src/js/cropper.js`](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/cropper.js) (Cropper), [`src/js/methods.js`](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/methods.js) (Methods), [`src/index.js`](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/index.js) (Entry), [`src/js/render.js`](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/render.js) (Render)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_src_index["Entry<br/>public API<br/>[index.js]"]
  node_src_cropper["Cropper<br/>orchestrator<br/>[cropper.js]"]
  node_src_defaults["Defaults<br/>config<br/>[defaults.js]"]
  node_src_constants["Constants<br/>shared symbols<br/>[constants.js]"]
  node_src_template["Template<br/>DOM scaffold<br/>[template.js]"]
  node_src_render["Render<br/>layout sync<br/>[render.js]"]
  node_src_change["Change<br/>state updates<br/>[change.js]"]
  node_src_handlers["Handlers<br/>input layer<br/>[handlers.js]"]
  node_src_events["Events<br/>[events.js]"]
  node_src_methods["Methods<br/>imperative API<br/>[methods.js]"]
  node_src_preview["Preview<br/>sync output<br/>[preview.js]"]
  node_src_utilities["Utilities<br/>geometry helpers<br/>[utilities.js]"]
end

subgraph group_build["Build & Dist"]
  node_dist_bundle["JS bundles<br/>distribution<br/>[cropper.js]"]
  node_dist_css["CSS bundle<br/>distribution<br/>[cropper.css]"]
  node_src_css["Styles<br/>source css<br/>[cropper.scss]"]
end

subgraph group_support["Support"]
  node_docs_examples["Examples<br/>usage docs"]
  node_docs_site["Docs site<br/>documentation<br/>[index.html]"]
  node_test_specs["Specs<br/>tests"]
  node_types_defs["Types<br/>type defs<br/>[index.d.ts]"]
end

node_src_index -->|"exports"| node_src_cropper
node_src_cropper -->|"uses"| node_src_defaults
node_src_cropper -->|"shares"| node_src_constants
node_src_cropper -->|"builds"| node_src_template
node_src_cropper -->|"renders"| node_src_render
node_src_cropper -->|"mutates"| node_src_change
node_src_cropper -->|"wires"| node_src_handlers
node_src_cropper -->|"exposes"| node_src_methods
node_src_methods -->|"updates"| node_src_change
node_src_methods -->|"emits"| node_src_events
node_src_handlers -->|"drives"| node_src_change
node_src_handlers -->|"fires"| node_src_events
node_src_change -->|"refreshes"| node_src_render
node_src_render -->|"syncs"| node_src_preview
node_src_render -->|"calculates"| node_src_utilities
node_src_template -->|"measures"| node_src_utilities
node_src_methods -->|"reads"| node_src_utilities
node_src_index -.->|"builds to"| node_dist_bundle
node_src_css -.->|"bundles to"| node_dist_css
node_docs_examples -.->|"imports"| node_src_index
node_docs_site -.->|"demonstrates"| node_src_index
node_test_specs -.->|"verifies"| node_src_methods
node_test_specs -.->|"verifies"| node_src_events
node_types_defs -.->|"types"| node_src_index

click node_src_index "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/index.js"
click node_src_cropper "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/cropper.js"
click node_src_defaults "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/defaults.js"
click node_src_constants "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/constants.js"
click node_src_template "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/template.js"
click node_src_render "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/render.js"
click node_src_change "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/change.js"
click node_src_handlers "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/handlers.js"
click node_src_events "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/events.js"
click node_src_methods "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/methods.js"
click node_src_preview "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/preview.js"
click node_src_utilities "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/utilities.js"
click node_dist_bundle "https://github.com/ravindrakhokharia/cropperjs/blob/master/dist/cropper.js"
click node_dist_css "https://github.com/ravindrakhokharia/cropperjs/blob/master/dist/cropper.css"
click node_src_css "https://github.com/ravindrakhokharia/cropperjs/blob/master/src/css/cropper.scss"
click node_docs_examples "https://github.com/ravindrakhokharia/cropperjs/blob/master/examples/cropper-in-modal.html"
click node_docs_site "https://github.com/ravindrakhokharia/cropperjs/blob/master/docs/index.html"
click node_test_specs "https://github.com/ravindrakhokharia/cropperjs/tree/master/test/specs"
click node_types_defs "https://github.com/ravindrakhokharia/cropperjs/blob/master/types/index.d.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_index,node_src_cropper,node_src_defaults,node_src_constants,node_src_template,node_src_render,node_src_change,node_src_handlers,node_src_events,node_src_methods,node_src_preview,node_src_utilities toneBlue
class node_dist_bundle,node_dist_css,node_src_css toneAmber
class node_docs_examples,node_docs_site,node_test_specs,node_types_defs toneMint
```

## Components

### Runtime

Browser-side cropper engine and its internal modules

- [Entry](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/index.js): public API · Main library entry that exports Cropper · `src/index.js`
- [Cropper](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/cropper.js): orchestrator · Central instance that owns state and coordinates subsystems · `src/js/cropper.js`
- [Defaults](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/defaults.js): config · Default options and baseline behavior · `src/js/defaults.js`
- [Constants](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/constants.js): shared symbols · Shared flags and identifiers used across modules · `src/js/constants.js`
- [Template](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/template.js): DOM scaffold · Builds the cropper element structure · `src/js/template.js`
- [Render](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/render.js): layout sync · Applies state to the DOM and keeps geometry current · `src/js/render.js`
- [Change](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/change.js): state updates · Mutates cropper state from API calls and gestures · `src/js/change.js`
- [Handlers](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/handlers.js): input layer · Pointer, mouse, touch, and wheel gesture handling · `src/js/handlers.js`
- [Events](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/events.js): custom events · Emits lifecycle and crop interaction events · `src/js/events.js`
- [Methods](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/methods.js): imperative API · Public operations such as move, zoom, rotate, reset, and export · `src/js/methods.js`
- [Preview](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/preview.js): sync output · Keeps external preview containers aligned with crop state · `src/js/preview.js`
- [Utilities](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/js/utilities.js): geometry helpers · Math and DOM helpers for coordinate conversion and transforms · `src/js/utilities.js`

### Build & Dist

Packaged artifacts and source styling/build inputs

- [JS bundles](https://github.com/ravindrakhokharia/cropperjs/blob/master/dist/cropper.js): distribution · Shipped CommonJS, ESM, and UMD builds · `dist/cropper.js`
- [CSS bundle](https://github.com/ravindrakhokharia/cropperjs/blob/master/dist/cropper.css): distribution · Compiled stylesheet shipped with the library · `dist/cropper.css`
- [Styles](https://github.com/ravindrakhokharia/cropperjs/blob/master/src/css/cropper.scss): source css · Source styles and SCSS for the cropper UI · `src/css/cropper.scss`

### Support

Documentation, examples, tests, and types

- [Examples](https://github.com/ravindrakhokharia/cropperjs/blob/master/examples/cropper-in-modal.html): usage docs · Reference integrations demonstrating options and workflows · `examples/cropper-in-modal.html`
- [Docs site](https://github.com/ravindrakhokharia/cropperjs/blob/master/docs/index.html): documentation · Browser documentation and demo assets · `docs/index.html`
- [Specs](https://github.com/ravindrakhokharia/cropperjs/tree/master/test/specs): tests · Behavioral tests for events, methods, and options · `test/specs`
- [Types](https://github.com/ravindrakhokharia/cropperjs/blob/master/types/index.d.ts): type defs · TypeScript declarations for the public API · `types/index.d.ts`

## Connections

- Entry → Cropper: exports · Public entry exposes the Cropper class
- Cropper → Defaults: uses · Initializes instances from default options
- Cropper → Constants: shares · Consumes shared flags and identifiers
- Cropper → Template: builds · Creates the DOM scaffold for the cropper
- Cropper → Render: renders · Synchronizes instance state to the DOM
- Cropper → Change: mutates · Routes API and interaction updates into state changes
- Cropper → Handlers: wires · Attaches gesture and pointer input handling
- Cropper → Methods: exposes · Public instance operations bridge into internal logic
- Methods → Change: updates · Transforms method calls into state mutations
- Methods → Events: emits · Triggers cropper lifecycle and interaction events
- Handlers → Change: drives · Converts user gestures into state deltas
- Handlers → Events: fires · Reports cropstart, cropmove, cropend, and zoom
- Change → Render: refreshes · Requests visual updates after state changes
- Render → Preview: syncs · Propagates crop state to preview containers
- Render → Utilities: calculates · Uses geometry helpers for layout and transforms
- Template → Utilities: measures · Relies on helper math for sizing and positioning
- Methods → Utilities: reads · Uses helper math for getters, setters, and export geometry
- Entry → JS bundles: builds to · Source is compiled into distributable JavaScript bundles
- Styles → CSS bundle: bundles to · Source styles are compiled into the shipped CSS artifact
- Examples → Entry: imports · Examples exercise the public Cropper entry
- Docs site → Entry: demonstrates · Documentation showcases the same public API
- Specs → Methods: verifies · Method behavior and return values are covered by specs
- Specs → Events: verifies · Event contracts are validated by specs
- Types → Entry: types · Declaration file mirrors the exposed API surface
