A web-based drawing app
git clone git://soophie.de/outline
blob: 7ac455db0d443640489bb9a5a9d14088bf7c1f85 (4738 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
if(state.pencil === 'pen') {121
g.strokeStyle = COLORS[state.color];122
}123
if(state.pencil === 'marker') {124
g.strokeStyle = COLORS[state.color] + '88';125
}126
if(state.pencil === 'eraser') {127
g.strokeStyle = 'white';128
}129
g.lineCap = 'round';130
g.lineJoin = 'round';131
g.lineWidth = 2 * state.radius;132
if(point_list.length < 3) {133
g.fillStyle = COLORS[state.color];134
g.beginPath();135
g.arc(point_list[0].x, point_list[0].y, g.lineWidth / 2, 0, Math.PI * 2, !0);136
g.closePath();137
g.fill();138
return;139
}140
g.beginPath();141
g.moveTo(point_list[0].x, point_list[0].y);142
let i;143
for(i = 1; i < point_list.length - 2; i++) {144
let dx = (point_list[i].x + point_list[i + 1].x);145
let dy = (point_list[i].y + point_list[i + 1].y);146
g.quadraticCurveTo(point_list[i].x, point_list[i].y, parseInt(dx / 2), parseInt(dy / 2));147
}148
g.quadraticCurveTo(point_list[i].x, point_list[i].y, point_list[i + 1].x, point_list[i + 1].y);149
g.stroke();150
}152
function draw_cursor(state) {153
o.clearRect(0, 0, size, size);154
o.lineWidth = 2;155
if(state.point) {156
if(state.pencil === 'pen') {157
o.fillStyle = COLORS[state.color];158
}159
if(state.pencil === 'marker') {160
o.fillStyle = COLORS[state.color] + '88';161
}162
if(state.pencil === 'eraser') {163
o.fillStyle = COLORS[state.color] + '55';164
}165
o.beginPath();166
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);167
o.closePath();168
o.fill();169
o.strokeStyle = 'white';170
o.beginPath();171
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);172
o.closePath();173
o.stroke();174
if(state.pencil === 'eraser') {175
o.strokeStyle = 'white';176
o.beginPath();177
o.moveTo(state.point.x - state.radius * 0.5, state.point.y);178
o.lineTo(state.point.x + state.radius * 0.5, state.point.y);179
o.stroke();180
o.beginPath();181
o.moveTo(state.point.x, state.point.y - state.radius * 0.5);182
o.lineTo(state.point.x, state.point.y + state.radius * 0.5);183
o.stroke();184
}185
}186
}188
function clear_cursor() {189
o.clearRect(0, 0, size, size);190
}192
export default {193
init,194
get_canvas,195
resize,196
scale_canvas,197
clear,198
on_press,199
on_move,200
on_release,201
draw_curve,202
draw_cursor,203
clear_cursor204
};