forked from kets99/White-Space
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.js
More file actions
124 lines (98 loc) · 3.59 KB
/
Copy pathboard.js
File metadata and controls
124 lines (98 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
(function() {
/* Canvas */
var canvas = document.getElementById('drawCanvas');
var ctx = canvas.getContext('2d');
var color = document.querySelector(':checked').getAttribute('data-color');
canvas.width = Math.min(document.documentElement.clientWidth, window.innerWidth || 300);
canvas.height = Math.min(document.documentElement.clientHeight, window.innerHeight || 300);
ctx.strokeStyle = color;
ctx.lineWidth = '3';
ctx.lineCap = ctx.lineJoin = 'round';
/* Mouse and touch events */
document.getElementById('colorSwatch').addEventListener('click', function() {
color = document.querySelector(':checked').getAttribute('data-color');
}, false);
var isTouchSupported = 'ontouchstart' in window;
var isPointerSupported = navigator.pointerEnabled;
var isMSPointerSupported = navigator.msPointerEnabled;
var downEvent = isTouchSupported ? 'touchstart' : (isPointerSupported ? 'pointerdown' : (isMSPointerSupported ? 'MSPointerDown' : 'mousedown'));
var moveEvent = isTouchSupported ? 'touchmove' : (isPointerSupported ? 'pointermove' : (isMSPointerSupported ? 'MSPointerMove' : 'mousemove'));
var upEvent = isTouchSupported ? 'touchend' : (isPointerSupported ? 'pointerup' : (isMSPointerSupported ? 'MSPointerUp' : 'mouseup'));
canvas.addEventListener(downEvent, startDraw, false);
canvas.addEventListener(moveEvent, draw, false);
canvas.addEventListener(upEvent, endDraw, false);
/* PubNub */
var channel = 'draw';
var pubnub = PUBNUB.init({
publish_key : 'pub-c-156a6d5f-22bd-4a13-848d-b5b4d4b36695',
subscribe_key : 'sub-c-f762fb78-2724-11e4-a4df-02ee2ddab7fe',
leave_on_unload : true,
ssl : document.location.protocol === "https:"
});
pubnub.subscribe({
channel: channel,
callback: drawFromStream,
presence: function(m){
if(m.occupancy > 1){
document.getElementById('unit').textContent = 'doodlers';
}
document.getElementById('occupancy').textContent = m.occupancy;
var p = document.getElementById('occupancy').parentNode;
p.classList.add('anim');
p.addEventListener('transitionend', function(){p.classList.remove('anim');}, false);
}
});
function publish(data) {
pubnub.publish({
channel: channel,
message: data
});
}
/* Draw on canvas */
function drawOnCanvas(color, plots) {
ctx.strokeStyle = color;
ctx.beginPath();
ctx.moveTo(plots[0].x, plots[0].y);
for(var i=1; i<plots.length; i++) {
ctx.lineTo(plots[i].x, plots[i].y);
}
ctx.stroke();
}
function drawFromStream(message) {
if(!message || message.plots.length < 1) return;
drawOnCanvas(message.color, message.plots);
}
// Get Older and Past Drawings!
if(drawHistory) {
pubnub.history({
channel : channel,
count : 50,
callback : function(messages) {
pubnub.each( messages[0], drawFromStream );
}
});
}
var isActive = false;
var plots = [];
function draw(e) {
e.preventDefault(); // prevent continuous touch event process e.g. scrolling!
if(!isActive) return;
var x = isTouchSupported ? (e.targetTouches[0].pageX - canvas.offsetLeft) : (e.offsetX || e.layerX - canvas.offsetLeft);
var y = isTouchSupported ? (e.targetTouches[0].pageY - canvas.offsetTop) : (e.offsetY || e.layerY - canvas.offsetTop);
plots.push({x: (x << 0), y: (y << 0)}); // round numbers for touch screens
drawOnCanvas(color, plots);
}
function startDraw(e) {
e.preventDefault();
isActive = true;
}
function endDraw(e) {
e.preventDefault();
isActive = false;
publish({
color: color,
plots: plots
});
plots = [];
}
})();