A web-based drawing app
git clone git://soophie.de/outline
blob: bdd146cefa0492ac9a65af5578f4995f0f8f59d1 (4001 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
o.clearRect(0, 0, window.innerWidth, window.innerHeight);66
}68
function scale_canvas(canvas, g) {69
if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {70
canvas.width = size * ratio;71
canvas.height = size * ratio;72
canvas.style.width = size + 'px';73
canvas.style.height = size + 'px';74
}75
else {76
canvas.width = size;77
canvas.height = size;78
canvas.style.width = '';79
canvas.style.height = '';80
}81
g.scale(ratio, ratio);82
}84
function clear() {85
g.clearRect(0, 0, size, size);86
m.clearRect(0, 0, size, size);87
}89
function on_press(event, state) {90
point_list.push({91
x: event.layerX,92
y: event.layerY93
});94
active = true;95
}97
function on_move(event, state) {98
if(active) {99
g.clearRect(0, 0, size, size);100
g.drawImage(memory, 0, 0, size, size);101
point_list.push({102
x: event.layerX,103
y: event.layerY104
});105
draw_curve(state);106
}107
}109
function on_release(event) {110
if(active) {111
if(event && event.type === 'pointerup') {112
active = false;113
}114
m.clearRect(0, 0, size, size);115
m.drawImage(canvas, 0, 0, size, size);116
point_list = [];117
}118
}120
function draw_curve(state) {121
g.lineJoin = 'round';122
g.lineCap = 'round';123
g.lineWidth = 2 * state.radius;124
g.strokeStyle = COLORS[state.color] + '';125
if(point_list.length < 3) {126
g.fillStyle = COLORS[state.color] + '';127
g.beginPath();128
g.arc(point_list[0].x, point_list[0].y, g.lineWidth / 2, 0, Math.PI * 2, !0);129
g.closePath();130
g.fill();131
return;132
}133
g.beginPath();134
g.moveTo(point_list[0].x, point_list[0].y);135
let i;136
for(i = 1; i < point_list.length - 2; i++) {137
let dx = (point_list[i].x + point_list[i + 1].x);138
let dy = (point_list[i].y + point_list[i + 1].y);139
g.quadraticCurveTo(point_list[i].x, point_list[i].y, parseInt(dx / 2), parseInt(dy / 2));140
}141
g.quadraticCurveTo(point_list[i].x, point_list[i].y, point_list[i + 1].x, point_list[i + 1].y);142
g.stroke();143
}145
function draw_cursor(state) {146
o.clearRect(0, 0, window.innerWidth, window.innerHeight);147
if(state.point) {148
if(!active) {149
o.fillStyle = COLORS[state.color] + '';150
o.beginPath();151
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);152
o.closePath();153
o.fill();154
}155
o.strokeStyle = 'white';156
o.beginPath();157
o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);158
o.closePath();159
o.stroke();160
}161
}163
export default {164
init,165
get_canvas,166
resize,167
scale_canvas,168
clear,169
on_press,170
on_move,171
on_release,172
draw_curve,173
draw_cursor174
};