简体   繁体   English

使用javascript创建彩虹追逐

[英]Create a rainbow chase using javascript

I'm trying to create a rainbow cycle/chase effect on 9 pixels. 我正在尝试在9个像素上创建彩虹循环/追逐效果。

i need to prepare a json statement that updates all 9 pixels at once 我需要准备一个同时更新所有9个像素的json语句

So far i've been able to make all 9 pixels change colors, but not in a chase pattern 到目前为止,我已经能够使所有9个像素改变颜色,但是不能以追逐模式

here is my code: 这是我的代码:

  var colors = [
"248,12,18",
"238,17,0",    
"255,51,17",    
"255,68,34",
"255,102,68",
"255,153,51",   
"254,174,45",
"204,187,51",
"208,195,16",   
"170,204,34",
"105,208,37",
"34,204,170",   
"18,189,185",
"17,170,187",
"68,68,221",    
"51,17,187",
"59,12,189",
"68,34,153"
]
var i = 0
function f(array) {
 var howManyTimes = array.length;
 json = 'http://xxx.xxxxxxxxxxxxxx.xxx/xxxxx?
  hash='+globalHash+'&colours={"1": ['+array[i]+'], "0": 
  ['+array[i]+'], "3": ['+array[i]+'], "2": ['+array[i]+'], "5": 
  ['+array[i]+'], "4": ['+array[i]+'], "7": ['+array[i]+'], "6": 
  ['+array[i]+'], "9": ['+array[i]+'], "8": ['+array[i]+']}'
sendColorChange(json);
i++;
if (i == howManyTimes) { i = 0}
if( i < howManyTimes ){
    setTimeout(function(){f(array)}, 1000);
 }
}

so what i need is for the variable json to change every iteration, but instead of changing every pixel to the same color each iteration, i would like it to chase forward, nice little assci art here as an example: 因此,我需要变量json进行每次迭代更改,但我不想让每个像素每次迭代都更改为相同的颜色,而是希望它向前发展,这里有一些很好的小插图:

[]=pixel  
r=red  
g=green  
b=blue  
w=white  
however, the colors that i want to use are rgb values in the colors array  
iteration 1:  
[r][][][][][][][][][]  
iteration 2:  
[g][r][][][][][][][][]  
iteration 3:  
[b][g][r][][][][][][][]  
iteration 4:  
[w][b][g][r][][][][][][]  
and so on  

i hope that clears it up a little 我希望这可以清除一点

Old question, but would still like to answer. 旧问题,但仍想回答。

If you can use HSL values, you can simply set your S(aturation) and L(ight) to a value you like and increment your H(ue) value on a loop to create a rainbow chase effect. 如果您可以使用HSL值,则可以简单地将S(饱和度)和L(强度)设置为您喜欢的值,并在循环中增加H(ue)值以创建彩虹追逐效果。

I use this setup in a rainbow script for webpages I created, where it would look something like this: 我在彩虹脚本中为我创建的网页使用此设置,看起来像这样:

for(rainbowelements.length; let i = 0; i++) {
  rainbowelements[i].style.color = 'hsl(' + counter + ', 100%, 70%)';
  counter++
}

This means that every run the for loop makes, the counter will go up one, changing the hue slightly. 这意味着每次运行for循环时,计数器都会增加一个,并稍微改变色相。 As your question isn't the clearest to me, I'm afraid that's as far as I can help. 由于您的问题对我来说不是最清楚的,所以恐怕这是我所能提供的。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM