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