cropper.js图片裁剪器js插件


cropper.js图片裁剪器js插件

第一步引入bfwone

第二步执行插件代码

示例如下

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>BFW NEW PAGE</title>
    <script id="bfwone" data="dep=jquery.17&err=0" type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/bfwone.js"></script>
    <script type="text/javascript">
        var $image;
        bready(function() {
            use(["cropper", "cropper"], function() {
                $image = $('#image');

                $image.cropper({
                    aspectRatio: 16 / 9,
                    crop: function(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);
                    }
                });

                // Get the Cropper.js instance after initialized
                var cropper = $image.data('cropper');

            });
        });
        function crop() {
            $image.cropper('getCroppedCanvas', {
                width: 300, // 裁剪后的长宽
                height: 300
            }).toBlob(function(blob) {
                // 裁剪后将图片放到指定标签
                $("#result").attr('src', URL.createObjectURL(blob));
            });
        }
    </script>
    <style>
        /* Limit image width to avoid overflow the container */
        img {
            max-width: 100%;
            /* This rule is very important, please do not ignore this! */
        }
    </style>
</head>
<body>
    <!-- Wrap the image or canvas element with a block element (container) -->
    <div>
        <img id="image" src="http://editor.bfw.wiki/bfwrepo/image/sample4.png">
    </div>
</div>
<button onclick="crop()">裁剪图片</button>
<div>
    结果:
</p>
<img src="" alt="裁剪结果" id="result" />
</div>

</body>
</html>


github地址 https://github.com/fengyuanchen/cropper

立即下载cropper.js查看所有js插件

网友评论0

程序员在线工具箱