简体   繁体   English

使用Javascript裁剪图像(裁剪)

[英]Crop Image using Javascript (Croppie)

I am trying to crop an image to cicle using the Croppie Library 我正在尝试使用Croppie Library将图像裁剪为cicle

I have tried to use their functions to return base64 encoded image. 我尝试使用它们的函数返回base64编码的图像。 And it return a base64 code but without the image: Here is my code: 它返回一个base64代码,但没有图像:这是我的代码:

<div id="vanilla-demo"></div>
 </div>
<img id="myImage" src="">

<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script src="croppie.js"></script>

<script type="text/javascript">


        var vanilla = new Croppie(document.getElementById('vanilla-demo'), {
            viewport: { width: 200, height: 200 , type:'circle'},
            boundary: { width: 400, height: 400 },
            showZoom: false
        });
        vanilla.bind('dac.jpg');
            vanilla.result('canvas','original').then(function (src) {
                    console.log(src);
                    $('#myImage').attr('src', src);
            });

</script>

I'm one of the creators of Croppie. 我是Croppie的创作者之一。 With a quick glance at your code, I'd say that your croppie isn't bound yet. 快速浏览一下您的代码,我想说您的裁剪尚未绑定。 The bind method returns a Promise that you'll need to wait to be resolved. bind方法返回一个Promise,您需要等待它才能解决。 The Promise waits for the image to load and all of the initialization logic to finish on the Croppie. Promise等待图像加载,并在Croppie上完成所有初始化逻辑。

I would change your bind and result logic to do the following instead: 我将更改绑定和结果逻辑来执行以下操作:

vanilla.bind('dac.jpg').then(function() {
    vanilla.result('canvas','original').then(function (src) {
        console.log(src);
        $('#myImage').attr('src', src);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM