A web-based drawing app
git clone git://soophie.de/outline
blob: d5d9efcb4c292aa88c84479fb1740852b2659cfb (4297 bytes)[doc][blame][raw]
1
// define2
let canvas;3
let g;4
let drag;5
let last;6
let radius = 3;7
let socket;8
let color = 'black';9
let ip = location.host; // '127.0.0.1';11
const Player = {12
name: null,13
ip: null14
};16
const Channel = {17
name: null18
};20
let buckets = [21
{ name: 'Black', code: 'black', active: true },22
{ name: 'White', code: 'white' },23
{ name: 'Red', code: 'red' },24
{ name: 'Blue', code: 'blue' },25
{ name: 'Green', code: 'green' },26
{ name: 'Yellow', code: 'yellow' },27
{ name: 'Brown', code: 'brown' },28
{ name: 'Grey', code: 'grey' },29
{ name: 'Purple', code: 'purple' },30
{ name: 'Bisque', code: 'bisque' }31
];33
// onload34
window.onload = e => {35
// init36
canvas = document.querySelector('#canvas');37
canvas.draggable = true;38
g = canvas.getContext('2d');39
// events40
canvas.ondragstart = e => false;41
canvas.oncontextmenu = e => false;42
canvas.onmousedown = e => {43
drag = true;44
let pos = { x: e.layerX, y: e.layerY };45
send({ pos: pos, last: last });46
draw(pos, last, radius, color);47
last = pos;48
};49
document.onmouseup = e => {50
drag = false;51
last = null;52
};53
canvas.onmousemove = e => {54
if(drag) {55
let pos = { x: e.layerX, y: e.layerY };56
send({ pos: pos, last: last });57
draw(pos, last, radius, color);58
last = pos;59
}60
};61
document.onwheel = e => {62
if(e.deltaY < 0) {63
radius += radius < 30 ? 0.5 : 0;64
}65
else {66
radius -= radius > 0.5 ? 0.5 : 0;67
}68
};69
document.onkeydown = e => {70
if(e.keyCode == 9) {71
// HUD fade in72
}73
if(e.keyCode >= 48 && e.keyCode <= 57) {74
let index = e.keyCode - 49;75
if(index == -1) {76
index = 9;77
}78
Array.from(document.querySelectorAll('.bucket')).forEach((bucket, i) => {79
if(index == i) {80
color = bucket.style.background;81
Array.from(document.querySelectorAll('.bucket')).forEach(b => {82
if(b != bucket) {83
b.classList.remove('focused');84
}85
});86
bucket.classList.add('focused');87
}88
});89
}90
};91
document.onkeyup = e => {92
if(e.keyCode == 9) {93
// HUD fade out94
}95
};96
window.onresize = e => resize();97
// ready98
resize();99
connect();101
buckets.forEach(bucket => {102
let tag = document.createElement('div');103
tag.classList.add('bucket');104
tag.classList.add('tooltip');105
tag.classList.add('up');106
tag.setAttribute('data-title', bucket.name);107
tag.style.background = bucket.code;108
if(bucket.active) {109
tag.classList.add('focused');110
}111
tag.addEventListener('click', e => {112
color = tag.style.background;113
Array.from(document.querySelectorAll('.bucket')).forEach(b => {114
if(b != bucket) {115
b.classList.remove('focused');116
}117
});118
tag.classList.add('focused');119
});120
document.querySelector('.toolbar').append(tag);121
});123
document.querySelector('#clear').addEventListener('click', e => {124
socket.emit('clear');125
g.clearRect(0, 0, window.innerWidth, window.innerHeight);126
});128
document.querySelector('#background').addEventListener('click', e => {129
socket.emit('background', color);130
g.fillStyle = color;131
g.fillRect(0, 0, window.innerWidth, window.innerHeight);132
g.fillStyle = 'black';133
});134
};136
// functions137
let resize = () => {138
canvas.width = window.innerWidth;139
canvas.height = window.innerHeight;140
};141
let connect = () => {142
socket = io('http://' + ip, {143
path: '/pipe'144
});145
socket.on('data', snap => {146
draw(snap.pos, snap.last, snap.rad, snap.clr);147
});148
socket.on('clear', snap => {149
g.clearRect(0, 0, window.innerWidth, window.innerHeight);150
});151
socket.on('background', snap => {152
g.fillStyle = snap;153
g.fillRect(0, 0, window.innerWidth, window.innerHeight);154
g.fillStyle = 'black';155
});156
};157
let send = ({ pos, last }) => {158
if(socket) {159
socket.emit('data', {160
pos: pos,161
last: last,162
rad: radius,163
clr: color164
});165
}166
};167
let draw = (pos, last, rad, clr) => {168
if(last) {169
let diff = {170
x: last.x - pos.x,171
y: last.y - pos.y172
};173
let dist = Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.y, 2), 2);174
let length = dist / (rad / 2);175
for(let i = 0; i < length; i++) {176
let x = pos.x + (diff.x / length * i);177
let y = pos.y + (diff.y / length * i);178
g.fillStyle = clr;179
g.beginPath();180
g.arc(x, y, 2 * rad, 0, 2 * Math.PI);181
g.fill();182
g.fillStyle = 'black';183
}184
}185
else {186
g.fillStyle = clr;187
g.beginPath();188
g.arc(pos.x, pos.y, 2 * rad, 0, 2 * Math.PI);189
g.fill();190
g.fillStyle = 'black';191
}192
};