so://phie

outline

A web-based drawing app
git clone git://soophie.de/outline

blob: dc9292b13979fc8bdc4b772c90b3f787b14835cd (2108 bytes)[doc][blame][raw]

1import { luma } from './util.js';

2

3export default {

4 init: function(app) {

5 this.app = app;

6 this.g = this.app.node.canvas.getContext('2d');

7 this.o = this.app.node.overlay.getContext('2d');

8 },

9 draw: function(args) {

10 let { pos, last, radius, color } = args || {};

11 if(pos) {

12 if(last) {

13 let delta = {

14 x: last.x - pos.x,

15 y: last.y - pos.y

16 };

17 let dist = Math.sqrt(Math.pow(delta.x, 2) + Math.pow(delta.y, 2));

18 let length = dist / (radius / 2);

19 for(let i = 0; i < length; i++) {

20 let x = pos.x + (delta.x / length * i);

21 let y = pos.y + (delta.y / length * i);

22 this.g.fillStyle = color;

23 this.g.beginPath();

24 this.g.arc(x, y, 2 * radius, 0, 2 * Math.PI);

25 this.g.fill();

26 this.g.fillStyle = 'black';

27 }

28 }

29 else {

30 this.g.fillStyle = color;

31 this.g.beginPath();

32 this.g.arc(pos.x, pos.y, 2 * radius, 0, 2 * Math.PI);

33 this.g.fill();

34 this.g.fillStyle = 'black';

35 }

36 }

37 },

38 roundRect: function(x, y, w, h, r) {

39 if(w < 2 * r) r = w / 2;

40 if(h < 2 * r) r = h / 2;

41 this.o.beginPath();

42 this.o.moveTo(x + r, y);

43 this.o.arcTo(x + w, y, x + w, y + h, r);

44 this.o.arcTo(x + w, y + h, x, y + h, r);

45 this.o.arcTo(x, y + h, x, y, r);

46 this.o.arcTo(x, y, x + w, y, r);

47 },

48 cursor: function(state) {

49 if(state.cursor) {

50 this.o.fillStyle = state.color;

51 this.o.lineWidth = 4;

52 this.o.beginPath();

53 this.o.arc(state.cursor.x, state.cursor.y, 2 * state.radius, 0, 2 * Math.PI);

54 this.o.strokeStyle = luma(state.color) ? 'black' : 'white';

55 this.o.stroke();

56 this.o.beginPath();

57 this.o.arc(state.cursor.x, state.cursor.y, 2 * state.radius, 0, 2 * Math.PI);

58 this.o.fill();

59 if(this.app.state !== state) {

60 this.o.fillStyle = '#212121';

61 this.o.font = '14px Roboto';

62 this.o.textBaseline = 'top';

63 this.o.textAlign = 'left';

64 let text = this.o.measureText(state.name);

65 this.roundRect(state.cursor.x, state.cursor.y, text.width + 12, 22, 4);

66 this.o.fill();

67 this.o.fillStyle = 'white';

68 this.o.fillText(state.name, state.cursor.x + 6, state.cursor.y + 5);

69 }

70 }

71 }

72};