This example shows how to connect to a party server, load a shared data object, and read and write to it.
This example loads p5.party.js from a local file.
<script src="/dist/p5.party.js"></script>
See hello_cdn for an example of loading p5.party.js from a cdn.
Try this example in two browser windows at once!
let shared;
function preload() {
// connect to the party server
partyConnect("wss://demoserver.p5party.org", "hello_party");
// begin loading shared object
// and provide starting values for the object to be used
// if there are no clients already connected to the room
// setup() won't be called until the shared object is loaded
shared = partyLoadShared("shared", { x: 100, y: 100 });
}
function setup() {
createCanvas(400, 400);
noStroke();
partyToggleInfo(true);
}
function mousePressed() {
// write shared data
shared.x = mouseX;
shared.y = mouseY;
}
function draw() {
background("#ffcccc");
fill("#000066");
// read shared data
ellipse(shared.x, shared.y, 100, 100);
}
<!DOCTYPE html>
<html>
<head> </head>
<body>
<main></main>
<div id="readme"></div>
<h2>index.js</h2>
<div id="source-javascript"></div>
<h2>index.html</h2>
<div id="source-html"></div>
<script src="https://cdn.jsdelivr.net/npm/p5@1.4.1/lib/p5.js"></script>
<script src="/dist/p5.party.js"></script>
<script src="index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/highlight.min.js"></script>
<link rel="stylesheet" href="/examples.css" />
<script src="/examples.js" type="module"></script>
</body>
</html>