Created
April 18, 2018 02:50
-
-
Save nasser/19b47448ca8e658ddbd9c2fb8d55520a to your computer and use it in GitHub Desktop.
socket drawing
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
<!-- in public folder --> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="viewport" width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0> | |
<style> body {padding: 0; margin: 0;} </style> | |
<script src="p5.min.js"></script> | |
<script src="/socket.io/socket.io.js"></script> | |
<!-- <script src="../addons/p5.dom.min.js"></script> --> | |
<!-- <script src="../addons/p5.sound.min.js"></script> --> | |
<script src="sketch.js"></script> | |
</head>344 | |
<body> | |
</body> | |
</html> |
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
var express = require('express'); | |
var app = express(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
app.use(express.static('public')); | |
io.on('connection', function(socket){ | |
console.log('a user connected', socket.id); | |
socket.on('brush', function(brushData) { | |
console.log('brush', brushData); | |
socket.broadcast.emit('brush', brushData); | |
}); | |
}); | |
http.listen(3000, function(){ | |
console.log('listening on *:3000'); | |
}); |
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
// in public folder | |
var socket; | |
var myColor; | |
function setup() { | |
socket = io(); | |
myColor = { r:random(255), g:random(255), b:random(255) } | |
// draw remote brush strokes | |
socket.on('brush', function(brushData) { | |
// we expect brushData.x, brushData.y, brushData.color | |
stroke(brushData.color.r, brushData.color.g, brushData.color.b); | |
line(brushData.x, brushData.y, brushData.px, brushData.py); | |
}); | |
createCanvas(displayWidth, displayHeight); | |
strokeWeight(10) | |
stroke(0); | |
} | |
function mouseDragged() { | |
stroke(myColor.r, myColor.g, myColor.b); | |
line(mouseX, mouseY, pmouseX, pmouseY); | |
socket.emit('brush', {x:mouseX, | |
y:mouseY, | |
px:pmouseX, | |
py:pmouseY, | |
color:myColor}); | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment