A web-based drawing app
git clone git://soophie.de/outline
blob: 2bf8875846bc8deb20acf7b9e426fe7426a5d71b (6612 bytes)[doc][blame][raw]
1
const DEVICE_PIXEL_RATIO = window.devicePixelRatio || 1;2
const MAX_HISTORY_STEP = 10;4
import { COLORS } from '../constant.js';6
let active;7
let point_list = [];8
let width;9
let height;10
let size;11
let last;12
let ratio;13
let backing_store_ratio;15
let canvas;16
let overlay;17
let memory;18
let temporary;19
let g;20
let o;21
let m;22
let t;24
function init() {25
width = window.innerWidth;26
height = window.innerHeight;27
canvas = document.querySelector('#canvas');28
overlay = document.querySelector('#overlay');29
memory = document.createElement('canvas');30
temporary = document.createElement('canvas');31
g = canvas.getContext('2d');32
o = overlay.getContext('2d');33
m = memory.getContext('2d');34
t = temporary.getContext('2d');35
resize();36
window.addEventListener('resize', resize);37
}39
function get_canvas() {40
return canvas;41
}43
function resize() {44
width = window.innerWidth;45
height = window.innerHeight;46
let max = width > height ? width : height;47
if(!size || max > size) {48
last = size;49
size = max;50
}51
else {52
last = null;53
}54
backing_store_ratio = (55
g.webkitBackingStorePixelRatio ||56
g.mozBackingStorePixelRatio ||57
g.msBackingStorePixelRatio ||58
g.oBackingStorePixelRatio ||59
g.backingStorePixelRatio || 160
)61
ratio = DEVICE_PIXEL_RATIO / backing_store_ratio;62
let buffer = document.createElement('canvas');63
let b = buffer.getContext('2d');64
scale_canvas(buffer, b, last || size);65
b.imageSmoothingEnabled = false;66
b.clearRect(0, 0, size, size);67
b.drawImage(canvas, 0, 0, last || size, last || size);68
scale_canvas(canvas, g, size);69
g.imageSmoothingEnabled = false;70
g.drawImage(buffer, 0, 0, last || size, last || size);71
scale_canvas(overlay, o, size);72
o.imageSmoothingEnabled = false;73
scale_canvas(memory, m, size);74
m.imageSmoothingEnabled = false;75
m.drawImage(canvas, 0, 0, last || size, last || size);76
b.clearRect(0, 0, size, size);77
b.drawImage(temporary, 0, 0, last || size, last || size);78
scale_canvas(temporary, t, size);79
t.imageSmoothingEnabled = false;80
t.drawImage(buffer, 0, 0, last || size, last || size);81
}83
function scale_canvas(canvas, g, size) {84
if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {85
canvas.width = size * ratio;86
canvas.height = size * ratio;87
canvas.style.width = size + 'px';88
canvas.style.height = size + 'px';89
}90
else {91
canvas.width = size;92
canvas.height = size;93
canvas.style.width = '';94
canvas.style.height = '';95
}96
g.scale(ratio, ratio);97
}99
function clear(state) {100
g.clearRect(0, 0, size, size);101
m.clearRect(0, 0, size, size);102
t.clearRect(0, 0, size, size);103
state.history = [];104
state.redo_history = [];105
}107
function on_press(event, state) {108
point_list.push({109
x: event.layerX,110
y: event.layerY111
});112
active = true;113
state.redo_history = [];114
draw_curve(g, state, point_list);115
}117
function on_move(event, state) {118
if(active) {119
g.clearRect(0, 0, size, size);120
g.drawImage(memory, 0, 0, size, size);121
point_list.push({122
x: event.layerX,123
y: event.layerY124
});125
draw_curve(g, state, point_list);126
}127
}129
function on_release(event, state, undrag) {130
if(active) {131
if((event && event.type === 'pointerup') || undrag) {132
active = false;133
}134
if(point_list.length > 0) {135
state.history.push({136
list: point_list,137
state: Object.assign({}, state)138
});139
if(state.history.length > MAX_HISTORY_STEP) {140
let line = state.history.shift();141
draw_curve(t, line.state, line.list);142
}143
}144
m.clearRect(0, 0, size, size);145
m.drawImage(canvas, 0, 0, size, size);146
point_list = [];147
}148
}150
function undo(state) {151
if(state.history.length > 0) {152
state.redo_history.push(state.history.pop());153
g.clearRect(0, 0, size, size);154
m.clearRect(0, 0, size, size);155
g.drawImage(temporary, 0, 0, size, size);156
m.drawImage(temporary, 0, 0, size, size);157
for(let line of state.history) {158
draw_curve(g, line.state, line.list);159
draw_curve(m, line.state, line.list);160
}161
}162
}164
function redo(state) {165
if(state.redo_history.length > 0) {166
let line = state.redo_history.pop();167
state.history.push(line);168
draw_curve(g, line.state, line.list);169
draw_curve(m, line.state, line.list);170
}171
}173
function draw_curve(g, state, point_list) {174
if(state.pencil === 'pen') {175
g.strokeStyle = COLORS[state.color];176
g.fillStyle = COLORS[state.color];177
}178
if(state.pencil === 'marker') {179
g.strokeStyle = COLORS[state.color] + '88';180
g.fillStyle = COLORS[state.color] + '88';181
}182
if(state.pencil === 'eraser') {183
g.strokeStyle = 'white';184
g.fillStyle = 'white';185
}186
g.lineCap = 'round';187
g.lineJoin = 'round';188
g.lineWidth = 2 * state.radius;189
if(point_list.length < 3) {190
g.beginPath();191
g.arc(point_list[0].x, point_list[0].y, g.lineWidth / 2, 0, Math.PI * 2, !0);192
g.closePath();193
g.fill();194
return;195
}196
g.beginPath();197
g.moveTo(point_list[0].x, point_list[0].y);198
let i;199
for(i = 1; i < point_list.length - 2; i++) {200
let dx = (point_list[i].x + point_list[i + 1].x);201
let dy = (point_list[i].y + point_list[i + 1].y);202
g.quadraticCurveTo(point_list[i].x, point_list[i].y, parseInt(dx / 2), parseInt(dy / 2));203
}204
g.quadraticCurveTo(point_list[i].x, point_list[i].y, point_list[i + 1].x, point_list[i + 1].y);205
g.stroke();206
}208
function draw_cursor(state) {209
o.clearRect(0, 0, size, size);210
o.lineWidth = 2;211
if(state.point) {212
if(state.pencil === 'pen') {213
o.fillStyle = COLORS[state.color];214
}215
if(state.pencil === 'marker') {216
o.fillStyle = COLORS[state.color] + '88';217
}218
if(state.pencil === 'eraser') {219
o.fillStyle = COLORS[state.color] + '55';220
}221
o.beginPath();222
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);223
o.closePath();224
o.fill();225
o.strokeStyle = 'white';226
o.beginPath();227
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);228
o.closePath();229
o.stroke();230
if(state.pencil === 'eraser') {231
o.strokeStyle = 'white';232
o.beginPath();233
o.moveTo(state.point.x - state.radius * 0.5, state.point.y);234
o.lineTo(state.point.x + state.radius * 0.5, state.point.y);235
o.stroke();236
o.beginPath();237
o.moveTo(state.point.x, state.point.y - state.radius * 0.5);238
o.lineTo(state.point.x, state.point.y + state.radius * 0.5);239
o.stroke();240
}241
}242
}244
function clear_cursor() {245
o.clearRect(0, 0, size, size);246
}248
function set_data(data) {249
let image = new Image();250
image.onload = function(event) {251
g.drawImage(image, 0, 0);252
m.drawImage(image, 0, 0);253
t.drawImage(image, 0, 0);254
}255
image.src = data;256
}258
function get_data() {259
return canvas.toDataURL();260
}262
export default {263
init,264
get_canvas,265
resize,266
scale_canvas,267
clear,268
on_press,269
on_move,270
on_release,271
undo,272
redo,273
draw_curve,274
draw_cursor,275
clear_cursor,276
set_data,277
get_data278
};