Last active
May 21, 2020 10:01
-
-
Save thisisjofrank/d177f0587530c596a30f875d53c585eb to your computer and use it in GitHub Desktop.
Publish an event to an Ably channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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