gpt4 book ai didi

javascript - 将数组转换为 context.fillColor 的字符串?

转载 作者:行者123 更新时间:2023-11-30 17:07:55 24 4
gpt4 key购买 nike

我想在 Canvas 上创建随机粒子。但是,我无法为每个粒子提供随机确定的 RGB 颜色值,它不会读取我的 fillStyle。

有人可以帮忙吗?

编辑:JSBIN 示例 http://jsbin.com/puworu/4/edit?js,console,output#J:L65

  var rgb = [];
rgb.push(Math.round(Math.random()*255));
rgb.push(Math.round(Math.random()*255));
rgb.push(Math.round(Math.random()*255));

context.fillStyle = '"rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')"';

最佳答案

这只是一个错字。 “”不应该在那里。应该是:

//context.fillStyle = '"rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')"';
context.fillStyle = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';

编码愉快!

编辑:我查看了您的 jsbin,它始终会抛出大量错误。

原因是:当您第一次从 init() 调用 draw(particles) 时,您从 init 传递了粒子数组。

但是,在 setIntervalID = setInterval(function(){draw()}) 中没有传递参数。一种解决方案是清除间隔 clearInterval(setIntervalID) 或将新数组传递给 draw()

关于javascript - 将数组转换为 context.fillColor 的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27581845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com