JavaScript image cropper. This is the branch for v1.x, for v2.x, check out the
v2branch.
dist/ ├── cropper.css ├── cropper.min.css (compressed) ├── cropper.js (UMD) ├── cropper.min.js (UMD, compressed) ├── cropper.common.js (CommonJS, default) └── cropper.esm.js (ES Module)
npm install cropperjs
In browser:
<link href="/path/to/cropper.css" rel="stylesheet"> <script src="/path/to/cropper.js"></script>
cdnjs provides CDN support for Cropper.js's CSS and JavaScript. You can find the links here.
new Cropper(element[, options])
element
HTMLImageElement or HTMLCanvasElementoptions (optional)
Object<!-- Wrap the image or canvas element with a block element (container) --> <div> <img id="image" src="picture.jpg"> </div>
/* Make sure the size of the image fits perfectly into the container */ img { display: block; /* This rule is very important, please don't ignore this */ max-width: 100%; }
// import 'cropperjs/dist/cropper.css'; import Cropper from 'cropperjs'; const image = document.getElementById('image'); const cropper = new Cropper(image, { aspectRatio: 16 / 9, crop(event) { console.log(event.detail.x); console.log(event.detail.y); console.log(event.detail.width); console.log(event.detail.height); console.log(event.detail.rotate); console.log(event.detail.scaleX); console.log(event.detail.scaleY); }, });
How to crop a new area after zooming in or zooming out?
Just double-click your mouse to enter crop mode.
How to move the image after cropping an area?
Just double-click your mouse to enter move mode.
How to fix the aspect ratio in free ratio mode?
Just hold the
Shiftkey when you resize the crop box.
How to crop a square area in free ratio mode?
Just hold the
Shiftkey when you crop on the image.
The size of the cropper inherits from the size of the image's parent element (wrapper), so be sure to wrap the image with a visible block element.
If you are using cropper in a modal, you should initialize the cropper after the modal is shown completely. Otherwise, you will not get the correct cropper.
The outputted cropped data is based on the original image size, so you can use them to crop the image directly.
If you try to start cropper on a cross-origin image, please make sure that your browser supports HTML5 CORS settings attributes, and your image server supports the Access-Control-Allow-Origin option (see the HTTP access control (CORS)).
Known iOS resource limits: As iOS devices limit memory, the browser may crash when you are cropping a large image (iPhone camera resolution). To avoid this, you may resize the image first (preferably below 1024 pixels) before starting a cropper.
Known image size increase: When exporting the cropped image on the browser side with the HTMLCanvasElement.toDataURL method, the size of the exported image may be greater than the original image's. This is because the type of the exported image is not the same as the original image. So just pass the original image's type as the first parameter to toDataURL to fix this. For example, if the original type is JPEG, then use cropper.getCroppedCanvas().toDataURL('image/jpeg') to export image.
You may set cropper options with new Cropper(image, options).
If you want to change the global default options, You may use Cropper.setDefaults(options).
Number00: no restrictions1: restrict the crop box not to exceed the size of the canvas.2: restrict the minimum canvas size to fit within the container. If the proportions of the canvas and the container differ, the minimum canvas will be surrounded by extra space in one of the dimensions.3: restrict the minimum canvas size to fill fit the container. If the proportions of the canvas and the container are different, the container will not be able to fit the whole canvas in one of the dimensions.Define the view mode of the cropper. If you set viewMode to 0, the crop box can extend outside the canvas, while a value of 1, 2, or 3 will restrict the crop box to the size of the canvas. viewMode of 2 or 3 will additionally restrict the canvas to the container. There is no difference between 2 and 3 when the proportions of the canvas and the container are the same.
String'crop''crop': create a new crop box'move': move the canvas'none': do nothingDefine the dragging mode of the cropper.
NumberNaNDefine the initial aspect ratio of the crop box. By default, it is the same as the aspect ratio of the canvas (image wrapper).
Only available when the
aspectRatiooption is set toNaN.
NumberNaNDefine the fixed aspect ratio of the crop box. By default, the crop box has a free ratio.
ObjectnullThe previous cropped data you stored will be passed to the setData method automatically when initialized.
Only available when the
autoCropoption had set to thetrue.
Element, Array (elements), NodeList or String (selector)''Add extra elements (containers) for preview.
Notes:
aspectRatio option, be sure to set the same aspect ratio to the preview container.overflow: hidden style to the preview container.BooleantrueRe-render the cropper when resizing the window.
BooleantrueRestore the cropped area after resizing the window.
BooleantrueCheck if the current image is a cross-origin image.
If so, a crossOrigin attribute will be added to the cloned image element, and a timestamp parameter will be added to the src attribute to reload the source image to avoid browser cache error.
Adding a crossOrigin attribute to the image element will stop adding a timestamp to the image URL and stop reloading the image. But the request (XMLHttpRequest) to read the image data for orientation checking will require a timestamp to bust the cache to avoid browser cache error. You can set the checkOrientation option to false to cancel this request.
If the value of the image's crossOrigin attribute is "use-credentials", then the withCredentials attribute will set to true when read the image data by XMLHttpRequest.
BooleantrueCheck the current image's Exif Orientation information. Note that only a JPEG image may contain Exif Orientation information.
Exactly, read the Orientation value for rotating or flipping the image, and then override the Orientation value with 1 (the default value) to avoid some issues (1, 2) on iOS devices.
Requires to set both the rotatable and scalable options to true at the same time.
Note: Do not trust this all the time as some JPG images may have incorrect (non-standard) Orientation values
Requires Typed Arrays support (IE 10+).
BooleantrueShow the black modal above the image and under the crop box.
BooleantrueShow the dashed lines above the crop box.
BooleantrueShow the center indicator above the crop box.
BooleantrueShow the white modal above the crop box (highlight the crop box).
BooleantrueShow the grid background of the container.
BooleantrueEnable to crop the image automatically when initialized.
Number0.8 (80% of the image)It should be a number between 0 and 1. Define the automatic cropping area size (percentage).
BooleantrueEnable to move the image.
BooleantrueEnable to rotate the image.
BooleantrueEnable to scale the image.
BooleantrueEnable to zoom the image.
BooleantrueEnable to zoom the image by dragging touch.
BooleantrueEnable to zoom the image by mouse wheeling.
Number0.1Define zoom ratio when zooming the image by mouse wheeling.
BooleantrueEnable to move the crop box by dragging.
BooleantrueEnable to resize the crop box by dragging.
BooleantrueEnable to toggle drag mode between "crop" and "move" when clicking twice on the cropper.
Requires
dblclickevent support.
Number200The minimum width of the container.
Number100The minimum height of the container.
Number0The minimum width of the canvas (image wrapper).
Number0The minimum height of the canvas (image wrapper).
Number0The minimum width of the crop box.
Note: This size is relative to the page, not the image.
Number0The minimum height of the crop box.
Note: This size is relative to the page, not the image.
FunctionnullA shortcut to the ready event.
FunctionnullA shortcut to the cropstart event.
FunctionnullA shortcut to the cropmove event.
FunctionnullA shortcut to the cropend event.
FunctionnullA shortcut to the crop event.
FunctionnullA shortcut to the zoom event.
As there is an asynchronous process when loading the image, you should call most of the methods after ready, except setAspectRatio, replace and destroy.
If a method doesn't need to return any value, it will return the cropper instance (
this) for chain composition.
new Cropper(image, { ready() { // this.cropper[method](argument1, , argument2, ..., argumentN); this.cropper.move(1, -1); // Allows chain composition this.cropper.move(1, -1).rotate(45).scale(1, -1); }, });
Show the crop box manually.
new Cropper(image, { autoCrop: false, ready() { // Do something here // ... // And then this.cropper.crop(); }, });
Reset the image and crop box to its initial states.
Clear the crop box.
url:
StringhasSameSize (optional):
BooleanfalseReplace the image's src and rebuild the cropper.
Enable (unfreeze) the cropper.
Disable (freeze) the cropper.
Destroy the cropper and remove the instance from the image.
offsetX:
NumberoffsetY (optional):
NumberoffsetX.Move the canvas (image wrapper) with relative offsets.
cropper.move(1); cropper.move(1, 0); cropper.move(0, -1);
x:
Numberleft value of the canvasy (optional):
Numbertop value of the canvasx.Move the canvas (image wrapper) to an absolute point.
NumberZoom the canvas (image wrapper) with a relative ratio.
cropper.zoom(0.1); cropper.zoom(-0.1);
ratio:
Numberpivot (optional):
Object{ x: Number, y: Number }

全球首个AI音乐社区
音述AI是全球首个AI音乐社区,致力让每个人都能用音乐表达自我。音述AI提供零门槛AI创作工具,独创GETI法则帮助用户精准定义音乐风格,AI润色功 能支持自动优化作品质感。音述AI支持交流讨论、二次创作与价值变现。针对中文用户的语言习惯与文化背景进行专门优化,支持国风融合、C-pop等本土音乐标签,让技术更好地承载人文表达。


阿里Qoder团队推出的桌面端AI智能体
QoderWork 是阿里推出的本地优先桌面 AI 智能体,适配 macOS14+/Windows10+,以自然语言交互实现文件管理、数据分析、AI 视觉生成、浏览器自动化等办公任务,自主拆解执行复杂工作流,数据本地运行零上传,技能市场可无限扩展,是高效的 Agentic 生产力办公助手。


一站式搞定所有学习需求
不再被海量信息淹没,开始真正理解知识。Lynote 可摘要 YouTube 视频、PDF、文章等内容。即时创建笔记,检测 AI 内容并下载资料,将您的学习效率提升 10 倍。


为AI短剧协作而生
专为AI短剧协作而生的AniShort正式发布,深度重构AI短剧全流程生产模式,整合创意策划、制作执行、实时协作、在线审片、资产复用等全链路功能,独创无限画布、双轨并行工业化工作流与Ani智能体助手,集成多款主流AI大模型,破解素材零散、版本混乱、沟通低效等行业痛点,助力3人团队效率提升800%,打造标准化、可追溯的AI短剧量产体系,是AI短剧团队协同创作、提升制作效率的核心工具。


能听懂你表达的视频模型
Seedance two是基于seedance2.0的中国大模型,支持图像、视频、音频、文本四种模态输入,表达方式更丰富,生成也更可控。


国内直接访问,限时3折
输入简单文字,生成想要的图片,纳米香蕉中文站基于 Google 模型的 AI 图片生成网站,支持文字生图、图生图。官网价格限时3折活动


职场AI,就用扣子
AI办公助手,复杂任务高效处理。办公效率低?扣子空间AI助手支持播客生成、PPT制作、网页开发及报告写作,覆盖科研、商业、舆情等领域的专家Agent 7x24小时响应,生活工作无缝切换,提升50%效率!


多风格AI绘画神器
堆友平台由阿里巴巴设计团队创建,作为一款AI驱动的设计工具,专为设计师提供一站式增长服务。功能覆盖海量3D素材、AI绘画、实时渲染以及专业抠图,显著提升设计品质和效率。平台不仅提供工具,还是一个促进创意交流和个人发展的空间,界面友好,适合所有级别的设计师和创意工作者。


零代码AI应用开发平台
零代码AI应用开发平台,用户只需一句话简单描述需求,AI能自动生成小程序、APP或H5网页应用,无需编写代码。


免费创建高清无水印Sora视频
Vora是一个免费创建高清无水印Sora视频的AI工具
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号