A web-based drawing app
git clone git://soophie.de/outline
4176
box-shadow: 0 0px 8px 2px rgba(0, 0, 0, 0.05);4177
cursor: pointer;4178
transform: scale(0.9);4179
user-select: none;4180
}4181
.toolbar .bucket:hover {4182
transition: transform 0.2s;
13
<script src="js/index.js"></script>14
</head>15
<body>16
<canvas id="canvas"></canvas>16
<canvas class="canvas"></canvas>17
<div class="toolbar">18
<div class="actions">19
<button id="color" class="action tooltip" data-title="Color">19
<button class="action action-color tooltip" data-title="Color">20
<i class="ico">color_lens</i>21
</button>22
<button id="brush" class="action tooltip" data-title="Brush">22
<button class="action action-brush tooltip" data-title="Brush">23
<i class="ico">brush</i>24
</button>25
<button id="background" class="action tooltip" data-title="Background">25
<button class="action action-background tooltip" data-title="Background">26
<i class="ico">format_paint</i>27
</button>28
<button id="scaling" class="action tooltip" data-title="Scaling">28
<button class="action action-scaling tooltip" data-title="Scaling">29
<i class="ico">bubble_chart</i>30
</button>31
<button id="clear" class="action tooltip" data-title="Clear">31
<button class="action action-clear tooltip" data-title="Clear">32
<i class="ico">delete</i>33
</button>34
<button id="screenshot" class="action tooltip" data-title="Screenshot">34
<button class="action action-screenshot tooltip" data-title="Screenshot">35
<i class="ico">fullscreen</i>36
</button>37
</div>
1
// define1
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;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
};24
window.addEventListener('load', function(event) {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');26
canvas = document.querySelector('.canvas');27
canvas.draggable = true;29
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;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
}60
};61
document.onwheel = e => {62
if(e.deltaY < 0) {63
radius += radius < 30 ? 0.5 : 0;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 {66
radius -= radius > 0.5 ? 0.5 : 0;67
}68
};69
document.onkeydown = e => {70
if(e.keyCode == 9) {71
// HUD fade in69
env.radius -= env.radius > 0.5 ? 0.5 : 0;70
}73
if(e.keyCode >= 48 && e.keyCode <= 57) {74
let index = e.keyCode - 49;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
}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');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
});86
bucket.classList.add('focused');87
}104
item.classList.add('focused');105
});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();106
});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');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
});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
});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
});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
};122
resize();123
connect();124
});136
// functions137
let resize = () => {126
function resize() {127
canvas.width = window.innerWidth;128
canvas.height = window.innerHeight;140
};141
let connect = () => {142
socket = io('http://' + ip, {129
}131
function connect() {132
env.socket = io('http://' + window.location.host, {133
path: '/pipe'134
});145
socket.on('data', snap => {146
draw(snap.pos, snap.last, snap.rad, snap.clr);135
env.socket.on('data', function(data) {136
draw(data.pos, data.last, data.radius, data.color);137
});148
socket.on('clear', snap => {138
env.socket.on('clear', function(data) {139
g.clearRect(0, 0, window.innerWidth, window.innerHeight);140
});151
socket.on('background', snap => {152
g.fillStyle = snap;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
});156
};157
let send = ({ pos, last }) => {158
if(socket) {159
socket.emit('data', {146
}148
function send({ pos, last }) {149
if(env.socket) {150
env.socket.emit('data', {151
pos: pos,152
last: last,162
rad: radius,163
clr: color153
radius: env.radius,154
color: env.color155
});156
}166
};167
let draw = (pos, last, rad, clr) => {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);174
let length = dist / (rad / 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);178
g.fillStyle = clr;170
g.fillStyle = color;171
g.beginPath();180
g.arc(x, y, 2 * rad, 0, 2 * Math.PI);172
g.arc(x, y, 2 * radius, 0, 2 * Math.PI);173
g.fill();174
g.fillStyle = 'black';175
}176
}177
else {186
g.fillStyle = clr;178
g.fillStyle = color;179
g.beginPath();188
g.arc(pos.x, pos.y, 2 * rad, 0, 2 * Math.PI);180
g.arc(pos.x, pos.y, 2 * radius, 0, 2 * Math.PI);181
g.fill();182
g.fillStyle = 'black';183
}192
};184
}
14
box-shadow: 0 0px 8px 2px rgba(0, 0, 0, 0.05);15
cursor: pointer;16
transform: scale(0.9);17
user-select: none;19
&:hover {20
transition: transform 0.2s;