CODE CENTER

Paw Cursor

The code here creates an effect like you can see here.


HTML

Nothing of particular note here, just that YOUR_CSS, YOUR_JS, and YOUR_CURSOR_IMAGE are to be filled in appropriately, and CANVAS_ID and IMAGE_ID can be any name as long as you use the same name in other files.

In order for us to use js to draw an image, the image needs to exist on the page; however it doesn't need to be visible, hence the img tag with display:none.


CSS

Important details to note here include pointer-events:none, z-index:100, and position:fixed. There are comments in the css to explain them.


JS

All the const values at the top are values you can adjust to change the behavior of your cursor. The code is commented to explain what each part does, but I'll go over some important details.

We're using a particle system here, which is to say: we set up an object that accepts some inputs and has very simple defined behavior (in this case, how to draw the particle, and how to track the lifetime of the particle) and simply draw and update every particle every frame, and remove particles once they're gone.

The other "fun" part is actually drawing the paw. Canvas objects are weird and you can't just draw a rotated object. Instead you have to move and rotate the canvas itself, draw your image, then undo the transformations.

Back to Code Center.