A web-based drawing app
git clone git://soophie.de/outline
blob: 48367ceeb6267e45cdbc60614ccc1a031a3cd131 (6906 bytes)[doc][blame][raw]
1
import constant from './mod/constant.js';2
import { uuid, download, switchTheme } from './mod/util.js';3
import { define, html } from './mod/node.js';4
import event from './mod/event.js';5
import paint from './mod/paint.js';6
import modal from './mod/modal.js';7
import socket from './mod/socket.js';9
const GOOGLE_API_KEY = 'QUl6YVN5QzZVcFRpNVlYc1h6eFJ2aEo4Z1RWbnJtTkpCQ2JCMjBn';11
export default {12
state: {13
last: null,14
cursor: null,15
radius: 3,16
color: '#000000',17
client: [],18
opened: true,19
current: undefined,20
modal: {},21
action: {},22
fps: 1000 / 60,23
then: Date.now(),24
dark: false25
},26
template: {},27
modal: {},28
init: function() {29
this.constant = constant;30
this.event = event;31
this.paint = paint;32
this.modal = modal;33
this.socket = socket;35
// build36
this.node = define({37
canvas: '.canvas',38
overlay: '.overlay',39
toolbar: '.toolbar',40
wrapper: '.wrapper',41
modal: { query: '.modal', all: true },42
action: { query: '[data-action]', all: true },43
list: '.bucket-list',44
spinner: '.spinner'45
});47
this.template.bucket = html(function(item) {48
return `<div class="bucket${item.active ? ' active' : ''}"49
data-color="${item.code}"50
style="background: ${item.code}"></div>`;51
});52
this.node.bucket = [];53
for(let item of this.constant.basic) {54
let result = this.template.bucket.build(item);55
this.node.bucket.push(result.element);56
result.render(this.node.toolbar);57
}58
this.node.color = [];59
for(let item of this.constant.color) {60
let result = this.template.bucket.build(item);61
this.node.color.push(result.element);62
result.render(this.node.list);63
}65
// init66
this.event.init(this);67
this.paint.init(this);68
this.modal.init(this);69
this.socket.init(this);70
let that = this;72
// cache73
if(typeof localStorage !== 'undefined') {74
if(typeof localStorage.getItem('outline.theme.dark') !== 'undefined') {75
this.state.dark = (localStorage.getItem('outline.theme.dark') === 'true');76
for(let item of this.node.action) {77
if(item.dataset.action === 'theme') {78
switchTheme(item, that);79
}80
}81
}82
}84
// events85
this.event.bind(this.node.canvas, {86
prevent: ['dragstart', 'contextmenu'],87
begin: ['mousedown', 'touchstart'],88
drag: ['mousemove', 'touchmove']89
});90
this.event.bind(document, {91
move: ['mousemove', 'touchmove'],92
end: ['mouseup', 'touchend', 'mouseleave'],93
key: 'keydown',94
out: 'mouseout',95
scroll: { type: 'wheel', passive: false }96
});97
this.event.bind(window, {98
resize: 'resize'99
});100
this.event.bind(this.node.bucket, {101
button: 'mousedown'102
});103
this.event.resize();105
// actions106
for(let item of this.node.action) {107
if(item.dataset.action === 'theme') {108
item.addEventListener('click', function(event) {109
switchTheme(item, that, true);110
});111
}112
}114
this.modal.get('color').action.open.before = function(modal) {115
for(let item of that.node.color) {116
item.classList[that.state.color === item.dataset.color ? 'add' : 'remove']('active');117
}118
}120
this.modal.get('join').element.querySelector('.action-uuid').focus();121
this.event.action(this.modal.get('join').action.uuid, 'keydown', function(event) {122
let input = that.modal.get('join').element.querySelector('.action-uuid');123
input.classList.remove('invalid');124
if(event.keyCode === 13) {125
that.state.name = input.value.replace(/\s+/g, '');126
if(that.state.name.length >= 3 && that.state.name.length <= 20) {127
that.state.uuid = that.state.name + '-' + uuid();128
that.socket.connect();129
that.modal.load();130
}131
else {132
input.classList.add('invalid');133
}134
}135
});136
this.event.action(this.modal.get('join').action.join, 'click', function(event) {137
let input = that.modal.get('join').element.querySelector('.action-uuid');138
that.state.name = input.value.replace(/\s+/g, '');139
if(that.state.name.length >= 3 && that.state.name.length <= 20) {140
that.state.uuid = that.state.name + '-' + uuid();141
that.socket.connect();142
that.modal.load();143
}144
else {145
input.classList.add('invalid');146
}147
});148
this.modal.get('scaling').action.open.before = function(modal) {149
modal.element.querySelector('input.action-range').value = that.state.radius;150
let circle = modal.element.querySelector('.circle');151
circle.style.width = that.state.radius * 4 + 'px';152
circle.style.height = that.state.radius * 4 + 'px';153
}154
this.event.action(this.modal.get('scaling').action.range, 'input', function(event) {155
setTimeout(function() {156
let circle = that.modal.get('scaling').element.querySelector('.modal .circle');157
circle.style.width = event.target.value * 4 + 'px';158
circle.style.height = event.target.value * 4 + 'px';159
}, 10);160
});161
this.event.action(this.modal.get('scaling').action.scaling, 'click', function(event) {162
that.state.radius = parseInt(that.modal.get('scaling').element.querySelector('.modal input.action-range').value);163
that.modal.close();164
});165
this.event.action(this.modal.get('clear').action.clear, 'click', function(event) {166
that.paint.g.fillStyle = 'white';167
that.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);168
that.socket.clear();169
that.modal.close();170
});171
this.modal.get('save').action.open.before = function(modal) {172
modal.element.querySelector('input.action-file').value = 'outline-image-' + uuid();173
let input = that.modal.get('save').element.querySelector('.modal input.action-file');174
input.classList.remove('invalid');175
}176
this.event.action(this.modal.get('save').action.save, 'click', function(event) {177
let input = that.modal.get('save').element.querySelector('.modal input.action-file');178
input.classList.remove('invalid');179
let file = input.value.replace(/\s+/g, '');180
if(file.length >= 3 && file.length <= 40) {181
download(file, that.node.canvas.toDataURL('image/jpg'));182
that.modal.close();183
}184
else {185
input.classList.add('invalid');186
}187
});188
for(let item of this.node.color) {189
item.addEventListener('click', function(event) {190
that.state.color = item.dataset.color;191
for(let _item of that.node.bucket) {192
_item.classList.remove('active');193
}194
that.modal.close();195
});196
}197
this.event.action(this.modal.get('error').action.reconnect, 'click', function(event) {198
that.modal.load();199
that.socket.reconnect();200
});201
this.loop();202
},203
update: function() {204
this.socket.cursor();205
this.paint.o.clearRect(0, 0, window.innerWidth, window.innerHeight);206
this.paint.cursor(this.state);207
for(let client of Object.values(this.state.client)) {208
this.paint.cursor(client);209
}210
},211
loop: function() {212
requestAnimationFrame(this.loop.bind(this));213
this.state.now = Date.now();214
this.state.elapsed = this.state.now - this.state.then;216
if(this.state.elapsed > this.state.fps) {217
this.state.then = this.state.now - (this.state.elapsed % this.state.fps);218
this.update();219
}220
}221
};