Skip to content

Instantly share code, notes, and snippets.

@BenoitZugmeyer
Created June 2, 2018 19:48
Show Gist options
  • Save BenoitZugmeyer/e828421b941bbd363461b9959fe46f07 to your computer and use it in GitHub Desktop.
Save BenoitZugmeyer/e828421b941bbd363461b9959fe46f07 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Improved</title>
</head>
<body>
<div id="leader" style="height: 40px; width: 40px; background: red; position: absolute; border-radius: 50%"></div>
<script type="module">
import observeRect from "https://cdn.rawgit.com/BenoitZugmeyer/observe-rect/improve-performances/src/index.js"
function createFollower() {
const follower = document.createElement("div")
follower.style.position = "absolute"
follower.style.width = follower.style.height = "20px"
follower.style.backgroundColor = "blue"
follower.style.borderRadius = "20px"
document.body.appendChild(follower)
const angle = Math.random() * Math.PI * 2
let observer = observeRect(leader, (rect) => {
follower.style.top = rect.top + rect.height / 2 - 10 + Math.sin(angle) * 50 + 'px'
follower.style.left = rect.left + rect.width / 2 - 10 + Math.cos(angle) * 50 + 'px'
})
observer.observe()
}
for (let i = 0; i < 100; i += 1) createFollower()
addEventListener('mousemove', (event) => {
leader.style.top = event.clientY - 20 + 'px'
leader.style.left = event.clientX - 20 + 'px'
})
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>Original</title>
</head>
<body>
<div id="leader" style="height: 40px; width: 40px; background: red; position: absolute; border-radius: 50%"></div>
<script type="module">
import observeRect from "https://cdn.rawgit.com/reach/observe-rect/a666e4e1/src/index.js"
function createFollower() {
const follower = document.createElement("div")
follower.style.position = "absolute"
follower.style.width = follower.style.height = "20px"
follower.style.backgroundColor = "blue"
follower.style.borderRadius = "20px"
document.body.appendChild(follower)
const angle = Math.random() * Math.PI * 2
let observer = observeRect(leader, (rect) => {
follower.style.top = rect.top + rect.height / 2 - 10 + Math.sin(angle) * 50 + 'px'
follower.style.left = rect.left + rect.width / 2 - 10 + Math.cos(angle) * 50 + 'px'
})
observer.observe()
}
for (let i = 0; i < 100; i += 1) createFollower()
addEventListener('mousemove', (event) => {
leader.style.top = event.clientY - 20 + 'px'
leader.style.left = event.clientX - 20 + 'px'
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment