野原新之助
野原新之助
  • 发布:2016-12-05 16:44
  • 更新:2020-02-07 01:12
  • 阅读:8290

纯js 头像裁剪

分类:MUI

引用cropper.js 纯js,无需其余第三方插件,完成图片裁剪功能,使用例子在附件中

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="utf-8">  
        <title></title>  
        <link rel="stylesheet" href="js/cropperjs-master/dist/cropper.min.css" />  
        <link rel="stylesheet" href="css/mui.min.css" />  
        <style>  
            #redo{  
                position: fixed;  
                bottom: 20px;  
                left :20px;  
                font-size: 30px;  
            }  
            #undo{  
                position: fixed;  
                bottom: 20px;  
                right :20px;  
                font-size: 30px;  
            }  
        </style>  
    </head>  
    <body>  
        <header class="mui-bar mui-bar-nav blueBackground">  
          <h1 id="patientId" class="mui-title">截取头像</h1>  
          <a id="save" style="vertical-align: middle;color:#8dc14f;line-height:2.5em;" class="mui-pull-right"><span>保存</span></a>  
        </header>  
        <div style="" class="mui-content">  
            <div style="">  
                <img style="width: 98%;" id="image" src="">  
            </div>  
            <button id="redo" type="button" class="mui-btn mui-btn-danger mui-btn-outlined mui-pull-left"><span class="mui-icon mui-icon-redo"></span></button>  
            <button id="undo" type="button" class="mui-btn mui-btn-danger mui-btn-outlined mui-pull-right"><span class="mui-icon mui-icon-undo"></button>  
        </div>  
        <script type="text/javascript" src="js/mui.min.js" ></script>  
        <script type="text/javascript" src="js/cropperjs-master/dist/cropper.min.js" ></script>  
        <script>  
            var image = document.getElementById('image');  
            var cropper = null;  
            var resImg = null;  
            //在选择或者拍摄完成后跳出裁剪页面并把图片路径传递到此页面  
            //image.src = plus.webview.currentWebview().imgSrc;  
            image.src = "img/head-default.jpg";  
            initCropper();  
            document.getElementById("save").addEventListener("tap",getImg);  
            document.getElementById("redo").addEventListener("tap",function(){  
                cropper.rotate(90);  
            });  
            document.getElementById("undo").addEventListener("tap",function(){  
                cropper.rotate(-90);  
            });  
            function initCropper(){  
                cropper = new Cropper(image, {  
                    aspectRatio: 1/1,  
                    dragMode:'move',  
                    rotatable:true,  
                    minCropBoxWidth:200,  
                    minCropBoxHeight:200,  
                    minCanvasWidth:200,  
                    minCanvasHeight:200,  
                    minContainerWidth:200,  
                    minContainerHeight:200,  
                    crop: function(data) {  
                     }  
                });  
            }  
            function getImg(){  
                resImg =  cropper.getCroppedCanvas({  
                      width: 200,  
                      height: 200  
                }).toDataURL();  
                console.log(resImg)  
                //裁剪完毕后将新的图片路径传递到需要显示的页面即可显示 结果为base64格式  
                //mui.fire(plus.webview.getWebviewById("personalInfoPage"),"cropperImg",{resImg:resImg});  
                //mui.back();  
            }  

        </script>  
    </body>  
</html>  
5 关注 分享
2***@qq.com Thyme释念帅 jwenlee 4***@qq.com 1***@qq.com

要回复文章请先登录注册

chinahappybeer

chinahappybeer

用了最新的库文件,保存时显示
Script error.filename:lineno:0
2020-02-07 01:12
chinahappybeer

chinahappybeer

IOS中不能用
2020-02-07 00:53
Yizhiyu

Yizhiyu

图片会变形
2018-05-23 15:55
距离您98米

距离您98米

ios图片变形,怎么解决?
2018-03-07 09:19
距离您98米

距离您98米

回复 k***@126.com :
怎么解决变形?
2018-03-07 09:18
野原新之助

野原新之助 (作者)

回复 3***@qq.com :
你js引入有问题吧
2018-02-02 11:22
3***@qq.com

3***@qq.com

cropper = new Cropper(image, { 这一行报错

ReferenceError: Can't find variable: Cropper at app/cropperPic.html:59
2018-01-29 16:30
2***@qq.com

2***@qq.com

回复 c***@163.com :
存本地文件可以使用plus.nativeObj.Bitmap,将base64转化为图片文件,详细参考:
http://ask.dcloud.net.cn/article/398
2017-06-22 16:14
移动达人

移动达人

感谢!好用,改改代码就融合到项目中了,非常感谢!
2017-06-18 20:37
k***@126.com

k***@126.com

苹果手机拍摄的图片尺寸不正确,变形了
2017-05-23 08:28