【问题标题】:How to do jquery canvas gradient for jquery knob?如何为jquery旋钮做jquery画布渐变?
【发布时间】:2015-08-06 05:21:53
【问题描述】:

这是我尝试使用画布实现的设计:

我必须在圆的半径上获得渐变,并在边缘带有一些内部阴影。

JSFIDDLE

我对画布的了解非常少,因此非常感谢任何帮助。

我正在使用jquery旋钮插件显示进度条:JQUERY-KNOB

HTML

<div class="demo">
  <div style="width: 100%; margin-bottom: 20px; margin-left:100px;">
    <br>
    <br>
    <input class="knob" data-width="100%" value="45">
  </div>
</div>

JS

        $(function ($) {

        $(".knob").knob({
            class: 'group-canvas',
            change: function (value) {
                //console.log("change : " + value);
            },
            release: function (value) {
                //console.log(this.$.attr('value'));
                console.log("release : " + value);
            },
            cancel: function () {
                console.log("cancel : ", this);
            },
            /*format : function (value) {
                return value + '%';
            },*/
            draw: function () {

                // "tron" case
                if (this.$.data('skin') == 'tron') {

                    this.cursorExt = 0.3;

                    var a = this.arc(this.cv) // Arc
                    ,
                        pa // Previous arc
                        ,
                        r = 1;

                    this.g.lineWidth = this.lineWidth;

                    if (this.o.displayPrevious) {
                        pa = this.arc(this.v);
                        this.g.beginPath();
                        this.g.strokeStyle = this.pColor;
                        this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, pa.s, pa.e, pa.d);
                        this.g.stroke();
                    }

                    this.g.beginPath();
                    this.g.strokeStyle = r ? this.o.fgColor : this.fgColor;
                    this.g.arc(this.xy, this.xy, this.radius - this.lineWidth, a.s, a.e, a.d);
                    this.g.stroke();

                    this.g.lineWidth = 2;
                    this.g.beginPath();
                    this.g.strokeStyle = this.o.fgColor;
                    this.g.arc(this.xy, this.xy, this.radius - this.lineWidth + 1 + this.lineWidth * 2 / 3, 0, 2 * Math.PI, false);
                    this.g.stroke();

                    return false;
                }
            }
        });


        // Example of infinite knob, iPod click wheel
        var v, up = 0,
            down = 0,
            i = 0,
            $idir = $("div.idir"),
            $ival = $("div.ival"),
            incr = function () {
                i++;
                $idir.show().html("+").fadeOut();
                $ival.html(i);
            },
            decr = function () {
                i--;
                $idir.show().html("-").fadeOut();
                $ival.html(i);
            };

    });

CSS

.group-canvas {

width: 20vh !important;

height: 20vh !important;

}

.knob {

margin-top: 14vh !important;

width: 24vh !important;

height: 13vh !important;

font-size: 9vh !important;

margin-left: -32vh !important;

}

这就是我到目前为止通过上面的代码和更改http://anthonyterrien.com/js/jquery.knob.js 文件中的 fgColor 实现的目标(我知道我不应该编辑这个文件,但作为一个试验,我已经更改了 fgColor) :

fgColor: this.$.data('fgcolor') || '#dd3002',

【问题讨论】:

  • 我为您发布了一个很好的起点。这里是深夜,所以有点粗糙——但这是一个好的开始。晚安!
  • 谢谢你,非常感谢。我去看看。

标签: javascript jquery html css canvas


【解决方案1】:

这是一个起点: 这是一个带有 3D 阴影的渐变弧。

您可以像这样绘制渐变弧:

function drawGradientArc(cx,cy,r, startAngle,endAngle, startColor,endColor, strokewidth) {
    var sweep=endAngle-startAngle;       
    var xStart = cx + Math.cos(startAngle) * r;
    var xEnd = cx + Math.cos(startAngle + sweep) * r;
    var yStart = cy + Math.sin(startAngle) * r;
    var yEnd = cy + Math.sin(startAngle + sweep) * r;
    //
    var gradient = ctx.createLinearGradient(xStart, yStart, xEnd, yEnd);
    gradient.addColorStop(0, startColor);
    gradient.addColorStop(1.0, endColor);
    //
    ctx.beginPath();
    ctx.arc(cx, cy, r, startAngle, startAngle + sweep);
    ctx.lineWidth = strokewidth;
    ctx.strokeStyle = gradient;
    ctx.stroke();
    ctx.closePath();
}

您可以像这样使用 3D 阴影绘制弧线:

function drawShadow(cx,cy,r,strokewidth){
    ctx.save();
    ctx.strokeStyle='white';
    ctx.lineWidth=5;
    ctx.shadowColor='black';
    ctx.shadowBlur=15;
    //
    ctx.beginPath();
    ctx.arc(cx,cy,r-5,0,PI*2);
    ctx.clip();
    //
    ctx.beginPath();
    ctx.arc(cx,cy,r,0,PI*2);
    ctx.stroke();
    //
    ctx.beginPath();
    ctx.arc(cx,cy,r-strokewidth,0,PI*2);
    ctx.stroke();
    ctx.shadowColor='rgba(0,0,0,0)';
    //
    ctx.beginPath();
    ctx.arc(cx,cy,r-strokewidth,0,PI*2);
    ctx.fillStyle='white'
    ctx.fill();
    //
    ctx.restore();
}

示例代码和演示:

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

var PI=Math.PI;
var startColor='#DD3002';
var endColor='#FF9966';

drawGradientArc(150,150,93, -PI/4,Math.PI/4, startColor,endColor,43);

drawShadow(150,150,120,50);

ctx.textAlign='center';
ctx.textBaseline='middle';
ctx.font='40px verdana';
ctx.fillStyle=startColor;
ctx.fillText('25%',150,150);

function drawShadow(cx,cy,r,strokewidth){
  ctx.save();
  ctx.strokeStyle='white';
  ctx.lineWidth=5;
  ctx.shadowColor='black';
  ctx.shadowBlur=15;
  //
  ctx.beginPath();
  ctx.arc(cx,cy,r-5,0,PI*2);
  ctx.clip();
  //
  ctx.beginPath();
  ctx.arc(cx,cy,r,0,PI*2);
  ctx.stroke();
  //
  ctx.beginPath();
  ctx.arc(cx,cy,r-strokewidth,0,PI*2);
  ctx.stroke();
  ctx.shadowColor='rgba(0,0,0,0)';
  //
  ctx.beginPath();
  ctx.arc(cx,cy,r-strokewidth,0,PI*2);
  ctx.fillStyle='white'
  ctx.fill();
  //
  ctx.restore();
}

function drawGradientArc(cx,cy,r, startAngle,endAngle, startColor,endColor, strokewidth) {
  var sweep=endAngle-startAngle;       
  var xStart = cx + Math.cos(startAngle) * r;
  var xEnd = cx + Math.cos(startAngle + sweep) * r;
  var yStart = cy + Math.sin(startAngle) * r;
  var yEnd = cy + Math.sin(startAngle + sweep) * r;
  //
  var gradient = ctx.createLinearGradient(xStart, yStart, xEnd, yEnd);
  gradient.addColorStop(0, startColor);
  gradient.addColorStop(1.0, endColor);
  //
  ctx.beginPath();
  ctx.arc(cx, cy, r, startAngle, startAngle + sweep);
  ctx.lineWidth = strokewidth;
  ctx.strokeStyle = gradient;
  ctx.stroke();
  ctx.closePath();
}
body{ background-color: white; }
canvas{border:1px solid red; margin:0 auto; }
&lt;canvas id="canvas" width=300 height=300&gt;&lt;/canvas&gt;

【讨论】:

  • 马克,我希望使用 jquery 旋钮插件来完成。这必须在所有设备上完全响应,并且 jquery 旋钮很容易做到这一点。此外,拱门必须在圆圈中分成 8 个部分。每个渐变填充部分是 1 个拱形,所以我们应该有 8 个拱形。
  • 酷——很高兴我能帮上忙!你接下来的步骤是 (#1) 从 jQuery-Knob 中提取数据绑定、UI 和动画逻辑,并且 (#2) 使用我的渐变 3D 弧形代码作为样式。顺便说一句,在您将 #1 和 #2 组合到您想要的库中之后,将您的库变成一个查询插件真的很简单。就这么简单:$.fn.KnobByDexter=function(){...} 祝你的项目好运。 :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多