帆布 – jquery旋钮角度渐变

前端之家收集整理的这篇文章主要介绍了帆布 – jquery旋钮角度渐变前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法在jQuery knob插件添加角度渐变,以便它从一种颜色开始,沿着弧线,变成另一种颜色?

解决方法

我挖了一个互联网寻求解决方案,但没有人试过这个或发布过一个解决方案.最后我发布了Q& A.如果有人有更好的解决方案,请与我们分享.

在初始化期间,我重写了draw方法并检查了属性shaded =“true”.如果它在那里,则形成一个渐变,从白色开始向fgColor移动.要选择白色以外的起始颜色,请设置属性shadeColor =“#(颜色十六进制代码)”.

<input class="knob" value="95" autocomplete="off" data-readOnly=true data-fgColor="#FF0000" data-bgColor="transparent" shaded="1" shadeColor="#00FF00"/>
<script>
    $(function(){
        $('.knob').knob({
            draw : function () {
                var a = this.angle(this.cv)  // Angle,sa = this.startAngle          // PrevIoUs start angle,sat = this.startAngle         // Start angle,ea                            // PrevIoUs end angle,eat = sat + a                 // End angle,r = 1;

                this.g.lineWidth = this.lineWidth;

                if(this.$.attr('shaded')){
                    var color1 = r ? this.o.fgColor : this.fgColor;
                    var color2 = this.$.attr('shadeColor') ? this.$.attr('shadeColor') : '#ffffff';
                    var grad = getGradient(color2,color1);

                    var saDeg = parseInt((sa * 180 / Math.PI) % 360);
                    var eatDeg = parseInt((eat * 180 / Math.PI) % 360);

                    for(var angle = saDeg;(angle % 360) != eatDeg;angle++){
                        sat = angle * (Math.PI / 180);
                        eat = (angle + 2) * (Math.PI / 180);

                        if(grad.color2[0] != grad.color1[0] && (angle + 1) % grad.steps[0] == 0){
                            grad.color1[0] += grad.adder[0];
                        }
                        if(grad.color2[1] != grad.color1[1] && (angle + 1) % grad.steps[1] == 0){
                            grad.color1[1] += grad.adder[1];
                        }
                        if(grad.color2[2] != grad.color1[2] && (angle + 1) % grad.steps[2] == 0){
                            grad.color1[2] += grad.adder[2];
                        }

                        color = '#' + toHex(grad.color1[0]) + toHex(grad.color1[1]) + toHex(grad.color1[2]);

                        this.g.beginPath();
                        this.g.strokeStyle = color;
                        this.g.arc(this.xy,this.xy,this.radius,sat,eat,false);
                        this.g.stroke();
                    }
                } else {
                    this.g.beginPath();
                    this.g.strokeStyle = r ? this.o.fgColor : this.fgColor ;
                    this.g.arc(this.xy,false);
                    this.g.stroke();
                }

                return false;
            }
        });
    });

    function getGradient(color1,color2){
        var ret = new Object();

        ret.color1 = new Array();
        ret.color2 = new Array();
        ret.steps = new Array();
        ret.adder = new Array();

        color1 = color1.replace('#','');
        ret.color1[0] = parseInt(color1.slice(0,2),16),ret.color1[1] = parseInt(color1.slice(2,4),ret.color1[2] = parseInt(color1.slice(4,6),16);

        color2 = color2.replace('#','');
        ret.color2[0] = parseInt(color2.slice(0,ret.color2[1] = parseInt(color2.slice(2,ret.color2[2] = parseInt(color2.slice(4,16);

        ret.steps[0] = (ret.color1[0] == ret.color2[0])? 0 : parseInt(360 / Math.abs(ret.color1[0] - ret.color2[0])),ret.steps[1] = (ret.color1[1] == ret.color2[1])? 0 : parseInt(360 / Math.abs(ret.color1[1] - ret.color2[1])),ret.steps[2] = (ret.color1[2] == ret.color2[2])? 0 : parseInt(360 / Math.abs(ret.color1[2] - ret.color2[2])),ret.adder[0] = (ret.color1[0] > ret.color2[0])? -1 : 1;
        ret.adder[1] = (ret.color1[1] > ret.color2[1])? -1 : 1;
        ret.adder[2] = (ret.color1[2] > ret.color2[2])? -1 : 1;

        return ret;
    }

    function toHex(number){
        number = number.toString(16);
        if(number.length < 2){
            number = '0' + number;
        }
        return number;
    }
</script>

它为每个度数绘制一个单独的弧(弧度角为2度,而不是为了平滑度).弧的颜色经过从fgColor到shadeColor的过渡.

混色效果就像油漆混合而不是混光,所以如果你从绿色开始走向红色,你就不会在中心得到黄色.它具有混光效果,看起来很酷,但不知道该怎么做.它也不是一个优化的代码,它只是一个解决方案.巨大的改进空间..

原文链接:https://www.f2er.com/jquery/241428.html

猜你在找的jQuery相关文章