A web-based drawing app
git clone git://soophie.de/outline
blob: ab056d2710dccb345a17e8e46724aac8b1283d40 (3915 bytes)[doc][blame][raw]
1
const DEVICE_PIXEL_RATIO = window.devicePixelRatio || 1;3
import { COLORS } from '../constant.js';5
let active;6
let point_list = [];7
let width;8
let height;9
let size;10
let ratio;11
let backing_store_ratio;13
let canvas;14
let overlay;15
let memory;16
let g;17
let o;18
let m;20
function init() {21
width = window.innerWidth;22
height = window.innerHeight;23
canvas = document.querySelector('#canvas');24
overlay = document.querySelector('#overlay');25
memory = document.createElement('canvas');26
g = canvas.getContext('2d');27
o = overlay.getContext('2d');28
m = memory.getContext('2d');29
resize();30
window.addEventListener('resize', resize);31
}33
function get_canvas() {34
return canvas;35
}37
function resize() {38
width = window.innerWidth;39
height = window.innerHeight;40
let max = width > height ? width : height;41
if(!size || max > size) {42
size = max;43
}44
backing_store_ratio = (45
g.webkitBackingStorePixelRatio ||46
g.mozBackingStorePixelRatio ||47
g.msBackingStorePixelRatio ||48
g.oBackingStorePixelRatio ||49
g.backingStorePixelRatio || 150
)51
ratio = DEVICE_PIXEL_RATIO / backing_store_ratio;52
let image_data;53
image_data = g.getImageData(0, 0, size * ratio, size * ratio);54
scale_canvas(canvas, g);55
g.putImageData(image_data, 0, 0);56
g.imageSmoothingEnabled = false;57
image_data = o.getImageData(0, 0, size * ratio, size * ratio);58
scale_canvas(overlay, o);59
o.putImageData(image_data, 0, 0);60
o.imageSmoothingEnabled = false;61
image_data = m.getImageData(0, 0, size * ratio, size * ratio);62
scale_canvas(memory, m);63
m.putImageData(image_data, 0, 0);64
m.imageSmoothingEnabled = false;65
}67
function scale_canvas(canvas, g) {68
if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {69
canvas.width = size * ratio;70
canvas.height = size * ratio;71
canvas.style.width = size + 'px';72
canvas.style.height = size + 'px';73
}74
else {75
canvas.width = size;76
canvas.height = size;77
canvas.style.width = '';78
canvas.style.height = '';79
}80
g.scale(ratio, ratio);81
}83
function clear() {84
g.clearRect(0, 0, size, size);85
m.clearRect(0, 0, size, size);86
}88
function on_press(event, state) {89
point_list.push({90
x: event.layerX,91
y: event.layerY92
});93
active = true;94
}96
function on_move(event, state) {97
if(active) {98
g.clearRect(0, 0, size, size);99
g.drawImage(memory, 0, 0, size, size);100
point_list.push({101
x: event.layerX,102
y: event.layerY103
});104
draw_curve(state);105
}106
}108
function on_release(event) {109
if(active) {110
if(event && event.type === 'pointerup') {111
active = false;112
}113
m.clearRect(0, 0, size, size);114
m.drawImage(canvas, 0, 0, size, size);115
point_list = [];116
}117
}119
function draw_curve(state) {120
g.lineJoin = 'round';121
g.lineCap = 'round';122
g.lineWidth = 2 * state.radius;123
g.strokeStyle = COLORS[state.color] + '';124
if(point_list.length < 3) {125
g.fillStyle = COLORS[state.color] + '';126
g.beginPath();127
g.arc(point_list[0].x, point_list[0].y, g.lineWidth / 2, 0, Math.PI * 2, !0);128
g.closePath();129
g.fill();130
return;131
}132
g.beginPath();133
g.moveTo(point_list[0].x, point_list[0].y);134
let i;135
for(i = 1; i < point_list.length - 2; i++) {136
let dx = (point_list[i].x + point_list[i + 1].x);137
let dy = (point_list[i].y + point_list[i + 1].y);138
g.quadraticCurveTo(point_list[i].x, point_list[i].y, parseInt(dx / 2), parseInt(dy / 2));139
}140
g.quadraticCurveTo(point_list[i].x, point_list[i].y, point_list[i + 1].x, point_list[i + 1].y);141
g.stroke();142
}144
function draw_cursor(state) {145
o.clearRect(0, 0, size, size);146
if(state.point) {147
if(!active) {148
o.fillStyle = COLORS[state.color] + '';149
o.beginPath();150
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);151
o.closePath();152
o.fill();153
}154
o.strokeStyle = 'white';155
o.beginPath();156
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);157
o.closePath();158
o.stroke();159
}160
}162
export default {163
init,164
get_canvas,165
resize,166
scale_canvas,167
clear,168
on_press,169
on_move,170
on_release,171
draw_curve,172
draw_cursor173
};