移動端圖片裁剪器,基於 cropperjs
製做的簡單裁剪器javascript
https://github.com/sayll/h5-cropperhtml
HTML部分:java
<!DOCTYPE html> <html lang="cn"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" /> <title>裁剪案例</title> </head> <script src="./h5-cropper.js"></script> <body style="font-size: 14px"> <img id="image" src="http://placekitten.com/g/720/400" width="100%" alt=""> <div> <button style="width: 100%; font-size: 18px;" id="button">開始裁剪</button> </div> </body> <script src="./index.js"></script> </html>
JS部分:git
const img = document.getElementById('image') document.getElementById('button').onclick = function () { var cropper = new window.H5Cropper(img.src, function (base64) { img.src = base64 }) }
演示案例:https://sayll.github.io/h5-cropper/index.htmlgithub
手機模式查看,效果更佳。
經過下載,直接引用dist/index.js
,經過全局的window
對象訪問。typescript
const cropper = new window.H5Cropper('http://placekitten.com/g/300/200', function (base64) { // to do something console.log(base64) })
下載方式:npm
npm i -S h5-cropper
使用方式:函數
import H5Cropper from 'h5-cropper' const cropper = new H5Cropper('http://placekitten.com/g/300/200', function (base64) { // to do something console.log(base64) })
調用方式:url
new H5Cropper(url: string, callback?: (data64: string) => any, options?: CropperjsOptions)
參數 | 描述 |
---|---|
url | 圖片地址或base64 |
callback | 裁剪完成後的回調函數,接收最終的圖片base64 |
options | 一些高級配置,詳細請查看插件Cropperjs |