0

控制台向我确认 opacity 正确地传播了 0 和 1 之间的值。我搜索了几个小时如何将此变量用于函数 draw() 以更改矩形的透明度值。我将不胜感激任何指导。

$(document).ready( function() {         
        $('.demo').each( function() {
         $(this).minicolors({
        opacity: true,                  
        change: function(hex, opacity) {                        
         console.log(hex + ' - ' + opacity);

                      draw();                       

                   },

                theme: 'bootstrap'
            });                
        });

    });

// 我需要将 opacity var 传递给 opacityVar(?)

function draw() {

     ctx.save();        
     ctx.beginPath();
     ctx.rect(0, 10, 200, 320); 
     ctx.fillStyle = 'rgba(77,225,77, MyOpacity'; 

      ctx.fill();
}
4

1 回答 1

1

你可以像这样通过它

//draw accepts opacity as a parameter
function draw(opacity) {
    ctx.save();
    ctx.beginPath();
    ctx.rect(0, 10, 200, 320);
    ctx.fillStyle = 'rgba(77,225,77, ' + opacity + ')';
    ctx.fill();
}

$(document).ready(function () {
    $('.demo').each(function () {
        $(this).minicolors({
            opacity: true,
            change: function (hex, opacity) {
                console.log(hex + ' - ' + opacity);
                //pass opacity as the argument
                draw(opacity);
            },
            theme: 'bootstrap'
        });
    });
});
于 2013-11-02T02:51:48.553 回答