Skip to content

Instantly share code, notes, and snippets.

@thisisjofrank
Last active May 21, 2020 10:01
Show Gist options
  • Save thisisjofrank/d177f0587530c596a30f875d53c585eb to your computer and use it in GitHub Desktop.
Save thisisjofrank/d177f0587530c596a30f875d53c585eb to your computer and use it in GitHub Desktop.
Publish an event to an Ably channel
// get each of the squares in the svg
const squares = document.getElementsByClassName('square');
// create an onClick function
const onClick = (e) => colorAndPublish(e, channel);
// add an eventlistener for a click event to each square
for (let square of squares) {
square.addEventListener("click", onClick, false);
}
function colorAndPublish(e, channel) {
// get the number of the clicked pixel from it's id
let pixel = e.target.id.replace('square', '');
// change the colour of the pixel to the selected colour
e.target.style.fill = selected;
// publish the pixel number and the rgb value of the selected colour to the tshirt channel
channel.publish("tshirt", pixel + rgb);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment