A web-based drawing app
git clone git://soophie.de/outline
blob: b1d98e5b6458cc475d1d8e1663618f6350554f5b (5896 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 ratio;12
let backing_store_ratio;14
let canvas;15
let overlay;16
let memory;17
let temporary;18
let g;19
let o;20
let m;21
let t;23
function init() {24
width = window.innerWidth;25
height = window.innerHeight;26
canvas = document.querySelector('#canvas');27
overlay = document.querySelector('#overlay');28
memory = document.createElement('canvas');29
temporary = document.createElement('canvas');30
g = canvas.getContext('2d');31
o = overlay.getContext('2d');32
m = memory.getContext('2d');33
t = temporary.getContext('2d');34
resize();35
window.addEventListener('resize', resize);36
}38
function get_canvas() {39
return canvas;40
}42
function resize() {43
width = window.innerWidth;44
height = window.innerHeight;45
let max = width > height ? width : height;46
if(!size || max > size) {47
size = max;48
}49
backing_store_ratio = (50
g.webkitBackingStorePixelRatio ||51
g.mozBackingStorePixelRatio ||52
g.msBackingStorePixelRatio ||53
g.oBackingStorePixelRatio ||54
g.backingStorePixelRatio || 155
)56
ratio = DEVICE_PIXEL_RATIO / backing_store_ratio;57
let buffer = document.createElement('canvas');58
let b = buffer.getContext('2d');59
scale_canvas(buffer, b);60
b.imageSmoothingEnabled = false;61
b.drawImage(canvas, 0, 0, size, size);62
scale_canvas(canvas, g);63
g.imageSmoothingEnabled = false;64
g.drawImage(buffer, 0, 0, size, size);65
scale_canvas(overlay, o);66
o.imageSmoothingEnabled = false;67
scale_canvas(memory, m);68
m.imageSmoothingEnabled = false;69
m.drawImage(canvas, 0, 0, size, size);70
scale_canvas(temporary, t);71
t.imageSmoothingEnabled = false;72
}74
function scale_canvas(canvas, g) {75
if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {76
canvas.width = size * ratio;77
canvas.height = size * ratio;78
canvas.style.width = size + 'px';79
canvas.style.height = size + 'px';80
}81
else {82
canvas.width = size;83
canvas.height = size;84
canvas.style.width = '';85
canvas.style.height = '';86
}87
g.scale(ratio, ratio);88
}90
function clear(state) {91
g.clearRect(0, 0, size, size);92
m.clearRect(0, 0, size, size);93
t.clearRect(0, 0, size, size);94
state.history = [];95
state.redo_history = [];96
}98
function on_press(event, state) {99
point_list.push({100
x: event.layerX,101
y: event.layerY102
});103
active = true;104
state.redo_history = [];105
}107
function on_move(event, state) {108
if(active) {109
g.clearRect(0, 0, size, size);110
g.drawImage(memory, 0, 0, size, size);111
point_list.push({112
x: event.layerX,113
y: event.layerY114
});115
draw_curve(g, state, point_list);116
}117
}119
function on_release(event, state) {120
if(active) {121
if(event && event.type === 'pointerup') {122
active = false;123
}124
if(point_list.length > 0) {125
state.history.push({126
list: point_list,127
state: Object.assign({}, state)128
});129
if(state.history.length > MAX_HISTORY_STEP) {130
let line = state.history.shift();131
draw_curve(t, line.state, line.list);132
}133
}134
m.clearRect(0, 0, size, size);135
m.drawImage(canvas, 0, 0, size, size);136
point_list = [];137
}138
}140
function undo(state) {141
if(state.history.length > 0) {142
state.redo_history.push(state.history.pop());143
g.clearRect(0, 0, size, size);144
m.clearRect(0, 0, size, size);145
g.drawImage(temporary, 0, 0, size, size);146
m.drawImage(temporary, 0, 0, size, size);147
for(let line of state.history) {148
draw_curve(g, line.state, line.list);149
draw_curve(m, line.state, line.list);150
}151
}152
}154
function redo(state) {155
if(state.redo_history.length > 0) {156
let line = state.redo_history.pop();157
state.history.push(line);158
draw_curve(g, line.state, line.list);159
draw_curve(m, line.state, line.list);160
}161
}163
function draw_curve(g, state, point_list) {164
if(state.pencil === 'pen') {165
g.strokeStyle = COLORS[state.color];166
}167
if(state.pencil === 'marker') {168
g.strokeStyle = COLORS[state.color] + '88';169
}170
if(state.pencil === 'eraser') {171
g.strokeStyle = 'white';172
}173
g.lineCap = 'round';174
g.lineJoin = 'round';175
g.lineWidth = 2 * state.radius;176
if(point_list.length < 3) {177
g.fillStyle = COLORS[state.color];178
g.beginPath();179
g.arc(point_list[0].x, point_list[0].y, g.lineWidth / 2, 0, Math.PI * 2, !0);180
g.closePath();181
g.fill();182
return;183
}184
g.beginPath();185
g.moveTo(point_list[0].x, point_list[0].y);186
let i;187
for(i = 1; i < point_list.length - 2; i++) {188
let dx = (point_list[i].x + point_list[i + 1].x);189
let dy = (point_list[i].y + point_list[i + 1].y);190
g.quadraticCurveTo(point_list[i].x, point_list[i].y, parseInt(dx / 2), parseInt(dy / 2));191
}192
g.quadraticCurveTo(point_list[i].x, point_list[i].y, point_list[i + 1].x, point_list[i + 1].y);193
g.stroke();194
}196
function draw_cursor(state) {197
o.clearRect(0, 0, size, size);198
o.lineWidth = 2;199
if(state.point) {200
if(state.pencil === 'pen') {201
o.fillStyle = COLORS[state.color];202
}203
if(state.pencil === 'marker') {204
o.fillStyle = COLORS[state.color] + '88';205
}206
if(state.pencil === 'eraser') {207
o.fillStyle = COLORS[state.color] + '55';208
}209
o.beginPath();210
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);211
o.closePath();212
o.fill();213
o.strokeStyle = 'white';214
o.beginPath();215
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);216
o.closePath();217
o.stroke();218
if(state.pencil === 'eraser') {219
o.strokeStyle = 'white';220
o.beginPath();221
o.moveTo(state.point.x - state.radius * 0.5, state.point.y);222
o.lineTo(state.point.x + state.radius * 0.5, state.point.y);223
o.stroke();224
o.beginPath();225
o.moveTo(state.point.x, state.point.y - state.radius * 0.5);226
o.lineTo(state.point.x, state.point.y + state.radius * 0.5);227
o.stroke();228
}229
}230
}232
function clear_cursor() {233
o.clearRect(0, 0, size, size);234
}236
export default {237
init,238
get_canvas,239
resize,240
scale_canvas,241
clear,242
on_press,243
on_move,244
on_release,245
undo,246
redo,247
draw_curve,248
draw_cursor,249
clear_cursor250
};