在原始图像上应用camanJS过滤器

Mar*_* C. 2 javascript jquery image-manipulation html5-canvas camanjs

如果我使用camanJS在我的图像上应用滤镜,一切都很好但是当我点击第二个滤镜时,它需要返回到原始图像并将其应用于该图像,但是目前它将第二个滤镜放在仍然保留的图像上有第一个过滤器.

这是我的HTML部分:

<table>
        <tr>
            <td><div id="photo"><canvas id="photoCanvas" width="500" height="500">Uw browser ondersteund geen canvas</canvas></div></td>
            <td><div id="filterContainer">
                    <h4>Please select your photo effect.</h4>
                    <ul id="filters">
                        <li> <a href="#" id="normal">Normal</a> </li>
                        <li> <a href="#" id="vintage">Vintage</a> </li>
                        <li> <a href="#" id="lomo">Lomo</a> </li>
                        <li> <a href="#" id="clarity">Clarity</a> </li>
                        <li> <a href="#" id="sinCity">Sin City</a> </li>
                        <li> <a href="#" id="sunrise">Sunrise</a> </li>
                        <li> <a href="#" id="pinhole">Pinhole</a> </li>
                    </ul>
                </div></td>
        </tr>
    </table>
Run Code Online (Sandbox Code Playgroud)

我创建了一个id为photoCanvas的画布,用于显示图像,我有一个列表,其中包含不同的过滤器,如果点击则会触发以下javascript部分:

$(function() {
Caman("#photoCanvas", "./images/183411_1871156496150_3955828_n.jpg", function () {
    this.render();
});

var filters = $('#filters li a');
//    originalCanvas = $('#canvas'),
//    photo = $('#photo');   

filters.click(function(e){

    e.preventDefault();

    var f = $(this);

    if(f.is('.active')){
        // Apply filters only once
        return false;
    }

    filters.removeClass('active');
    f.addClass('active');
    // Listen for clicks on the filters
    var effect = $.trim(f[0].id);

    Caman("#photoCanvas", "images/183411_1871156496150_3955828_n.jpg", function () {
        // If such an effect exists, use it:
        if( effect in this){
            this[effect]();
            this.render();
        }
    });
});
});
Run Code Online (Sandbox Code Playgroud)

我试图告诉程序它需要使用特定的图像来应用过滤器,但它会不断堆叠过滤器.

如何解决这个问题,以便它不会叠加所有过滤器,而是将图像重置为原始图像,然后应用新过滤器?

小智 12

你能做到的只是以下几点:

更改代码的这一部分(实际上你应该只添加一行):

你的代码:

Caman("#photoCanvas", "images/183411_1871156496150_3955828_n.jpg", function () {
    // If such an effect exists, use it:
    if( effect in this){
        this[effect]();
        this.render();
    }
});
Run Code Online (Sandbox Code Playgroud)

你如何改变它:

Caman("#photoCanvas", "images/183411_1871156496150_3955828_n.jpg", function () {
    // If such an effect exists, use it:
    if( effect in this){
        //NOTE THIS IS THE LINE THAT I ADD FOR YOU:
        this.revert();
        this[effect]();
        this.render();
    }
});
Run Code Online (Sandbox Code Playgroud)

希望现在它很酷:))

请告诉我:)

最好的迪努兹

  • 请注意,如果你想懒得这样做,你需要将`false`传递给`this.revert()`(所以`this.revert(false)`).这样用户就不会看到重置图像的闪光,因为CamanJS只会在内部恢复而不直接将其绘制到`<canvas>`. (3认同)
  • @LuisNell:请注意,revert函数的`updateContext`参数仅适用于Bleeding Egde包.目前的稳定版本4.1.1缺少此选项! (2认同)