This example is like hello_party, but uses partyWatchShared()
to react to data updates.
This example shows how to connect to a party server, load a shared data object, and read and write to it. It also shows how to share a p5 color.
Try this example in two browser windows at once!
let shared;
function preload() {
partyConnect("wss://demoserver.p5party.org", "hello_watch");
shared = partyLoadShared("shared", { x: 0, y: 0 });
}
function setup() {
createCanvas(400, 400);
noStroke();
// stop looping; we'll redraw only when data changes
noLoop();
// subscribe to data changes, and draw when they happen
partyWatchShared(shared, onDataChange, true);
}
function mousePressed(e) {
// write shared dataa
shared.x = mouseX;
shared.y = mouseY;
}
function draw() {
background("#ffcccc");
fill("#000066");
// read shared data
ellipse(shared.x, shared.y, 100, 100);
}
function onDataChange(newValue) {
console.log("onDataChange", newValue);
// draw when the circle moves
draw();
}
<!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>