Cursor · 22 of 22
Rope Trail
Elastic rope of segments chases cursor
<div class="rope-area" id="ropeArea">
<h2>Rope</h2>
<svg class="rope-svg" id="ropeSvg"><polyline fill="none" stroke="#7c5cff" stroke-width="3" stroke-linecap="round" stroke-linejoin="round" points=""/></svg>
</div>
.rope-area {
position: relative;
height: 240px;
display: flex;
align-items: center;
justify-content: center;
background: #12121a;
overflow: hidden;
font-family: sans-serif;
}
.rope-area h2 { color: #333; }
.rope-svg {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
pointer-events: none;
}
var area = document.getElementById('ropeArea');
var line = document.querySelector('#ropeSvg polyline');
var SEGMENTS = 18;
// Per-segment lag in ms. Frame-rate independent, so the rope has the same length
// and slack at 60Hz and 144Hz instead of snapping tight on fast displays.
var SEG_TAU = 38;
var pts = [];
for (var i = 0; i < SEGMENTS; i++) pts.push({ x: 0, y: 0 });
var ropeX = 0, ropeY = 0, inside = false;
area.addEventListener('mousemove', function (e) {
var rect = area.getBoundingClientRect();
ropeX = e.clientX - rect.left;
ropeY = e.clientY - rect.top;
if (!inside) {
// Snap the whole rope to the entry point to avoid a whip from (0,0)
pts.forEach(function (p) { p.x = ropeX; p.y = ropeY; });
inside = true;
}
});
area.addEventListener('mouseleave', function () { inside = false; });
var ropeLast = 0;
function tick(now) {
var dt = ropeLast ? Math.min(now - ropeLast, 64) : 16.7;
ropeLast = now;
var a = 1 - Math.exp(-dt / SEG_TAU);
// Head is pinned to the cursor; each segment chases the one before it,
// so the rope always trails behind instead of drifting ahead
pts[0].x = ropeX;
pts[0].y = ropeY;
for (var i = 1; i < SEGMENTS; i++) {
pts[i].x += (pts[i - 1].x - pts[i].x) * a;
pts[i].y += (pts[i - 1].y - pts[i].y) * a;
}
line.setAttribute('points', pts.map(function (p) {
return p.x.toFixed(1) + ',' + p.y.toFixed(1);
}).join(' '));
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);