Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="mySocial">
  <a href="https://www.linkedin.com/in/rominamartinliberon/" target="_new" class="social">
    <i class="fab fa-linkedin"></i>
  </a>
  <a href="https://rominamartin.github.io/" target="_new" class="social">
    <i class="fab fa-github"></i>
  </a>
  <a href="https://twitter.com/rominamartinlib" target="_new" class="social">
    <i class="fab fa-twitter"></i>
  </a>
</div>

<h3 id="followers_total" class="followers_amount">NOT BAD:
  <span id="followers_amount"></span> FOLLOWERS!</h3>
<h3 id="no_followers" class="followers_amount">OH, YOU DON'T HAVE ANY FOLLOWERS :(</h3>

<div class="input_container">
  <h1>USERNAME</h1>
  <div class="user_container">
    <div class="social_icon">
      <span id="social_twitter" class="active">
        <i class="fab fa-twitter fa-lg"></i>
      </span>
      <span id="social_codepen">
        <i class="fab fa-codepen fa-lg"></i>
      </span>
    </div>
    <input id="user_input" type="text" value="@rominamartinlib">
    <span id="error_message">USER NOT VALID</span>
  </div>
  <button class="button active" id="search_button">SEARCH</button>
  <div id="loader"></div>
</div>

<div id="alert_container">
  <h1>WAIT WHAT</h1>
  <span>WHAT ARE YOU DOING HERE?</span>
  <span class="followers_amount">YOU'VE GOT
    <span id="followers_count"></span> FOLLOWERS!</span>
  <button class="button" id="hide_alert">STOP IT SILLY!</button>
</div>

<canvas></canvas>
<!-- Pen based on @Christopher Lis and following his tutorial at: https://www.youtube.com/watch?v=3b7FyIxWW94  -->
              
            
!

CSS

              
                body {
  margin: 0;
  overflow: hidden;
  background-color: #f7f5e8;
  font-family: "Open Sans Condensed", sans-serif;
}

.user_container {
  width: 180px;
  background: #fff;
  height: 20px;
  padding: 10px 5px;
  border-radius: 5px;
  margin-left: 35px;
  box-shadow: 0 1px 0 1px rgba(70, 70, 70, 0.2);
}

.user_container input {
  width: 120px;
  border: none;
  padding: 5px 0;
  text-align: center;
  color: #5c4b51;
  float: left;
  outline: none;
  font-size: 0.75rem;
}

.social_icon {
  color: rgba(100, 100, 100, 0.3);
  float: left;
  background: #fff;
  height: 100%;
}

.social_icon span {
  cursor: pointer;
}

.social_twitter.active,
#social_twitter.active {
  color: #00aced;
}
.social_codepen.active,
#social_codepen.active {
  color: rgba(0, 0, 0, 1);
}

.button {
  padding: 10px 20px;
  margin-top: 40px;
  background-color: #fff;
  border: none;
  color: #86868d;
  font-weight: bold;
  cursor: pointer;
  border-radius: 5px;
}

#search_button {
  display: none;
}

.button:hover {
  background: #ededed;
}

#error_message {
  margin-top: 10px;
  display: none;
  color: #ca1c24;
  font-weight: bolder;
}

canvas {
  position: relative;
  z-index: 0;
}

.input_container {
  position: absolute;
  left: calc(50% - 125px);
  top: 50px;
  background: #6ec4db;
  opacity: 0.6;
  color: #fff;
  text-align: center;
  border-radius: 50%;
  width: 250px;
  height: 250px;
  z-index: 2;
}

.input_container h1 {
  height: 60px;
  margin-top: 20px;
  line-height: 60px;
}

#alert_container {
  display: none;
  position: absolute;
  left: 40%;
  top: 50px;
  background: #5c4b51;
  color: #fff;
  text-align: center;
  width: 250px;
  height: 250px;
  z-index: 2;
  border-radius: 5px;
  z-index: 2;
}

.active {
  display: inline-block !important;
}

#alert_container h1 {
  color: #ffafc5;
  text-shadow: -1px 1px 1px #ededed;
}

.followers_amount {
  display: block;
  width: 100%;
  margin-top: 25px;
  color: #c9f0ff;
  font-weight: bold;
}

.mySocial {
  position: absolute;
  top: 25px;
  left: 25px;
}
.mySocial a {
  display: inline-block;
  height: 2.5em;
}
.mySocial a i {
  font-size: 1.5em;
}
.mySocial a i.fa-linkedin {
  color: #0073b0;
}
.mySocial a i.fa-github {
  color: #24292e;
}
.mySocial a i.fa-twitter {
  color: #1da1f2;
}

.followers_amount {
  display: none;
  position: absolute;
  top: 50px;
  left: 25px;
  color: rgba(70, 70, 70, 0.8);
}

#loader {
  display: none;
  border: 5px solid #fff;
  border-radius: 50%;
  border-top: 5px solid #86868d;
  width: 25px;
  height: 25px;
  animation: spin 1.5s linear infinite;
  margin-top: 40px;
}

@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}

              
            
!

JS

              
                // CONSTANTS
const GRAVITY = 0.3;
const FRICTION = 0.5;
const COLORS = ["#333", "#7ECEFD"];

const MAX_FOLLOWERS = 1500;

const MY_TWITTER_USER = "rominamartinlib";
const MY_CODEPEN_USER = "rominamartin";

// Data
const CODEPEN_BASE_URL = "https://cpv2api.com/profile/";
const TWITTER_BASE_URL =
  "https://cors-anywhere.herokuapp.com/https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=";

// I made this based on this cool tutorial, check it out!
// https://www.youtube.com/watch?v=3b7FyIxWW94

const canvas = document.querySelector("canvas");
const c = canvas.getContext("2d");
let initialized = false;
let social_network = "twitter";
let lastSearch = { network: null, user: null };

canvas.width = innerWidth;
canvas.height = innerHeight;

// Variables
let followersCount = null;

addEventListener("resize", function () {
  canvas.width = innerWidth;
  canvas.height = innerHeight;

  if (followersCount !== null) init();
});

// Utility Functions
randomIntFromRange = (min, max) =>
  Math.floor(Math.random() * (max - min + 1) + min);

randomColor = (colors) => colors[Math.floor(Math.random() * colors.length)];

toggleSocial = (network) => {
  let currentValue = document.getElementById("user_input").value;

  if (network === "twitter") {
    document.getElementById(`social_${network}`).classList.add("active");
    document.getElementById(`social_codepen`).classList.remove("active");

    if (currentValue[0] !== "@") {
      currentValue =
        currentValue === MY_CODEPEN_USER ? MY_TWITTER_USER : currentValue;
      document.getElementById("user_input").value = "@" + currentValue;
    }
  } else {
    document.getElementById(`social_${network}`).classList.add("active");
    document.getElementById(`social_twitter`).classList.remove("active");
    if (network === "codepen" && currentValue[0] === "@") {
      currentValue =
        currentValue === `@${MY_TWITTER_USER}` ? MY_CODEPEN_USER : currentValue;
      document.getElementById("user_input").value = currentValue.replace(
        "@",
        ""
      );
    }
  }
};

toggleElement = (element) => {
  document.getElementById(element).classList.toggle("active");
};

toggleLoader = () => {
  document.getElementById("loader").classList.toggle("active");
  document.getElementById("search_button").classList.toggle("active");
};

removeUserError = () => {
  document.getElementById("error_message").classList.remove("active");
  document.getElementById("followers_total").classList.remove("active");
  document.getElementById("no_followers").classList.remove("active");
};

// Objects
function Ball(x, y, dx, dy, radius, color) {
  this.x = x;
  this.y = y;
  this.dx = dx;
  this.dy = dy;
  this.radius = radius;
  this.color = color;

  this.update = () => {
    if (this.y + this.radius + this.dy > canvas.height) {
      this.dy = -this.dy;
      this.dy = this.dy * FRICTION;
      this.dx = this.dx * FRICTION;
    } else {
      this.dy += GRAVITY;
    }

    if (this.x + this.radius >= canvas.width || this.x - this.radius <= 0) {
      this.dx = -this.dx * FRICTION;
    }

    this.x += this.dx;
    this.y += this.dy;
    this.draw();
  };

  this.draw = () => {
    c.beginPath();
    c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
    c.fillStyle = this.color;
    c.fill();
    c.stroke();
    c.closePath();
  };
}

obtainData = () => {
  let currentValue = document.getElementById("user_input").value;

  if (currentValue.length < 2) {
    document.getElementById("user_input").value =
      social_network === "twitter" ? `@${MY_TWITTER_USER}` : MY_CODEPEN_USER;
    currentValue = document.getElementById("user_input").value;
  }

  if (
    currentValue !== lastSearch.user ||
    social_network !== lastSearch.network
  ) {
    lastSearch = { user: currentValue, network: social_network };

    toggleLoader();

    if (social_network === "codepen") getCodepenFollowers();
    else getTwitterFollowers();
  } else {
    init();
  }
};

document.getElementById("hide_alert").addEventListener("click", () => {
  toggleElement("alert_container");
});
document.getElementById("search_button").addEventListener("click", obtainData);
document.getElementById("user_input").addEventListener("keyup", (e) => {
  let currentValue = document.getElementById("user_input").value;
  if (social_network === "twitter") {
    document.getElementById("user_input").value =
      currentValue[0] !== "@" ? `@${currentValue}` : currentValue;
  }
  if (e.keyCode === 13) {
    obtainData();
  }
});

document.getElementById("user_input").addEventListener("focus", () => {
  if (
    document.getElementById("user_input").value === `@${MY_TWITTER_USER}` ||
    document.getElementById("user_input").value === MY_CODEPEN_USER
  )
    document.getElementById("user_input").value = "";
});

document.getElementById("social_twitter").addEventListener("click", () => {
  social_network = "twitter";
  toggleSocial("twitter");
});
document.getElementById("social_codepen").addEventListener("click", () => {
  social_network = "codepen";
  toggleSocial("codepen");
});

checkMaxFollowers = (value) => {
  if (value > MAX_FOLLOWERS) {
    followersCount = MAX_FOLLOWERS;
    document.getElementById("followers_count").innerText = value;
    toggleElement("alert_container");
  }
};

getCodepenFollowers = () => {
  let username = document.getElementById("user_input").value;
  username = username[0] === "@" ? username.replace("@", "") : username;

  fetch(`${CODEPEN_BASE_URL}${username}`)
    .then((data) => data.json())
    .then((res) => {
      followersCount = Number(res.data.followers.replace(",", ""));
      checkMaxFollowers(followersCount);
      toggleLoader();
      init();
    })
    .catch((err) => {
      toggleElement("error_message");
      toggleLoader();
    });
};

getTwitterFollowers = () => {
  let username = document.getElementById("user_input").value;
  username = username[0] === "@" ? username.replace("@", "") : username;

  fetch(`${TWITTER_BASE_URL}${username}`)
    .then((data) => data.json())
    .then((res) => {
      followersCount = Number(res[0].followers_count);
      checkMaxFollowers(followersCount);
      toggleLoader();
      init();
    })
    .catch((err) => {
      toggleLoader();
      toggleElement("error_message");
    });
};

var followersArray = [];

init = () => {
  removeUserError();
  followersArray = [];
  document.getElementById("no_followers").classList.remove("active");

  if (followersCount < MAX_FOLLOWERS && followersCount > 1) {
    document.getElementById("followers_amount").innerText = followersCount;
    document.getElementById("followers_total").classList.add("active");
  } else if (followersCount == 0) {
    document.getElementById("no_followers").classList.add("active");
  } else {
    document.getElementById("followers_total").classList.remove("active");
    document.getElementById("no_followers").classList.remove("active");
  }

  for (let i = 0; i < followersCount; i++) {
    var x = randomIntFromRange(radius, canvas.width - radius);
    var y = randomIntFromRange(0, canvas.height - radius);
    var dx = randomIntFromRange(-3, 3);
    var dy = randomIntFromRange(-2, 2);
    var radius = randomIntFromRange(10, 20);

    followersArray.push(new Ball(x, y, dx, dy, radius, randomColor(COLORS)));
  }
  if (!initialized) animate();
};

// Animation Loop
animate = () => {
  initialized = true;
  requestAnimationFrame(animate);

  c.clearRect(0, 0, canvas.width, canvas.height);

  followersArray.forEach((follower) => {
    follower.update();
  });
};

              
            
!
999px

Console