1
20
2009
2

[JavaScript]颜色渐变代码

 

//颜色渐变方法
function fadeColor( from, to, callback, duration, totalFrames) {
        //用一个函数来包裹setTimeout,根据帧数来确定延时
        function doTimeout(color,frame) {
                setTimeout(function() {
                        try {
                                callback(color);
                        } catch(e) {
                                JSLog.write(e);
                        }
                }, (duration*1000/totalFrames)*frame);
                //总持续秒数/每秒帧数*当前帧数=延时(秒),再乘以1000作为延时(毫秒)
        }
        // 整个渐变过程的持续时间,默认为1秒
        var duration = duration || 1;
        // 总帧数,默认为持续秒数*15帧,也即每秒15帧
        var totalFrames = totalFrames || duration*15;
        var r,g,b;
        var frame = 1;
        //在第0帧设置起始颜色
        doTimeout('rgb(' + from.r + ',' + from.g + ',' + from.b + ')',0);
        //计算每次变化所需要改变的rgb值
        while (frame < totalFrames+1) {
                r = Math.ceil(from.r * ((totalFrames-frame)/totalFrames)
                + to.r * (frame/totalFrames));
                g = Math.ceil(from.g * ((totalFrames-frame)/totalFrames)
                + to.g * (frame/totalFrames));
                b = Math.ceil(from.b * ((totalFrames-frame)/totalFrames)
                + to.b * (frame/totalFrames));
                // 调用本frame的doTimeout
                doTimeout('rgb(' + r + ',' + g + ',' + b + ')',frame);
                frame++;
        }
}

//使用方法
fadeColor(
        {r:255,g:255,b:134}, //star color
        {r:255,g:255,b:255}, //end color
                function(color) {
                        document.getElementById("left").style.backgroundColor = color;
                },
        1
);
Category: 未分类 | Tags: javascript 颜色渐变 | Read Count: 1861
Avatar_small
romantic video statu 说:
2019年5月20日 18:49

romantic video status for WhatsApp you can easily download from our website just click to see our all love status for WhatsApp.

Avatar_small
romantic video statu 说:
2019年5月30日 22:49

romantic video status for WhatsApp you can easily download from our website just click to see our all love status for WhatsApp.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com