【问题标题】:Canvas multiply hover effect画布乘以悬停效果
【发布时间】:2013-02-15 23:03:07
【问题描述】:

我正在尝试在图像上制作悬停乘法效果。我正在关注本教程:

演示: http://albertogasparin.it/demo/multiply-filter/

http://albertogasparin.it/articles/2011/05/html5-multiply-filter-canvas/

我的问题是我不知道如何传递给函数,所以它只在“id="multiply_hover" 上悬停时工作,因为现在乘法过滤器在页面加载后显示。

这是我的标记:

<div class="item">
    <img id="multiply_hover" src="img/coleccionesII_1.jpg" alt="coleccionesII_1" width="195" height="343">
    <div class="item_info">
        <div class="item_text">
            <a href="javascript:void(0)">SEDA BLOUSE BOLSILLOS</a>
            <a href="javascript:void(0)">CREP PANTALÓN</a>
            <a href="javascript:void(0)">Zapato Rojo</a>
        </div>
    </div>
</div>

使用此脚本:

var multiplyFilter = (function() {
    //** private vars **//
    var multiplyColor,
        imageBottom, imageId,
        canvas;

    //** private functions **//
    function createCanvas() {
    canvas = document.createElement('canvas');
    canvas.width = imageBottom.width;
    canvas.height = imageBottom.height;
    imageBottom.parentNode.insertBefore(canvas, imageBottom);
    // no canvas support?
    if (!canvas.getContext) { return; }

    draw();
    }

    function draw() {
    var context, imgData, pix,
        w = imageBottom.width,
        h = imageBottom.height;
    // get 2d context
    context = canvas.getContext('2d');
    // draw the image on the canvas
    context.drawImage(imageBottom, 0, 0);
    // Get the CanvasPixelArray from the given coordinates and dimensions.
    imgData = context.getImageData(0, 0, w, h);
    pix = imgData.data;
    // Loop over each pixel and change the color.
    for (var i = 0, n = pix.length; i < n; i += 4) {
        pix[i ] = multiplyPixels(multiplyColor[0], pix[i ]); // red
        pix[i+1] = multiplyPixels(multiplyColor[1], pix[i+1]); // green
        pix[i+2] = multiplyPixels(multiplyColor[2], pix[i+2]); // blue
        // pix[i+3] is alpha channel (ignored)

        // another check to see if image is still empty
        if(i < 5 && !pix[i] && !pix[i+1] && !pix[i+2] && !pix[i+3]) {
        canvas.parentNode.removeChild(canvas);
        setTimeout(function() { multiplyFilter.init(imageId, multiplyColor); }, 500);
        return false;
        }
    }
    // Draw the result on the canvas
    context.putImageData(imgData, 0, 0);
    }

    //** helper function **//
    function multiplyPixels(topValue, bottomValue) {
    // the multiply formula
    return topValue * bottomValue / 255;
    }       


    //** public functions **//
    return {
    init : function(imgId, color) {
        imageId = imgId;
        imageBottom = document.getElementById(imageId);
        multiplyColor = color;

        // lauch the draw function as soon as the image is loaded
        if(imageBottom && imageBottom.clientWidth > 50) { // image loaded
        createCanvas();
        } else { // not yet ready
        setTimeout(function() { multiplyFilter.init(imageId, color); }, 1000);
        }
    }
    }
})();


multiplyFilter.init('multiply_hover', [0, 0, 210]);

我尝试使用类似这样的东西,它在悬停时有效,但效果不佳,因为它在每次悬停时都会创建一个新的画布元素:

    // Hover effect
    $(".item").bind('mouseenter', function() {
        $(this).children(".item_info").fadeIn();
        multiplyFilter.init('multiply_hover', [0, 0, 210]);
    });

    $(".item").bind('mouseleave', function() {
        $(this).children(".item_info").fadeOut();
    });

关于如何在悬停时正确传递函数的任何想法?

【问题讨论】:

    标签: javascript jquery html css


    【解决方案1】:

    试试这个代码:

    <!DOCTYPE html>
    <html>
    <body>
        <div class="item">
            <img id="multiply_hover" src="img/coleccionesII_1.jpg" alt="coleccionesII_1">
            <div class="item_info">
                <div class="item_text">
                    <a href="javascript:void(0)">SEDA BLOUSE BOLSILLOS</a>
                    <a href="javascript:void(0)">CREP PANTALÓN</a>
                    <a href="javascript:void(0)">Zapato Rojo</a>
                </div>
            </div>
        </div>
    
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    
        <script>
    
        var multiplyFilter = (function() {
            //** private vars **//
            var multiplyColor,
                imageBottom, imageId,
                canvas;
    
            //** private functions **//
            function createCanvas() {
            canvas = document.createElement('canvas');
            canvas.width = imageBottom.width;
            canvas.height = imageBottom.height;
            imageBottom.parentNode.insertBefore(canvas, imageBottom);
            // no canvas support?
            if (!canvas.getContext) { return; }
    
            draw();
            }
    
            function draw() {
            var context, imgData, pix,
                w = imageBottom.width,
                h = imageBottom.height;
            // get 2d context
            context = canvas.getContext('2d');
            // draw the image on the canvas
            context.drawImage(imageBottom, 0, 0);
            // Get the CanvasPixelArray from the given coordinates and dimensions.
            imgData = context.getImageData(0, 0, w, h);
            pix = imgData.data;
            // Loop over each pixel and change the color.
            for (var i = 0, n = pix.length; i < n; i += 4) {
                pix[i ] = multiplyPixels(multiplyColor[0], pix[i ]); // red
                pix[i+1] = multiplyPixels(multiplyColor[1], pix[i+1]); // green
                pix[i+2] = multiplyPixels(multiplyColor[2], pix[i+2]); // blue
                // pix[i+3] is alpha channel (ignored)
    
                // another check to see if image is still empty
                if(i < 5 && !pix[i] && !pix[i+1] && !pix[i+2] && !pix[i+3]) {
                canvas.parentNode.removeChild(canvas);
                setTimeout(function() { multiplyFilter.init(imageId, multiplyColor); }, 500);
                return false;
                }
            }
            // Draw the result on the canvas
            context.putImageData(imgData, 0, 0);
            }
    
            //** helper function **//
            function multiplyPixels(topValue, bottomValue) {
            // the multiply formula
            return topValue * bottomValue / 255;
            }       
    
    
            //** public functions **//
            return {
            init : function(imgId, color) {
                imageId = imgId;
                imageBottom = document.getElementById(imageId);
                multiplyColor = color;
    
                // lauch the draw function as soon as the image is loaded
                if(imageBottom && imageBottom.clientWidth > 50) { // image loaded
                createCanvas();
                } else { // not yet ready
                setTimeout(function() { multiplyFilter.init(imageId, color); }, 1000);
                }
    
                //Hide the original image
                $('#'+imgId).hide();
            }
            }
        })();
    
        // Hover effect
            $(".item").bind('mouseenter', function() {
                $(this).children(".item_info").fadeIn();
                multiplyFilter.init('multiply_hover', [0, 0, 210]);
            });
    
            $(".item").bind('mouseleave', function() {
                $(this).children(".item_info").fadeOut();
                $('canvas').hide(); //hide the canvas
                $('#multiply_hover').show(); //show the original image
            });
    
        </script>
    
    
    </body>
    </html>
    

    我修改了乘法脚本使其隐藏了原始图像,以及悬停效果使其隐藏了画布并显示了原始图像

    【讨论】:

      【解决方案2】:

      你试试这个http://api.jquery.com/hover/ 吗?

      您可以使用它向 $("#multiply_hover") 添加一个函数。 Hover Jquery 有处理程序 IN 和 OUT

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-14
        • 2012-02-04
        • 2015-02-20
        相关资源
        最近更新 更多