I am trying to create an effect over a grid with many small elements, I want the cursor to leave a trail over the grid when mouse moves over it.
Working example in codepen: https://codepen.io/SrPunta/pen/bGNORmW
loadGrid([]);
function loadGrid(imageElements) {
let emptySquare = {
width: 8,
height: 8,
imageUrl: '',
isEmpty: true
};
const gridElements = [];
// this for only to fill 10000 empty squares
for (let row = 0; row < 100; row++) {
for (let column = 0; column < 100; column++) {
emptySquare.offsetLeft = column * 10;
emptySquare.offsetTop = row * 10;
gridElements.push(_.clone(emptySquare));
}
}
const gridContainer = $('.grid-container');
console.log('container2', gridContainer);
const resultGrid = gridElements.concat(imageElements);
resultGrid.forEach(element => addEmptySquare(element));
}
function addEmptySquare(element, container) {
const square = document.createElement('div');
square.style.width = element.width + 'px';
square.style.height = element.height + 'px';
square.style.left = element.offsetLeft + 'px';
square.style.top = element.offsetTop + 'px';
square.className = 'empty-square';
document.getElementById('grid-container').appendChild(square);
}
.empty-square {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAANQTFRF9/X1s+1L5AAAAAtJREFUeJxjYEAFAAAQAAE5vY9lAAAAAElFTkSuQmCC');
border: 1px solid #CCCCCC;
position: absolute;
transition: background-image 1s ease;
-moz-transition: background-image 1s ease;
-webkit-transition: background-image 1s ease;
}
.empty-square:hover {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAIAQMAAAD+wSzIAAAAAXNSR0IB2cksfwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAANQTFRFgpD+bo+nNQAAAAtJREFUeJxjYEAFAAAQAAE5vY9lAAAAAElFTkSuQmCC');
transition: background-image 0s ease;
-moz-transition: background-image 0s ease;
-webkit-transition: background-image 0s ease;
}
<html>
<head>
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
</head>
<body>
<div id="grid-container"> </div>
</body>
</html>
But as you can see when mouse moves fast some element are skipped and the hover animation is not triggered.
How I can achieve this or have a continuous trail without skip elements?
NOTE: I use base64 image as background because it is much faster with many elements.