JavaScript image cropper. This is the branch for v1.x, for v2.x, check out the
v2
branch.
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 HTMLCanvasElement
options (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
Shift
key when you resize the crop box.
How to crop a square area in free ratio mode?
Just hold the
Shift
key 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)
.
Number
0
0
: 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.
Number
NaN
Define 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
aspectRatio
option is set toNaN
.
Number
NaN
Define the fixed aspect ratio of the crop box. By default, the crop box has a free ratio.
Object
null
The previous cropped data you stored will be passed to the setData
method automatically when initialized.
Only available when the
autoCrop
option 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.Boolean
true
Re-render the cropper when resizing the window.
Boolean
true
Restore the cropped area after resizing the window.
Boolean
true
Check 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.
Boolean
true
Check 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+).
Boolean
true
Show the black modal above the image and under the crop box.
Boolean
true
Show the dashed lines above the crop box.
Boolean
true
Show the center indicator above the crop box.
Boolean
true
Show the white modal above the crop box (highlight the crop box).
Boolean
true
Show the grid background of the container.
Boolean
true
Enable to crop the image automatically when initialized.
Number
0.8
(80% of the image)It should be a number between 0 and 1. Define the automatic cropping area size (percentage).
Boolean
true
Enable to move the image.
Boolean
true
Enable to rotate the image.
Boolean
true
Enable to scale the image.
Boolean
true
Enable to zoom the image.
Boolean
true
Enable to zoom the image by dragging touch.
Boolean
true
Enable to zoom the image by mouse wheeling.
Number
0.1
Define zoom ratio when zooming the image by mouse wheeling.
Boolean
true
Enable to move the crop box by dragging.
Boolean
true
Enable to resize the crop box by dragging.
Boolean
true
Enable to toggle drag mode between "crop"
and "move"
when clicking twice on the cropper.
Requires
dblclick
event support.
Number
200
The minimum width of the container.
Number
100
The minimum height of the container.
Number
0
The minimum width of the canvas (image wrapper).
Number
0
The minimum height of the canvas (image wrapper).
Number
0
The minimum width of the crop box.
Note: This size is relative to the page, not the image.
Number
0
The minimum height of the crop box.
Note: This size is relative to the page, not the image.
Function
null
A shortcut to the ready
event.
Function
null
A shortcut to the cropstart
event.
Function
null
A shortcut to the cropmove
event.
Function
null
A shortcut to the cropend
event.
Function
null
A shortcut to the crop
event.
Function
null
A 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:
String
hasSameSize (optional):
Boolean
false
Replace 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:
Number
offsetY (optional):
Number
offsetX
.Move the canvas (image wrapper) with relative offsets.
cropper.move(1); cropper.move(1, 0); cropper.move(0, -1);
x:
Number
left
value of the canvasy (optional):
Number
top
value of the canvasx
.Move the canvas (image wrapper) to an absolute point.
Number
Zoom the canvas (image wrapper) with a relative ratio.
cropper.zoom(0.1); cropper.zoom(-0.1);
ratio:
Number
pivot (optional):
Object
{ x: Number, y: Number }
一键生成PPT和Word,让学习生活更轻松
讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。
深度推理能力全新升级,全面对标OpenAI o1
科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。
一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型
Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。
字节跳动发布的AI编程神器IDE
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。
AI助力,做PPT更简单!
咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。
选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。