简体   繁体   English

JavaScript,如何使 fill() 成为变量(可以在自定义函数中使用)?

[英]JavaScript, how do I make fill() a variable (that I can use in a custom function)?

I'm new to JS and have not found an answer to this question (or any question like it).我是 JS 新手,还没有找到这个问题的答案(或任何类似的问题)。 I'm making a relatively simple program: create fish in tank and have them be of different lengths and colour.我正在制作一个相对简单的程序:在坦克中创建鱼,并让它们具有不同的长度和颜色。

I can make them different lengths through having a length variable in the parameters and editing the length argument within the function, but I do not think that same logic can be applied to custom adjustable colours in the same way*.我可以通过在参数中使用长度变量并在 function 中编辑长度参数来使它们具有不同的长度,但我认为相同的逻辑不能以相同的方式应用于自定义可调节颜色*。

So in essence, how would I structure my code so that I can create the command fill() , a variable such that it can be edited to make different colours from the function call?所以本质上,我将如何构造我的代码,以便我可以创建命令fill() ,一个变量,以便可以编辑它以从 function 调用中生成不同的颜色?

*EDIT: Apologies, I forgot to add code: *编辑:抱歉,我忘了添加代码:

background(147, 226, 250);

var colour = fill(158, 52, 158); //Failed attempt at custom fill()

var drawFish = function(posX, posY, x) {

  ellipse(posX, posY, 166, 31);
  triangle(posX + 81, posY, posX + 125, posY - 16, posX + 125, posY + 16);
  ellipse(posX - 60, posY - 1, 10, 10);

};
drawFish(90, 241);
drawFish(121, 174);
drawFish(175, 356);
drawFish(238, 300);
drawFish(263, 22);

ANSWERED.回答。 I couldn't make fill() a variable of course but instead I created variables that would generate random numbers from 0 to 256(exclusive) and placed them inside the fill() function.我当然不能让 fill() 成为变量,而是创建了可以生成 0 到 256(不包括)随机数的变量,并将它们放在 fill() 函数中。 This allowed me to generate different colours for my fish!这让我可以为我的鱼生成不同的颜色!

Thanks for the help.谢谢您的帮助。

Sample code:示例代码:

var drawFish = function (posX, posY, tailSize) {
    
    stroke(74, 74, 74);
    var randomNum = random(0,256);
    var randomNum1 = random(0,256);
    var randomNum2 = random(0, 256);
    
    fill(randomNum, randomNum1, randomNum2); 
    var bodySize = random(150, 200);
    ellipse (posX, posY, 166,31); //body
    
    
    fill(randomNum1, randomNum, randomNum2);
    triangle (posX + 81, posY, posX + 125/20*tailSize, posY - 16/20*tailSize, posX + 125/20*tailSize, posY+16/20*tailSize); //tail
    
    //...(Removed code)
  
};

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

相关问题 如何使此javascript函数不使用全局变量? - How can I make this javascript function not use a global variable? 如何在 JavaScript 中将函数用作变量? - How do I use a function as a variable in JavaScript? 如何使函数使用在另一个函数JavaScript / jQuery上声明的变量 - How do I make a function use a variable that was declared on another function JavaScript/jQuery 如何使此匿名Javascript函数引用正确的变量? - How do I make this anonymous Javascript function reference the right variable? 我如何在 Vanilla JavaScript class 中使用变量来制作基于 Z84E2C64F55AB78BA3ZEA5C2 的 function - How can i use variable inside Vanilla JavaScript class to make a function based on boolean 如何在 javascript 的另一个 function 中使用 function 的变量? - how can i use a variable of a function in another function in javascript? 我如何制作一个JavaScript变量并将其与jQuery一起使用 - How can I make a javascript variable and use it with jQuery 如何在Javascript中使对象属性既是函数又是变量? - How can I make an object property be both a function and a variable in Javascript? Javascript - 如何在函数中使用变量 - Javascript - how do I use a variable within a function 如何在javascript中使用自定义“类”(函数)作为数组数据类型? - How can I use custom “class”(function) as array datatype in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM