A web-based drawing app
git clone git://soophie.de/outline
blob: bbce0af1e87d0a73b6378618fd224131e4dbca91 (7834 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, height) {84
if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {85
canvas.width = size * ratio;86
canvas.height = (height || size) * ratio;87
canvas.style.width = size + 'px';88
canvas.style.height = (height || size) + 'px';89
}90
else {91
canvas.width = size;92
canvas.height = 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
let { color, radius, pencil } = state;136
state.history.push({137
list: point_list,138
state: {139
color,140
radius,141
pencil142
}143
});144
if(state.history.length > MAX_HISTORY_STEP) {145
let line = state.history.shift();146
draw_curve(t, line.state, line.list);147
}148
}149
m.clearRect(0, 0, size, size);150
m.drawImage(canvas, 0, 0, size, size);151
point_list = [];152
}153
}155
function undo(state) {156
if(state.history.length > 0) {157
state.redo_history.push(state.history.pop());158
g.clearRect(0, 0, size, size);159
m.clearRect(0, 0, size, size);160
g.drawImage(temporary, 0, 0, size, size);161
m.drawImage(temporary, 0, 0, size, size);162
for(let line of state.history) {163
draw_curve(g, line.state, line.list);164
draw_curve(m, line.state, line.list);165
}166
}167
}169
function redo(state) {170
if(state.redo_history.length > 0) {171
let line = state.redo_history.pop();172
state.history.push(line);173
draw_curve(g, line.state, line.list);174
draw_curve(m, line.state, line.list);175
}176
}178
function draw_curve(g, state, point_list) {179
if(state.pencil === 'pen') {180
g.strokeStyle = COLORS[state.color];181
g.fillStyle = COLORS[state.color];182
}183
if(state.pencil === 'marker') {184
g.strokeStyle = COLORS[state.color] + '88';185
g.fillStyle = COLORS[state.color] + '88';186
}187
if(state.pencil === 'eraser') {188
g.strokeStyle = 'white';189
g.fillStyle = 'white';190
}191
g.lineCap = 'round';192
g.lineJoin = 'round';193
g.lineWidth = 2 * state.radius;194
if(point_list.length < 3) {195
g.beginPath();196
g.arc(point_list[0].x, point_list[0].y, g.lineWidth / 2, 0, Math.PI * 2, !0);197
g.closePath();198
g.fill();199
return;200
}201
g.beginPath();202
g.moveTo(point_list[0].x, point_list[0].y);203
let i;204
for(i = 1; i < point_list.length - 2; i++) {205
let dx = (point_list[i].x + point_list[i + 1].x);206
let dy = (point_list[i].y + point_list[i + 1].y);207
g.quadraticCurveTo(point_list[i].x, point_list[i].y, parseInt(dx / 2), parseInt(dy / 2));208
}209
g.quadraticCurveTo(point_list[i].x, point_list[i].y, point_list[i + 1].x, point_list[i + 1].y);210
g.stroke();211
}213
function draw_cursor(state) {214
o.clearRect(0, 0, size, size);215
o.lineWidth = 2;216
if(state.point) {217
if(state.pencil === 'pen') {218
o.fillStyle = COLORS[state.color];219
}220
if(state.pencil === 'marker') {221
o.fillStyle = COLORS[state.color] + '88';222
}223
if(state.pencil === 'eraser') {224
o.fillStyle = COLORS[state.color] + '55';225
}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.fill();230
o.strokeStyle = 'white';231
o.beginPath();232
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);233
o.closePath();234
o.stroke();235
if(state.pencil === 'eraser') {236
o.strokeStyle = 'white';237
o.beginPath();238
o.moveTo(state.point.x - state.radius * 0.5, state.point.y);239
o.lineTo(state.point.x + state.radius * 0.5, state.point.y);240
o.stroke();241
o.beginPath();242
o.moveTo(state.point.x, state.point.y - state.radius * 0.5);243
o.lineTo(state.point.x, state.point.y + state.radius * 0.5);244
o.stroke();245
}246
}247
}249
function draw_crop(state) {250
o.clearRect(0, 0, size, size);251
if(state.option.crop_mode) {252
o.fillStyle = 'rgba(0, 0, 0, 0.3)';253
o.beginPath();254
o.rect(0, 0, size, size);255
o.fill();256
if(state.region && state.region.length == 2) {257
let [ begin, end ] = state.region;258
if(begin && end) {259
let delta = {260
x: end.x - begin.x,261
y: end.y - begin.y262
};263
if(delta.x != 0 && delta.y != 0) {264
o.clearRect(begin.x, begin.y, delta.x, delta.y);265
o.strokeStyle = 'white';266
o.beginPath();267
o.rect(begin.x, begin.y, delta.x, delta.y);268
o.stroke();269
}270
}271
}272
}273
}275
function clear_cursor() {276
o.clearRect(0, 0, size, size);277
}279
function set_data(data) {280
let image = new Image();281
image.onload = function(event) {282
g.drawImage(image, 0, 0);283
m.drawImage(image, 0, 0);284
t.drawImage(image, 0, 0);285
}286
image.src = data;287
}289
function get_data() {290
return canvas.toDataURL();291
}293
function create_capture(x, y, width, height, offset) {294
if(!offset) {295
offset = 0;296
}297
let cache = document.createElement('canvas');298
let c = cache.getContext('2d');299
scale_canvas(cache, c, width * ratio, (height - offset) * ratio);300
c.translate(-x * ratio, -(y * ratio + offset));301
c.fillStyle = 'white';302
c.fillRect(0, 0, window.innerWidth * ratio, window.innerHeight * ratio);303
c.drawImage(canvas, 0, 0 - offset);304
let data = cache.toDataURL() || null;305
cache.remove();306
return data;307
}309
export default {310
init,311
get_canvas,312
resize,313
scale_canvas,314
clear,315
on_press,316
on_move,317
on_release,318
undo,319
redo,320
draw_curve,321
draw_cursor,322
draw_crop,323
clear_cursor,324
set_data,325
get_data,326
create_capture327
};