A web-based drawing app
git clone git://soophie.de/outline
blob: e327d3d0e1b1e9e714d1fea4f906cbc024ddc963 (2090 bytes)[doc][blame][raw]
1
export default {2
init: function(app) {3
this.app = app;4
this.g = this.app.node.canvas.getContext('2d');5
this.o = this.app.node.overlay.getContext('2d');6
},7
draw: function(args) {8
let { pos, last, radius, color } = args || {};9
if(pos) {10
if(last) {11
let delta = {12
x: last.x - pos.x,13
y: last.y - pos.y14
};15
let dist = Math.sqrt(Math.pow(delta.x, 2) + Math.pow(delta.y, 2), 2);16
let length = dist / (radius / 2);17
for(let i = 0; i < length; i++) {18
let x = pos.x + (delta.x / length * i);19
let y = pos.y + (delta.y / length * i);20
this.g.fillStyle = color;21
this.g.beginPath();22
this.g.arc(x, y, 2 * radius, 0, 2 * Math.PI);23
this.g.fill();24
this.g.fillStyle = 'black';25
}26
}27
else {28
this.g.fillStyle = color;29
this.g.beginPath();30
this.g.arc(pos.x, pos.y, 2 * radius, 0, 2 * Math.PI);31
this.g.fill();32
this.g.fillStyle = 'black';33
}34
}35
},36
roundRect: function(x, y, w, h, r) {37
if(w < 2 * r) r = w / 2;38
if(h < 2 * r) r = h / 2;39
this.o.beginPath();40
this.o.moveTo(x + r, y);41
this.o.arcTo(x + w, y, x + w, y + h, r);42
this.o.arcTo(x + w, y + h, x, y + h, r);43
this.o.arcTo(x, y + h, x, y, r);44
this.o.arcTo(x, y, x + w, y, r);45
},46
cursor: function(state) {47
if(state.cursor) {48
this.o.fillStyle = state.color;49
this.o.lineWidth = 4;50
this.o.beginPath();51
this.o.arc(state.cursor.x, state.cursor.y, 2 * state.radius, 0, 2 * Math.PI);52
this.o.strokeStyle = this.app.util.luma(state.color) ? 'black' : 'white';53
this.o.stroke();54
this.o.beginPath();55
this.o.arc(state.cursor.x, state.cursor.y, 2 * state.radius, 0, 2 * Math.PI);56
this.o.fill();57
if(this.app.state !== state) {58
this.o.fillStyle = '#212121';59
this.o.font = '14px Roboto';60
this.o.textBaseline = 'top';61
this.o.textAlign = 'left';62
let text = this.o.measureText(state.name);63
this.roundRect(state.cursor.x, state.cursor.y, text.width + 12, 22, 4);64
this.o.fill();65
this.o.fillStyle = 'white';66
this.o.fillText(state.name, state.cursor.x + 6, state.cursor.y + 5);67
}68
}69
}70
};