---
title: Dancing cursors and Voronoi diagrams
date: 2023-09-25 14:26
tags: [partykit]
author: "Matt Webb"
pubDate: "Mon, 25 Sep 2023 14:26:00 +0100"
link: "https://interconnected.org/home/2023/09/25/dancing-cursors-and-voronoi-diagrams"
urlPrev: "https://interconnected.org/home/2023/09/22/wards.md"
urlNext: "https://interconnected.org/home/2023/09/29/live-polls-with-stencil.md"
---
# Dancing cursors and Voronoi diagrams

_By {{page.author}} on {{page.pubDate}}._

_The following was first posted on the[PartyKit blog][1] and has since been
archived here._

---

If you go the [PartyKit homepage][2] right now, you may be lucky enough to see
it dancing with cursors.

_(Or maybe it doesn't do that anymore! For me, "right now" is September
2023.)_

It's a trope but I love multiplayer cursors. The web should feel more _alive!_
And when you see other people's real-time cursors, you get a sense of
presence.

The pattern itself is a **Voronoi diagram**.

To make a [Voronoi diagram][3] _(Wikipedia),_ you take a point (a cursor in
this case) and look for its nearest neighbour. Then you draw a line,
crossways, at the midpoint between them. Rinse and repeat for all points and
all neighbours. That gives you a set of cells: small cells for points with
many nearby neighbours, large cells when a point has plenty of space. Colour
the cells as you want.

And this is what that is, only interactive. I find myself playing with other
people's cursors, seeing how the cell boundary changes between us --- and
sometimes you find someone who dances back.

Actually, this does happen! Check out [this exchange][4] I happened across on
X/Twitter:

![A Twitter thread. The first message is 'and ofc partykit gets much better
with friends' with a picture of the homepage. The first reply is: 'holy s---,
was that you I was going this with!??'][5]

Happy serendipity!

### Diving into the code

[Here's a standalone demo.][6] And [here's the code on GitHub][7].

The PartyKit server receives all the cursor coordinates and re-broadcasts them
to all other clients connected to the room. (Actually it's not absolute
coordinates. They're scaled in proportion to the screen.) [The server code is
pretty brief][7], but note three things:

  * we've opted into **hibernation** ([docs][8]) which means the server can
    handle thousands of connections simultaneously. The bottleneck is the
    frequency of cursor updates from the clients: really those should be
    debounced, and the updates batched up so they're not broadcast so often.
  * **country flags!** In `onConnect` we're looking at the `request` object
    and pulling out some Cloudflare-specific data. In particular, we're
    grabbing the connection's location (to a country resolution) and using an
    attachment to save it on the websocket object for later display. That's
    how the flags appear.
  * finally, **a cursor carries its type** with it: `mouse` or `touch`.
    Touches on a smartphone screen are ephemeral --- the cursor only shows up
    while the user is touching their phone, and the client displays it as a
    hand not a pointer.

The Voronoi pattern itself is drawn with the data visualisation library
[D3][9].

D3 comes with [a package to compute Voronoi diagrams][10].

You can see [the Voronoi integration itself here][11]. It's part of the
client-side app.

It's pretty neat to be able to flow messages from `usePartySocket` into a
live, programmatic SVG. This sketch is just a toy, but given D3 is a data
visualisation language, I do wonder what other real time sources could be
similarly plugged in.

### Cursors, cursors, cursors

Like I said, I can't get enough of cursors. _(Which don't translate well to
smartphones or future spatial computing, sure, but we'll cross that bridge
when we come to it.)_

![A screengrab of my blog with multiple blurred-out cursors behind the
text.][12]

On [my blog][13], for example, I show real-time multiplayer cursors on every
page, but blurred slightly as if beyond frosted glass. For vibes.

And I too have had that serendipitious experience of dancing cursors with a
stranger!

But all of that is a story for another day.

   [1]: https://blog.partykit.io/posts/dancing-cursors-and-voronoi-diagrams

   [2]: https://www.partykit.io

   [3]: https://en.wikipedia.org/wiki/Voronoi_diagram

   [4]: https://twitter.com/adam_janis/status/1701703493143409148

   [5]: /more/2026/partykit/assets/dancing-cursors-and-voronoi-diagrams-1.png

   [6]: https://multicursor-sketch.vercel.app

   [7]: https://github.com/partykit/sketch-voronoi

   [8]: https://docs.partykit.io/guides/scaling-partykit-servers-with-hibernation/

   [9]: https://d3js.org

   [10]: https://d3js.org/d3-delaunay

   [11]: https://github.com/partykit/sketch-voronoi/blob/main/src/app/shared-space.tsx

   [12]: /more/2026/partykit/assets/dancing-cursors-and-voronoi-diagrams-2.png

   [13]: https://interconnected.org/home/


