| 1 |
paper.install(window); |
| 2 |
|
| 3 |
$(function(){ |
| 4 |
// This demo depends on the canvas element |
| 5 |
if(!('getContext' in document.createElement('canvas'))){ |
| 6 |
alert('Sorry, it looks like your browser does not support canvas!'); |
| 7 |
return false; |
| 8 |
} |
| 9 |
var canvas = $('canvas')[0]; |
| 10 |
paper.setup(canvas); |
| 11 |
|
| 12 |
var doc = $(document) |
| 13 |
var color = randomColor(); |
| 14 |
var points = 5; |
| 15 |
var length = 15; |
| 16 |
var tool = new Tool(); |
| 17 |
|
| 18 |
// Generate an unique ID |
| 19 |
var id = Math.round($.now()*Math.random()); |
| 20 |
|
| 21 |
var clients = []; |
| 22 |
var worms = []; |
| 23 |
|
| 24 |
var centrifuge = new Centrifuge('ws://localhost:8000/connection/websocket'); |
| 25 |
|
| 26 |
centrifuge.subscribe("moving", function(message) { |
| 27 |
data = message.data; |
| 28 |
var path = data.path[1]; |
| 29 |
if(!(data.id in clients)){ |
| 30 |
if (data.id != id) { |
| 31 |
// New user has joined – create new worm. |
| 32 |
worms[data.id] = createWorm(data.color); |
| 33 |
} |
| 34 |
} |
| 35 |
if (data.id !== id) { |
| 36 |
worms[data.id].segments = path.segments; |
| 37 |
worms[data.id].strokeColor = data.color; |
| 38 |
} |
| 39 |
|
| 40 |
// Saving the current client state |
| 41 |
clients[data.id] = data; |
| 42 |
clients[data.id].updated = $.now(); |
| 43 |
}); |
| 44 |
|
| 45 |
centrifuge.connect(); |
| 46 |
|
| 47 |
var initialized = false; |
| 48 |
var myPath; |
| 49 |
|
| 50 |
// Remove inactive clients after 10 seconds of inactivity |
| 51 |
setInterval(function(){ |
| 52 |
for(var ident in clients){ |
| 53 |
if($.now() - clients[ident].updated > 10000){ |
| 54 |
// Last update was more than 10 seconds ago. |
| 55 |
// This user has probably closed the page |
| 56 |
delete clients[ident]; |
| 57 |
worms[ident].remove(); |
| 58 |
delete worms[ident]; |
| 59 |
} |
| 60 |
} |
| 61 |
}, 10000); |
| 62 |
|
| 63 |
function createWorm(color){ |
| 64 |
var path = new paper.Path({ |
| 65 |
strokeColor: color, |
| 66 |
strokeWidth: 20, |
| 67 |
strokeCap: 'round' |
| 68 |
}); |
| 69 |
|
| 70 |
var start = new paper.Point(Math.random()*100,Math.random()*100); |
| 71 |
for (var i = 0; i < points; i++) { |
| 72 |
path.add(new paper.Point(i * length + start.x, 0 + start.y)); |
| 73 |
} |
| 74 |
|
| 75 |
return path; |
| 76 |
} |
| 77 |
|
| 78 |
function randomColor() { |
| 79 |
colors = ['#5C4B51', '#8CBEB2', '#F3B562', '#F06060'] |
| 80 |
return colors[Math.floor(Math.random()*colors.length)]; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
var lastEmit = $.now(); |
| 85 |
|
| 86 |
paper.tool.onMouseMove = function(event) { |
| 87 |
if (!initialized) { |
| 88 |
// initialize worm on first mouse move. |
| 89 |
myPath = createWorm(color); |
| 90 |
initialized = true; |
| 91 |
} |
| 92 |
myPath.firstSegment.point = event.point; |
| 93 |
for (var i = 0; i < points - 1; i++) { |
| 94 |
var segment = myPath.segments[i]; |
| 95 |
var nextSegment = segment.next; |
| 96 |
var vector = new paper.Point(segment.point.x - nextSegment.point.x,segment.point.y - nextSegment.point.y); |
| 97 |
vector.length = length; |
| 98 |
nextSegment.point = new paper.Point(segment.point.x - vector.x,segment.point.y - vector.y); |
| 99 |
} |
| 100 |
myPath.smooth(); |
| 101 |
|
| 102 |
if ($.now() - lastEmit > 5) { |
| 103 |
var data = { |
| 104 |
'name': 'mousemove', |
| 105 |
'payload': { |
| 106 |
'color' : color, |
| 107 |
'path' : myPath, |
| 108 |
'id': id |
| 109 |
} |
| 110 |
} |
| 111 |
centrifuge.send(data); |
| 112 |
lastEmit = $.now(); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
paper.tool.onMouseUp = function(event) { |
| 117 |
var newColor = myPath.strokeColor; |
| 118 |
while(newColor === myPath.strokeColor){ |
| 119 |
newColor = randomColor(); |
| 120 |
} |
| 121 |
color = newColor; |
| 122 |
myPath.strokeColor = newColor; |
| 123 |
} |
| 124 |
|
| 125 |
function tick() { |
| 126 |
paper.view.draw(); |
| 127 |
requestAnimationFrame(tick); |
| 128 |
} |
| 129 |
requestAnimationFrame(tick); |
| 130 |
}); |