0

这是我的 HTML 元素

<div class="wrapper">
    <nav class="navbar navbar-transparent">
      <div class="container-fluid">
        <div class="navbar-header">
            <span class="btn btn-white btn-file">
                <i class="material-icons">file_upload</i> Upload your file
                <input type="file" id="upload-image">
            </span>
        </div>
      </div>
    </nav>
    <div class="container-fluid">
        <div class="row">
            <div id="image-display" class="col-md-6 col-md-offset-2">
                <canvas id="edit-canvas" width="400">
            </div>
            <div id="toolbox" class="col-md-2">
                <div class="color-slider">
                    <strong>Brightness</strong>
                    <div id="brightnessSlider" class="slider slider-info">
                    </div>
                    <span id="brightness"></span>
                </div>
                <div class="color-slider">
                    <strong>Saturation</strong>
                    <div id="saturateSlider" class="slider slider-info">
                    </div>
                    <span id="saturate"></span>
                </div>
            </div>
        </div>
    </div>
</div>

我包括 CamanJS 以控制画布过滤效果。这是我的 JavaScript 代码,用于将图像上传到画布、调整大小和过滤它

// Upload, resize image, draw canvas

function fileOnload(e) {
    var img=new Image;
    var canvas=document.getElementById('edit-canvas');
    var context=canvas.getContext('2d');
    img.onload=function(){
        canvas.height=canvas.width * img.height / img.width;
        var oc = document.createElement('canvas'), octx = oc.getContext('2d');
        oc.width = img.width;
        oc.height = img.height;
        octx.drawImage(img, 0, 0);
        while (oc.width * 0.5 > canvas.width) {
            oc.width *= 0.5;
            oc.height *= 0.5;
            octx.drawImage(oc, 0, 0, oc.width, oc.height);
        }
        oc.width = canvas.width;
        oc.height = oc.width * img.height / img.width;
        context.drawImage(img, 0, 0, oc.width, oc.height);
    };
    img.src=e.target.result;
    Caman('#edit-canvas',function(){
        this.render();
    });
};

$('#upload-image').change(function(e){
    var file = e.target.files[0];
    var imageType = /image.*/;
    if (!file.type.match(imageType))
        return;
    var reader = new FileReader();
    reader.onload = fileOnload;
    reader.readAsDataURL(file);
});

// Brightness slider

var brightnessSlider=document.getElementById('brightnessSlider');
noUiSlider.create(brightnessSlider,{
    start: 0,
    step: 1,
    connect: 'lower',
    range: {
        min: -100,
        max: 100
    },
    format: wNumb({
        decimals: 0
    })
});
brightnessSlider.noUiSlider.on('update',function(values,handle){
    $('#brightness').html(values[handle]);
    Caman('#edit-canvas',function(){
        this.brightness(values[handle]).render();
    });
});

当我滑动时,我尝试登录控制台,值确实发生了变化,但画布没有任何效果。实际上,画布的一部分甚至消失了。我不知道为什么。我从 CamanJS 的指南中做错了什么吗?

4

1 回答 1

1

我已经发现了我的问题。这是因为我糟糕的异步处理。它应该是这样的:

var brightnessSlider=document.getElementById('brightnessSlider');
noUiSlider.create(brightnessSlider,{
    start: 0,
    step: 1,
    connect: 'lower',
    range: {
        min: -100,
        max: 100
    },
    format: wNumb({
        decimals: 0
    })
});

(function(){

    // Upload, resize image, draw canvas

    function fileOnload(e) {
        var img=new Image;
        img.src=e.target.result;
        var canvas=document.getElementById('edit-canvas');
        var context=canvas.getContext('2d');
        img.onload=function(){
            canvas.height=canvas.width * img.height / img.width;
            var oc = document.createElement('canvas'), octx = oc.getContext('2d');
            oc.width = img.width;
            oc.height = img.height;
            octx.drawImage(img, 0, 0);
            while (oc.width * 0.5 > canvas.width) {
                oc.width *= 0.5;
                oc.height *= 0.5;
                octx.drawImage(oc, 0, 0, oc.width, oc.height);
            }
            oc.width = canvas.width;
            oc.height = oc.width * img.height / img.width;
            context.drawImage(img, 0, 0, oc.width, oc.height);
            Caman('#edit-canvas',function(){
                this.render();
            });

            brightnessSlider.noUiSlider.on('change',function(values,handle){
                $('#brightness').html(values[handle]);
                Caman('#edit-canvas',function(){
                    this.revert(false);
                    this.brightness(values[handle]).render();
                });
            });
        };
    };

    $('#upload-image').change(function(e){
        var file = e.target.files[0];
        var imageType = /image.*/;
        if (!file.type.match(imageType))
            return;
        var reader = new FileReader();
        reader.onload = fileOnload;
        reader.readAsDataURL(file);
    });
})();
于 2016-09-26T10:00:22.810 回答