#CodepenChallenge 100 Dots: Follow the Mouse Pointer
Tags: codepen, html, css, javascript
Published: 2019-08-20 13:04:50

<p>These dots are following your mouse pointer.</p>

<p>Supposed to be, I wanted it to be a little bit creepy. Just like many eyes observing your every movement. But when I am doing it, I found that the movement is so cute, like confetti, so I made it colorful.</p>

<p>Instead of using the text dot (•), I changed it to a span that has border-radius to 50%. It is easier to manipulate after all.</p>

<p>I did not use any libraries, nor pre-processors in this one.</p>

---

# How I did it.

<p>First of all, you have to go back to high school, and remember your trigonometry class. I will not cover-up the HTML and CSS codes, because I only used a simple border-radius, which I am sure, everyone can use comfortably.</p>

### Define the constants

<p>First of all, we have to get the x and y location of the container of each circles.</p>

```javascript
// The circle
const p = i.querySelector('span');
// Getting the position of container of the circle
const q = i.getBoundingClientRect();
// 'x' position of container
const x1 = q.left;
// 'y' position of container
const y1 = q.top;
```

<p>Then, we have to get each of the sizes of the circles and its container, to measure the boundary.</p>

```javascript
// Width of container
const iw = i.offsetWidth / 2;
// Height of container
const ih = i.offsetHeight / 2;
// Width of circle
const pw = p.offsetWidth / 2;
// Height of circle
const ph = p.offsetHeight / 2;
```
<p>Finally, the position of the mouse</p>

```javascript
'x' position of the mouse
const x2 = e.clientX;
'y' position of the mouse
const y2 = e.clientY;
```

### Compute the resources

<p>Now, we have defined all the needed constants for the movement, let us do a little computation for the movement.</p>

<p>In order to have out circles follow the position of the mouse, we need to get the angle from the center position up to the position of the mouse. We have to use the Pythagorean Theorem for that.</p>

<p>Luckily, because the Javascript will do the most of the job, we just have to subtract the 'x' position of the center of the circle, and the 'x' position of the position of the mouse. Also, the 'y' position of the center of the circle, and the 'y' position of the position of the mouse.</p>

<p>And then, use the arc tangent squared function that was already provided by Javascript.</p>

<p>By the law, cos() is for the 'x' axis, and then sin() is for the 'y' axis (Yeah, I think it works like that).</p>

```javascript
let px = x2 - x1;
let py = y2 - y1;
let t  = Math.atan2(py, px);
let ax = iw + (pw * Math.cos(t));
let ay = ih + (ph * Math.sin(t));
```

<p>It is weird if the circle come out from the boundary, right? So we have to do the collision for each circles, so that it will not come out from its boundary.</p>

```javascript
// Yeah, this is just a simple collision algorithm but it really does its job. Even thou it is just for square, I just taken an advantage of how the shapes looks, so it is quite fine.
if (x2 > x1 && y2 > y1) {
  px = (px + iw > ax) ? (ax - iw) : px;
  py = (py + ih > ay) ? (ay - ih) : py;
}
else if (x2 < x1 && y2 < y1) {
  px = (px + iw < ax) ? (ax - iw) : px;
  py = (py + ih < ay) ? (ay - ih) : py;
}
else if (x2 > x1 && y2 < y1) {
  px = (px + iw > ax) ? (ax - iw) : px;
  py = (py + ih < ay) ? (ay - ih) : py;
}
else if (x2 < x1 && y2 > y1) {
  px = (px + iw < ax) ? (ax - iw) : px;
  py = (py + ih > ay) ? (ay - ih) : py;
}
```

<p>Finally, render the circle by its position.</p>

```javascript
p.style.left = (px + pw) + 'px';
p.style.top  = (py + pw) + 'px';
```

# Conclusion

<p>I am hearing many people saying that we don't need mathematics anymore in programming. I think they're wrong. Because in my opinion, if you don't know how the things works, you don't quite seem to know how to use the functions that are provided by each programming languages.</p>

<p>There are still so many things to learn that uses the principles that are taught on our mathematics class. Geometry is easier, but the statistics. We need it, example for the machine learning, deep learning, artificial intelligence, and any of the related principles. For game development, we need to learn physics, which is even with our current technology, it is not yet perfect.</p>

<p>Thank you for reading! If you have comments, suggestions, or reactions, please comment down below.</p>

---

<p>P.S. I am looking for better, smoother, and faster algorithm. if you could provide me, please comment down below.</p>

---

Resources:
[Where I read about Pythagorean theorem](https://en.wikipedia.org/wiki/Pythagorean_theorem)
[Where I read about Javascript](https://developer.mozilla.org/en-US/docs/Web/JavaScript)

{% codepen https://codepen.io/takaneichinose/pen/PoYGEmd %}