Arrow Grid Follow Variable Controls
by smombartz
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Arrow Grid Interaction</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="controls">
<form id="settings-form">
<label>
SVG Size:
<input type="number" id="svgSize" value="20" />
</label>
<label>
Grid Gap:
<input type="number" id="gridGap" value="20" />
</label>
<label>
Max Offset (degrees):
<input type="number" id="maxOffset" value="120" />
</label>
<label>
Smoothing Factor:
<input type="number" step="0.01" id="smoothingFactor" value="0" />
</label>
<label>
Rotation Speed (seconds):
<input type="number" step="0.1" id="rotationSpeed" value="0.25" />
</label>
<button type="submit">Apply Settings</button>
</form>
</div>
<div id="grid-container"></div>
<script src="script.js"></script>
</body>
</html>
CSS
/* Controls section styles */
#controls {
padding: 10px;
background-color: #f4f4f4;
border-bottom: 1px solid #ddd;
}
#settings-form {
display: flex;
gap: 10px;
flex-wrap: wrap;
}
#settings-form label {
display: flex;
flex-direction: column;
font-size: 14px;
}
#settings-form input {
padding: 5px;
margin-top: 5px;
font-size: 14px;
}
#settings-form button {
padding: 10px 15px;
font-size: 14px;
cursor: pointer;
}
/* Grid container styles */
#grid-container {
display: grid;
width: 100%;
height: 100vh;
gap: 10px; /* Default gap between grid items */
}
/* Grid item styles */
.grid-item {
display: flex;
justify-content: center;
align-items: center;
}
/* Arrow SVG styles */
.arrow {
transition: transform 0.25s ease; /* Smooth rotation */
}
JavaScript
// Default variables
let svgSize = 40; // Size of the SVG (in pixels)
let gridGap = 10; // Gap between grid items (in pixels)
let maxOffset = 30; // Maximum offset (in degrees) for arrows furthest from the mouse
let smoothingFactor = 0.2; // Factor to smooth out the interpolation
let rotationSpeed = 0.5; // Rotation speed (in seconds)
// Initialize the grid and controls
function initializeGrid() {
const container = document.getElementById('grid-container');
container.innerHTML = ''; // Clear existing grid
container.style.gridTemplateColumns = `repeat(auto-fit, minmax(${svgSize}px, 1fr))`;
container.style.gap = `${gridGap}px`;
// Populate the grid with enough SVGs to fill the screen
const totalArrows = 100;
for (let i = 0; i < totalArrows; i++) {
const gridItem = document.createElement('div');
gridItem.classList.add('grid-item');
gridItem.innerHTML = `
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 40 40" fill="none" width="${svgSize}" height="${svgSize}" class="arrow">
<path d="M1 20L20 1L40 20" stroke="currentColor"/>
<line x1="20" y1="1" x2="20" y2="40" stroke="currentColor"/>
</svg>`;
container.appendChild(gridItem);
}
// Apply rotation speed to all arrows
const arrows = document.querySelectorAll('.arrow');
arrows.forEach((arrow) => {
arrow.style.transition = `transform ${rotationSpeed}s ease`;
});
}
// Track the last angle and continuous angle for robust interpolation
let continuousAngle = 0;
// Add mousemove event listener to calculate rotation angle
document.addEventListener('mousemove', (e) => {
const { clientX, clientY } = e;
const centerX = window.innerWidth / 2;
const centerY = window.innerHeight / 2;
const dx = clientX - centerX;
const dy = clientY - centerY;
let newBaseAngle = Math.atan2(dy, dx) * (180 / Math.PI) + 90;
const angleDifference = newBaseAngle - (continuousAngle % 360);
if (angleDifference > 180) {
newBaseAngle -= 360;
} else if...