/
index.js
48 lines (38 loc) · 1.22 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
let shared;
function preload() {
partyConnect("wss://demoserver.p5party.org", "p5_objects");
shared = partyLoadShared("shared");
}
function setup() {
createCanvas(400, 400);
if (partyIsHost()) {
partySetShared(shared, {
pos: { x: 200, y: 200 },
color: color("red").toString(),
});
}
}
function mousePressed() {
// p5.Vectors can't be shared directly
const mouseVector = createVector(mouseX, mouseY);
// but you probably only need to share
// the x and y (and sometimes Z) properties
// unpack the values you need to share
// from the vector
shared.pos = { x: mouseVector.x, y: mouseVector.y };
// shared.pos isn't a p5.Vector, just a simple data object
// p5.Color objects can't be shared directly
const randomColor = color(random(255), random(255), random(255));
// but you can convert p5.Color objects to strings for sharing
shared.color = randomColor.toString();
// the string looks like this "rgba(255, 0, 0, 1)"
}
function draw() {
background(220);
noStroke();
// p5 functions that take color arguments
// can accept the color description strings
fill(shared.color);
// shared.pos has the x and y values we need.
ellipse(shared.pos.x, shared.pos.y, 50, 50);
}