Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

186
Vistas
¿Se puede desempaquetar (difundir) una expresión como un parámetro en javascript?

Estoy tratando de calcular ciertas posiciones y tamaños de rectángulos en la cuadrícula, y me gustaría aleatorizarlos por cada mosaico.

Se puede hacer algo como esto:

 for (let x = 0; x < p.width; x += tiles) { for (let y = 0; y < p.height; y += tiles) { let subItemPerTile = p.random(itemsPerTile); // This should contain random X/Y options using -i, +i, *i in a list let coordinatesPerTile = [['x', 'y'], ['x-i', 'y-i'], ['x+i', 'y-i']]; for (let i = tilesInGrid; i > 0; i--) { p.fill(p.color(p.random(255))); let s = (tiles * i) / subItemPerTile; // This is working method where I'd like to substitute randomly, x,y,s from the list above // p.square(x, y, (tiles * i) / subItemPerTile); //This was desired way, and I had 's' as the part of the list, but removed it until I figure out if this can be done this way //p.square.apply(null, p.random(coordinatesPerTile); //p.square(...coordinatesPerTile); // This is me debugging on second item in the list let coords = coordinatesPerTile[1]; x = eval(coords[0]); y = eval(coords[1]); console.log(coords[0]); // let s = eval(coords[2]); // console.log(s); // Below works for coordinatesPerTile[0], but not for coordinatesPerTile[1], I don't understand why p.square(x, y, s); }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

No hay una conversión implícita de una cadena que incluye javascript al resultado de evaluar ese javascript en el ámbito local, y cualquier mecanismo para hacerlo sería una idea extremadamente mala. Esto es probablemente lo que quieres:

 function sketch(p) { const tileSize = 20; const itemsPerTile = [1, 2, 3, 4]; p.setup = function() { p.createCanvas(300, 300); p.frameRate(1); }; p.draw = function() { for (let x = 0; x < p.width; x += tileSize) { for (let y = 0; y < p.height; y += tileSize) { let subItemPerTile = p.random(itemsPerTile); for (let i = subItemPerTile; i > 0; i--) { // By simply declaring this inside the for loop that // defines i, the code is quite simple let coordinatesPerTile = [ [x, y], [x - i, y - i], [x + i, y - i] ]; p.fill(p.color(p.random(255))); let s = (tileSize * i) / subItemPerTile; p.square(...p.random(coordinatesPerTile), s); } } } }; } new p5(sketch);
 <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.0/p5.js"></script>

Debido a que su código estaba incompleto, hubo muchas conjeturas para hacer que el boceto anterior hiciera algo útil.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda