A web-based drawing app
git clone git://soophie.de/outline
blob: 9d28a68ac3a380b35961ce8375194163a0281283 (7266 bytes)[doc][blame][raw]
1
import util from './mod/util.js';2
import node from './mod/node.js';3
import event from './mod/event.js';4
import paint from './mod/paint.js';5
import modal from './mod/modal.js';6
import socket from './mod/socket.js';8
const GOOGLE_API_KEY = 'QUl6YVN5QzZVcFRpNVlYc1h6eFJ2aEo4Z1RWbnJtTkpCQ2JCMjBn';10
export default {11
constant: {12
color: [13
{ name: 'Salmon', code: '#FA8072' },14
{ name: 'Crimson', code: '#DC143C' },15
{ name: 'Firebrick', code: '#B22222' },16
{ name: 'Pink', code: '#FFC0CB' },17
{ name: 'Hotpink', code: '#FF69B4' },18
{ name: 'Coral', code: '#FF7F50' },19
{ name: 'Tomato', code: '#FF6347' },20
{ name: 'Orange', code: '#FFA500' },21
{ name: 'Gold', code: '#FFD700' },22
{ name: 'Khaki', code: '#F0E68C' },23
{ name: 'Lavender', code: '#E6E6FA' },24
{ name: 'Plum', code: '#DDA0DD' },25
{ name: 'Violet', code: '#EE82EE' },26
{ name: 'Magenta', code: '#FF00FF' },27
{ name: 'Purple', code: '#800080' },28
{ name: 'Indigo', code: '#4B0082' },29
{ name: 'Lime', code: '#00FF00' },30
{ name: 'Olive', code: '#808000' },31
{ name: 'Teal', code: '#008080' },32
{ name: 'Cyan', code: '#00FFFF' },33
{ name: 'Aquamarine', code: '#7FFFD4' },34
{ name: 'Turquoise', code: '#40E0D0' },35
{ name: 'Navy', code: '#000080' },36
{ name: 'Bisque', code: '#FFE4C4' },37
{ name: 'Tan', code: '#D2B48C' },38
{ name: 'Peru', code: '#CD853F' },39
{ name: 'Chocolate', code: '#D2691E' },40
{ name: 'Sienna', code: '#A0522D' },41
{ name: 'Brown', code: '#A52A2A' },42
{ name: 'Silver', code: '#C0C0C0' }43
],44
basic: [45
{ name: 'Black', code: '#000000', active: true },46
{ name: 'White', code: '#FFFFFF' },47
{ name: 'Grey', code: '#808080' },48
{ name: 'Red', code: '#FF0000' },49
{ name: 'Blue', code: '#0000FF' },50
{ name: 'Green', code: '#00FF00' },51
{ name: 'Yellow', code: '#FFFF00' }52
]53
},54
state: {55
last: null,56
cursor: null,57
radius: 3,58
color: '#000000',59
client: [],60
opened: true,61
current: undefined,62
modal: {},63
action: {}64
},65
template: {},66
modal: {},67
init: function() {68
this.util = util;69
this.node = node;70
this.event = event;71
this.paint = paint;72
this.modal = modal;73
this.socket = socket;75
// build76
this.node = node.define({77
canvas: '.canvas',78
overlay: '.overlay',79
toolbar: '.toolbar',80
wrapper: '.wrapper',81
modal: { query: '.modal', all: true },82
action: { query: '[data-action]', all: true },83
list: '.bucket-list',84
spinner: '.spinner'85
});86
this.template.bucket = node.template(function(item) {87
return `<li class="bucket tooltip${item.up ? ' up' : ''}${item.active ? ' active' : ''}" data-title="${item.name}" data-color="${item.code}" style="background: ${item.code}"></li>`;88
});89
this.node.bucket = [];90
for(let item of this.constant.basic) {91
let render = this.template.bucket({ up: true, ...item});92
this.node.bucket.push(render.element);93
render(this.node.toolbar);94
}95
this.node.color = [];96
for(let item of this.constant.color) {97
let render = this.template.bucket({ up: false, ...item});98
this.node.color.push(render.element);99
render(this.node.list);100
}102
// init103
this.event.init(this);104
this.paint.init(this);105
this.modal.init(this);106
this.socket.init(this);107
let that = this;109
// events110
this.event.bind(this.node.canvas, {111
prevent: ['dragstart', 'contextmenu'],112
begin: ['mousedown', 'touchstart'],113
drag: ['mousemove', 'touchmove']114
});115
this.event.bind(document, {116
move: ['mousemove', 'touchmove'],117
end: ['mouseup', 'touchend', 'mouseleave'],118
key: 'keydown',119
out: 'mouseout',120
scroll: { type: 'wheel', passive: false }121
});122
this.event.bind(window, {123
resize: 'resize'124
});125
this.event.bind(this.node.bucket, {126
button: 'mousedown'127
});128
this.event.resize();130
this.modal.get('color').action.open.before = function(modal) {131
for(let item of that.node.color) {132
if(that.state.color === item.dataset.color) {133
item.classList.add('active');134
}135
else {136
item.classList.remove('active');137
}138
}139
}141
this.modal.get('join').element.querySelector('.action-uuid').focus();142
this.event.action(this.modal.get('join').action.uuid, 'keydown', function(event) {143
if(event.keyCode === 13) {144
that.state.name = that.modal.get('join').element.querySelector('.action-uuid').value.replace(/\s+/g, '');145
if(that.state.name.length >= 3 && that.state.name.length <= 20) {146
that.state.uuid = that.state.name + '-' + that.util.uuid();147
that.socket.connect();148
that.socket.cursor();149
}150
}151
});152
this.event.action(this.modal.get('join').action.join, 'click', function(event) {153
that.state.name = that.modal.get('join').element.querySelector('.action-uuid').value.replace(/\s+/g, '');154
if(that.state.name.length >= 3 && that.state.name.length <= 20) {155
that.state.uuid = that.state.name + '-' + that.util.uuid();156
that.socket.connect();157
that.socket.cursor();158
}159
});160
this.modal.get('scaling').action.open.before = function(modal) {161
modal.element.querySelector('input.action-range').value = that.state.radius;162
let circle = modal.element.querySelector('.circle');163
circle.style.width = that.state.radius * 4 + 'px';164
circle.style.height = that.state.radius * 4 + 'px';165
}166
this.event.action(this.modal.get('scaling').action.range, 'input', function(event) {167
setTimeout(function() {168
let circle = that.modal.get('scaling').element.querySelector('.modal .circle');169
circle.style.width = event.target.value * 4 + 'px';170
circle.style.height = event.target.value * 4 + 'px';171
}, 10);172
});173
this.event.action(this.modal.get('scaling').action.scaling, 'click', function(event) {174
that.state.radius = parseInt(that.modal.get('scaling').element.querySelector('.modal input.action-range').value);175
that.socket.cursor();176
that.update();177
that.modal.close();178
});179
this.event.action(this.modal.get('clear').action.clear, 'click', function(event) {180
that.paint.g.fillStyle = 'white';181
that.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);182
that.socket.clear();183
that.modal.close();184
});185
this.modal.get('save').action.open.before = function(modal) {186
modal.element.querySelector('input.action-file').value = 'outline-image-' + that.util.uuid();187
}188
this.event.action(this.modal.get('save').action.save, 'click', function(event) {189
let file = that.modal.get('save').element.querySelector('.modal input.action-file').value.replace(/\s+/g, '');190
if(file.length >= 3 && file.length <= 40) {191
that.util.download(file, that.node.canvas.toDataURL('image/jpg'));192
that.modal.close();193
}194
});195
for(let item of this.node.color) {196
item.addEventListener('click', function(event) {197
that.state.color = item.dataset.color;198
for(let _item of that.node.bucket) {199
_item.classList.remove('active');200
}201
that.modal.close();202
});203
}204
this.event.action(this.modal.get('error').action.reconnect, 'click', function(event) {205
that.modal.load();206
that.socket.reconnect();207
});208
},209
update: function() {210
this.paint.o.clearRect(0, 0, window.innerWidth, window.innerHeight);211
this.paint.cursor(this.state);212
for(let client of Object.values(this.state.client)) {213
this.paint.cursor(client);214
}215
},216
verify: function() {217
return atob(GOOGLE_API_KEY);218
}219
};