Svelte Cursorphobic

A (very) lightweight component that makes elements afraid of your cursor. Used to easily create cute animations that react to your mouse.

GitHub

Installation

Install using Yarn or NPM.

yarn add svelte-cursorphobic --dev npm install svelte-cursorphobic --save-dev

Include the component in your app.

import Cursorphobic from "svelte-cursorphobic"

Usage

The component is nothing but a wrapper to make anything cursorphobic. All you do is wrap an element in the component, and you're done!

<Cursorphobic>
  <div>I am afraid of cursors!</div>
</Cursorphobic>
I am afraid of cursors!

To make multiple elements cursorphobic, you wrap each element inside their own component. Position is done via regular old css. It's just an element, style it however you want!

<div style="..."><Cursorphobic><div /></Cursorphobic></div>
<div style="..."><Cursorphobic><div /></Cursorphobic></div>
<div style="..."><Cursorphobic><div /></Cursorphobic></div>
<div style="..."><Cursorphobic><div /></Cursorphobic></div>

Need a grid like the one at the top of the page? Svelte, along with some basic CSS, does the heavy lifting.

<div class="grid">
  {#each { length: 80 } as _}
    <Cursorphobic>
      <div class="element" />
    </Cursorphobic>
  {/each}
</div>

.grid {
  display: grid;
  grid-template-columns: repeat(20, 1fr);
  width: fit-content;
}

Properties

The component has 3 properties to how the element reacts to your cursor.

Range

The range property controls from how far away the component starts reacting to your cursor. (Defaults to 200)

<Cursorphobic range="100">
  ...
</Cursorphobic>
100
<Cursorphobic range="300">
  ...
</Cursorphobic>
300

Multiplier

The multiplier property controls how heavily an element reacts to your cursor. (Range from 0 to 1, defaults to 0.1)

<Cursorphobic multiplier="0.02">
  ...
</Cursorphobic>
0.02
<Cursorphobic multiplier="0.5">
  ...
</Cursorphobic>
0.5

Smoothing

The smoothing property controls how quickly an element moves relative to changes in your cursor. (Range from 0 to 1, defaults to 0.25)

<Cursorphobic smoothing="0.05">
  ...
</Cursorphobic>
0.05
<Cursorphobic smoothing="1">
  ...
</Cursorphobic>
1

Playground

Range 200
Multiplier 0.1
Smoothing 0.25
Size 1