A web-based drawing app
git clone git://soophie.de/outline
blob: 221d83259799e1ede0a214a0df867c82c8e478a3 (4618 bytes)[doc][blame][raw]
1
let env = {2
drag: false,3
last: null,4
color: 'black',5
radius: 3,6
bucket: [7
{ name: 'Black', code: 'black', active: true },8
{ name: 'White', code: 'white' },9
{ name: 'Red', code: 'red' },10
{ name: 'Blue', code: 'blue' },11
{ name: 'Green', code: 'green' },12
{ name: 'Yellow', code: 'yellow' },13
{ name: 'Brown', code: 'brown' },14
{ name: 'Grey', code: 'grey' },15
{ name: 'Purple', code: 'purple' },16
{ name: 'Bisque', code: 'bisque' }17
],18
socket: null19
};21
let canvas;22
let g;24
window.addEventListener('load', function(event) {26
canvas = document.querySelector('.canvas');27
canvas.draggable = true;29
g = canvas.getContext('2d');31
env.bucket.forEach(function(item) {32
let template = document.createElement('template');33
template.innerHTML = `<div class="bucket tooltip up${item.active ? ' focused' : ''}" data-title="${item.name}" style="background: ${item.code}"></div>`;34
document.querySelector('.toolbar').append(template.content.cloneNode(true));35
});37
canvas.addEventListener('dragstart', function(event) {38
event.preventDefault();39
});40
canvas.addEventListener('contextmenu', function(event) {41
event.preventDefault();42
});43
canvas.addEventListener('mousemove', function(event) {44
if(env.drag) {45
let pos = { x: event.layerX, y: event.layerY };46
send({ pos: pos, last: env.last });47
draw(pos, env.last, env.radius, env.color);48
env.last = pos;49
}50
});51
canvas.addEventListener('mousedown', function(event) {52
env.drag = true;53
let pos = { x: event.layerX, y: event.layerY };54
send({ pos: pos, last: env.last });55
draw(pos, env.last, env.radius, env.color);56
env.last = pos;57
});59
document.addEventListener('mouseup', function(event) {60
env.drag = false;61
env.last = null;62
});63
document.addEventListener('wheel', function(event) {65
if(event.deltaY < 0) {66
env.radius += env.radius < 30 ? 0.5 : 0;67
}68
else {69
env.radius -= env.radius > 0.5 ? 0.5 : 0;70
}71
});72
document.addEventListener('keydown', function(event) {74
if(event.keyCode >= 48 && event.keyCode <= 57) {75
let index = event.keyCode - 49;76
if(index == -1) {77
index = 9;78
}79
Array.from(document.querySelectorAll('.bucket'))80
.forEach(function(item, i) {81
if(index == i) {82
env.color = item.style.background;83
item.classList.add('focused');84
}85
else {86
item.classList.remove('focused');87
}88
});89
}90
});92
window.addEventListener('resize', resize);94
Array.from(document.querySelectorAll('div.bucket'))95
.forEach(function(item) {96
item.addEventListener('click', function(event) {97
env.color = item.style.background;98
Array.from(document.querySelectorAll('.bucket'))99
.forEach(function(_item) {100
if(_item != item) {101
_item.classList.remove('focused');102
}103
});104
item.classList.add('focused');105
});106
});108
document.querySelector('button.action-clear')109
.addEventListener('click', function(event) {110
env.socket.emit('clear');111
g.clearRect(0, 0, window.innerWidth, window.innerHeight);112
});114
document.querySelector('button.action-background')115
.addEventListener('click', function(event) {116
env.socket.emit('background', env.color);117
g.fillStyle = env.color;118
g.fillRect(0, 0, window.innerWidth, window.innerHeight);119
g.fillStyle = 'black';120
});122
resize();123
connect();124
});126
function resize() {127
canvas.width = window.innerWidth;128
canvas.height = window.innerHeight;129
}131
function connect() {132
env.socket = io('http://' + window.location.host, {133
path: '/pipe'134
});135
env.socket.on('data', function(data) {136
draw(data.pos, data.last, data.radius, data.color);137
});138
env.socket.on('clear', function(data) {139
g.clearRect(0, 0, window.innerWidth, window.innerHeight);140
});141
env.socket.on('background', function(data) {142
g.fillStyle = data;143
g.fillRect(0, 0, window.innerWidth, window.innerHeight);144
g.fillStyle = 'black';145
});146
}148
function send({ pos, last }) {149
if(env.socket) {150
env.socket.emit('data', {151
pos: pos,152
last: last,153
radius: env.radius,154
color: env.color155
});156
}157
}159
function draw(pos, last, radius, color) {160
if(last) {161
let diff = {162
x: last.x - pos.x,163
y: last.y - pos.y164
};165
let dist = Math.sqrt(Math.pow(diff.x, 2) + Math.pow(diff.y, 2), 2);166
let length = dist / (radius / 2);167
for(let i = 0; i < length; i++) {168
let x = pos.x + (diff.x / length * i);169
let y = pos.y + (diff.y / length * i);170
g.fillStyle = color;171
g.beginPath();172
g.arc(x, y, 2 * radius, 0, 2 * Math.PI);173
g.fill();174
g.fillStyle = 'black';175
}176
}177
else {178
g.fillStyle = color;179
g.beginPath();180
g.arc(pos.x, pos.y, 2 * radius, 0, 2 * Math.PI);181
g.fill();182
g.fillStyle = 'black';183
}184
}