xxxxxxxxxx
var socket = io.connect(":30000?sketch=1376322");
// tilt and rotation for enabled devices
// on a desktop nothing will happen, but it wont break
// variables allow motion and ios to be detected.
// further code to detect a compass and tilt hardware on non-ios devices needed
let motion = false;
let ios = false;
// below code is essential for ios13 and above.
// A click is needed for the device to request permission
if (typeof DeviceMotionEvent.requestPermission === 'function') {
document.body.addEventListener('click', function() {
DeviceMotionEvent.requestPermission()
.then(function() {
console.log('DeviceMotionEvent enabled');
motion = true;
ios = true;
})
.catch(function(error) {
console.warn('DeviceMotionEvent not enabled', error);
})
})
} else {
// we are not on ios13 and above
// todo
// add detection for hardware for other devices
// if(got the hardware) {
// motion = true;
// }
}
function setup() {
createCanvas(windowWidth, windowHeight);
socket.on('iamdrawing', someoneIsDrawing); //when "iamdrawing" messages is received from server, set "someoneIsDrawing" function to be called
socket.on('hello!', someoneJoined); //when "hello!" messages is received from server, set "someoneJoined" function to be called
socket.emit('hello!'); //let's send a message to anyone in the room
background(0);
}
function draw() {
for(let i = 0;i<touches.length;i++)
{
fill(255, 255, 255, 90);
ellipse(touches[i].x, touches[i].y, 50, 50);
socket.emit('iamdrawing',touches[i].x, touches[i].y,50);
}
}
function someoneIsDrawing(x,y,r){ //this function will be called to draw a red ellipse for guest user
noStroke();
fill(255,0,0,90);
ellipse(x,y, r, r);
}
function someoneJoined(){ //this function will be called to notify users when a new user joins
print('New user joined!');
}
function touchStarted () {
var fs = fullscreen();
if (!fs) {
fullscreen(true);
}
}
/* prevents the mobile browser from processing some default
* touch events, like swiping left for "back" or scrolling
* the page.
*/
document.ontouchmove = function(event) {
event.preventDefault();
};
var socket = io.connect($OP.getEchoServerURL(1376322));
Learn more See an example