最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - JavaScript Canvas - change smoothly rgb - Stack Overflow

programmeradmin5浏览0评论

I'm trying to change color of a rectangle from 'yellow' to 'blue'. I use for it fillStyle with rgb.

var 
    canvas = document.getElementById('mycanvas'),
    ctx = document.getElementById('mycanvas').getContext('2d'),
    red = 255,
    blue = 0,
    green = 179,
    inc = 2;

function animate(){
    ctx.clearRect(0,0,canvas.width,canvas.height);
    ctx.beginPath();
    ctx.rect(100, 100, 100, 100);
    ctx.fillStyle = 'rgb('+ red +','+ green +','+ blue +')';
    ctx.fill();    
    ctx.closePath();
    red = Math.min(Math.max(red - inc, 36))
    green = Math.max(green, Math.min(182, green + inc));
    blue = Math.max(blue, Math.min(255, blue + inc));

    requestAnimationFrame(animate);
}

animate();

The idea was to change valuse of rgb by adding or subtracting variable named inc (equal 2). The problem is that before rectangle reach blue, he gets also other colors (like green for example). This is because the green color will increase by only 3 units, but rest of colors still needs time to get their new values. Example:

/

Is there any way to make 'green' reach its new value at the same time as the 'red' and 'blue'? How to do that?

I'm trying to change color of a rectangle from 'yellow' to 'blue'. I use for it fillStyle with rgb.

var 
    canvas = document.getElementById('mycanvas'),
    ctx = document.getElementById('mycanvas').getContext('2d'),
    red = 255,
    blue = 0,
    green = 179,
    inc = 2;

function animate(){
    ctx.clearRect(0,0,canvas.width,canvas.height);
    ctx.beginPath();
    ctx.rect(100, 100, 100, 100);
    ctx.fillStyle = 'rgb('+ red +','+ green +','+ blue +')';
    ctx.fill();    
    ctx.closePath();
    red = Math.min(Math.max(red - inc, 36))
    green = Math.max(green, Math.min(182, green + inc));
    blue = Math.max(blue, Math.min(255, blue + inc));

    requestAnimationFrame(animate);
}

animate();

The idea was to change valuse of rgb by adding or subtracting variable named inc (equal 2). The problem is that before rectangle reach blue, he gets also other colors (like green for example). This is because the green color will increase by only 3 units, but rest of colors still needs time to get their new values. Example:

http://jsfiddle/f2Za8/

Is there any way to make 'green' reach its new value at the same time as the 'red' and 'blue'? How to do that?

Share Improve this question asked Mar 29, 2014 at 17:34 AmayAmay 1,5215 gold badges29 silver badges57 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Does this do what you want?

var debug = document.querySelector("#debug"),
    canvas = document.querySelector('#mycanvas'),
    ctx = canvas.getContext('2d'),
    red = 255,
    blue = 0,
    green = 179,
    tm = Date.now(),
    inc = 2;
    
function lerp(a, b, n) {
    return (b - a) * n + a;
}
    
function animate() {
    var n = (Date.now() - tm) * 0.001;
    n = Math.max(0, Math.min(1, n));
    
    red = lerp(255, 36, n) | 0;
    green = lerp(179, 182, n) | 0;
    blue = lerp(0, 255, n) | 0;
    
    debug.innerText = `${red} ${green} ${blue}`;
    
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    ctx.beginPath();
    ctx.rect(100, 100, 100, 100);
    ctx.fillStyle = `rgb(${red}, ${green}, ${blue})`;
    ctx.fill();    
    ctx.closePath();
    
    window.requestAnimationFrame(animate);
}
    
animate();
<p id="debug"></p>
<canvas id="mycanvas" width="600" height="400"></canvas>

I would use a this function to be flexible about which colors you want to fade into which.

function blendColors(r1,g1,b1,r2,g2,b2,balance) {
    var bal = Math.min(Math.max(balance,0),1);
    var nbal = 1-bal;
    return {
            r : Math.floor(r1*nbal + r2*bal),
            g : Math.floor(g1*nbal + g2*bal),
            b : Math.floor(b1*nbal + b2*bal)
           };
} 

balance must be between 0 and 1. So can could increase a variable within the animation loop and call blendColors() with this variable as balance argument.

The function returns an object with r,g and b properties. Usage:

var blendedColor = blendColors(0,0,255,255,255,0,0.5);
// blendedColor.r contains 127;
// blendedColor.g contains 127;
// blendedColor.b contains 127;

I have adapted your example here: http://jsfiddle/f2Za8/7/

发布评论

评论列表(0)

  1. 暂无评论