Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to iterative Leaf Creation #19

Open
atahrijouti opened this issue Feb 11, 2023 · 0 comments
Open

Switch to iterative Leaf Creation #19

atahrijouti opened this issue Feb 11, 2023 · 0 comments

Comments

@atahrijouti
Copy link
Owner

Old code

var colors = [
  ["#FEFED7", "#F63E62"],
  ["#FEFED7", "#75A63A"],
  ["#FEFED7", "#844A87"],
];
var randomColor = function () {
  var r = Math.floor(Math.random() * colors.length);
  return (
    ".leaf{background:" +
    colors[r][1] +
    "}" +
    "body{background:" +
    colors[r][0] +
    "}"
  );
};
var refreshStyle = function () {
  document.getElementById("custom-style").innerHTML = randomColor();
};

var base = document.querySelector(".base"),
  leaves = [base],
  children,
  currentLeaf = base,
  newLeaf,
  leavesIndex = 0;

var animation = function () {
  currentLeaf = leaves[Math.floor(Math.random() * leaves.length)];
  newLeaf = document.createElement("div");
  newLeaf.classList.add("leaf");
  currentLeaf.appendChild(newLeaf);
  leaves[++leavesIndex] = newLeaf;

  if (currentLeaf.children.length === 2) {
    currentLeaf.classList.add("full");
    leaves.splice(leaves.indexOf(currentLeaf), 1);
    leavesIndex--;
  }
};
refreshStyle();
var animationInterval = setInterval(animation, 450);
var styleInterval = setInterval(refreshStyle, 4000);
setTimeout(function () {
  clearInterval(animationInterval);
}, 20000);

    
@atahrijouti atahrijouti moved this to 🆕 New in tahri.dev Feb 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 New
Development

No branches or pull requests

1 participant