【问题标题】:Horizontally Animate Gradient in HTML5 Canvas using jCanvas & jQuery [closed]使用 jCanvas 和 jQuery 在 HTML5 Canvas 中水平动画渐变 [关闭]
【发布时间】:2014-12-03 03:14:55
【问题描述】:

我想让 HTML5 Canvas 元素内的文本的 填充渐变 水平动画 以创建 类似极光的效果动态更改其colorstops (从右到左) 的位置,我认为可以使用setTimeout() 方法完成,但我不知道该怎么做。

我目前在这里有一个静态脚本:

HTML:

<script src="https://raw.github.com/caleb531/jcanvas/master/jcanvas.min.js"></script>
<canvas></canvas>

Javascript:

var mw = $(document).width();
var mh = $(document).height();

var gs1 = 0
var gs2 = 1/5
var gs3 = 2/5
var gs4 = 3/5
var gs5 = 4/5
var gs6 = 1

$('canvas').attr('width', mw).attr('height', mh);

var Grad = $('canvas').createGradient({
  x1: 0, y1: 0,
  x2: mw, y2: 0,
  c1: '#E33A3A', s1: gs1,
  c2: '#FB57FB', s2: gs2,
  c3: '#21E5EB', s3: gs3,
  c4: '#1ECA18', s4: gs4,
  c5: '#EBD427', s5: gs5,
  c6: '#EB4127', s6: gs6
});

$('canvas').drawText({
  fillStyle: Grad,
  x: mw/2, y: mh/20,
  fontSize: mw/1.8 + '%',
  fromCenter: true,
  fontFamily: "Calibri, Ariel",
  text: 'WOW! Amazing Rainbow!'
})

请帮助我,并尝试使用jCanvas 和jQuery 语法。谢谢。

【问题讨论】:

    标签: jquery html canvas gradient jcanvas


    【解决方案1】:
    var i = setInterval( timer, 500 );
    function timer() {
        if(boo!=='change'){
            boo='change';
            a = '#E33A3A';
            b = '#FB57FB';
            c = '#21E5EB';
            d = '#1ECA18';
            e = '#EBD427';
            f = '#EB4127';
        } else {
            boo='other';
            a = '#EB4127';
            b = '#EBD427';
            c = '#1ECA18';
            d = '#21E5EB';
            e = '#FB57FB';
            f = '#E33A3A';
        }
        console.log( boo );
        dragTextAnimatable();
    }
    

    JSFIDDLE


    本地 JAVASCRIPT

    JSFIDDLE

    var gradient = context.createLinearGradient( 0,0,mw,0);
            for (var i=0; i < colorStops.length; i++) {
                var tempColorStop = colorStops[i];
                var tempColor = tempColorStop.color;
                var tempStopPercent = tempColorStop.stopPercent;
                gradient.addColorStop(tempStopPercent,tempColor);
                tempStopPercent += .015;
                if (tempStopPercent > 1) {
                    tempStopPercent = 0;
                }
                tempColorStop.stopPercent = tempStopPercent;;
                colorStops[i] = tempColorStop;
            }
    

    【讨论】:

    • 非常感谢,先生。但是有没有可能做出像这样一个横向平滑的动画效果:link?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-24
    • 2016-07-13
    相关资源
    最近更新 更多