Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

133
Visualizações
How do I create an array of colour schemes and then randomly select one with JavaScript

I'm trying to load a colour scheme, from a selection of pre-defined colour schemes, at random each time the page is loaded. Hopefully my attempt at coding it is self-explanatory, but my approach has been to create an array called 'colourSchemes', populate it with 3 colour schemes (with key value pairs defining the 3 colours that make up the scheme) and then store the selected colour scheme in a variable.

I've then attempted to assign a colour from the selected scheme to the fillStyle of an object. Unfortunately I'm drawing a blank (literally). Any help would be appreciated. Snippet:

//define colour schemes
var colourSchemes = [];
colourSchemes.colourScheme_1 = {colour_1 : "#84FFC9", colour_2 : "#AAB2FF", colour_3 : "#ECA0FF"};
colourSchemes.colourScheme_2 = {colour_1 : "#540D6E", colour_2 : "#EE4266", colour_3 : "#FFD23F"};
colourSchemes.colourScheme_3 = {colour_1 : "#FFBA49", colour_2 : "#20A39E", colour_3 : "#EF5B5B"};

//random colour scheme selected on load
var active_colourScheme = colourSchemes[Math.floor(Math.random() * colourSchemes.length)];

//background colour
ctx.fillStyle = active_colourScheme.colour_1;
ctx.fillRect(0, 0, canvasW, canvasH);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That is now how you populate an Array with values. In your case you can just put the values in the array during the array declaration:

const colourScheme_1 = {colour_1 : "#84FFC9", colour_2 : "#AAB2FF", colour_3 : "#ECA0FF"};
const colourScheme_2 = {colour_1 : "#540D6E", colour_2 : "#EE4266", colour_3 : "#FFD23F"};
const colourScheme_3 = {colour_1 : "#FFBA49", colour_2 : "#20A39E", colour_3 : "#EF5B5B"};
const colourSchemes = [colourScheme_1, colourScheme_2, colourScheme_3];

//random colour scheme selected on load
const active_colourScheme = colourSchemes[Math.floor(Math.random() * colourSchemes.length)];
console.log(active_colourScheme);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Or alternatively push() the values into the array. This works if you want to add values to the array later on after its declaration.

const colourSchemes = [];
const colourScheme_1 = {colour_1 : "#84FFC9", colour_2 : "#AAB2FF", colour_3 : "#ECA0FF"};
const colourScheme_2 = {colour_1 : "#540D6E", colour_2 : "#EE4266", colour_3 : "#FFD23F"};
const colourScheme_3 = {colour_1 : "#FFBA49", colour_2 : "#20A39E", colour_3 : "#EF5B5B"};
colourSchemes.push(colourScheme_1, colourScheme_2, colourScheme_3)

//random colour scheme selected on load
const active_colourScheme = colourSchemes[Math.floor(Math.random() * colourSchemes.length)];
console.log(active_colourScheme);
.as-console-wrapper { max-height: 100% !important; top: 0; }

I would recommend you read the article on Array which I have linked here (and above), so you see how you can use arrays properly in JavaScript.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda