so://phie

outline

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

blob: 751bd200f4c557d161b61c6a2d2d996d17aa3f18 (3880 bytes)[doc][blame][raw]

1const DEVICE_PIXEL_RATIO = window.devicePixelRatio || 1;

2

3import { COLORS } from '../constant.js';

4

5let active;

6let point_list = [];

7let width;

8let height;

9let ratio;

10let backing_store_ratio;

11

12let canvas;

13let overlay;

14let memory;

15let g;

16let o;

17let m;

18

19function init() {

20 width = window.innerWidth;

21 height = window.innerHeight;

22 canvas = document.querySelector('#canvas');

23 overlay = document.querySelector('#overlay');

24 memory = document.createElement('canvas');

25 g = canvas.getContext('2d');

26 o = overlay.getContext('2d');

27 m = memory.getContext('2d');

28 resize();

29 window.addEventListener('resize', resize);

30}

31

32function get_canvas() {

33 return canvas;

34}

35

36function resize() {

37 width = window.innerWidth;

38 height = window.innerHeight;

39 backing_store_ratio = (

40 g.webkitBackingStorePixelRatio ||

41 g.mozBackingStorePixelRatio ||

42 g.msBackingStorePixelRatio ||

43 g.oBackingStorePixelRatio ||

44 g.backingStorePixelRatio || 1

45 )

46 ratio = DEVICE_PIXEL_RATIO / backing_store_ratio;

47 let image_data;

48 image_data = g.getImageData(0, 0, width * ratio, height * ratio);

49 scale_canvas(canvas, g);

50 g.putImageData(image_data, 0, 0);

51 g.imageSmoothingEnabled = false;

52 image_data = o.getImageData(0, 0, width * ratio, height * ratio);

53 scale_canvas(overlay, o);

54 o.putImageData(image_data, 0, 0);

55 o.imageSmoothingEnabled = false;

56 image_data = m.getImageData(0, 0, width * ratio, height * ratio);

57 scale_canvas(memory, m);

58 m.putImageData(image_data, 0, 0);

59 m.imageSmoothingEnabled = false;

60}

61

62function scale_canvas(canvas, g) {

63 if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {

64 canvas.width = width * ratio;

65 canvas.height = height * ratio;

66 canvas.style.width = width + 'px';

67 canvas.style.height = height + 'px';

68 }

69 else {

70 canvas.width = width;

71 canvas.height = height;

72 canvas.style.width = '';

73 canvas.style.height = '';

74 }

75 g.scale(ratio, ratio);

76}

77

78function clear() {

79 g.clearRect(0, 0, width, height);

80 m.clearRect(0, 0, width, height);

81}

82

83function on_press(event, state) {

84 point_list.push({

85 x: event.layerX,

86 y: event.layerY

87 });

88 active = true;

89}

90

91function on_move(event, state) {

92 if(active) {

93 g.clearRect(0, 0, width, height);

94 g.drawImage(memory, 0, 0, width, height);

95 point_list.push({

96 x: event.layerX,

97 y: event.layerY

98 });

99 draw_curve(state);

100 }

101}

102

103function on_release(event) {

104 if(active) {

105 if(event && event.type === 'pointerup') {

106 active = false;

107 }

108 m.clearRect(0, 0, width, height);

109 m.drawImage(canvas, 0, 0, width, height);

110 point_list = [];

111 }

112}

113

114function draw_curve(state) {

115 g.lineJoin = 'round';

116 g.lineCap = 'round';

117 g.lineWidth = 2 * state.radius;

118 g.strokeStyle = COLORS[state.color] + '';

119 if(point_list.length < 3) {

120 g.fillStyle = COLORS[state.color] + '';

121 g.beginPath();

122 g.arc(point_list[0].x, point_list[0].y, g.lineWidth / 2, 0, Math.PI * 2, !0);

123 g.closePath();

124 g.fill();

125 return;

126 }

127 g.beginPath();

128 g.moveTo(point_list[0].x, point_list[0].y);

129 let i;

130 for(i = 1; i < point_list.length - 2; i++) {

131 let dx = (point_list[i].x + point_list[i + 1].x);

132 let dy = (point_list[i].y + point_list[i + 1].y);

133 g.quadraticCurveTo(point_list[i].x, point_list[i].y, parseInt(dx / 2), parseInt(dy / 2));

134 }

135 g.quadraticCurveTo(point_list[i].x, point_list[i].y, point_list[i + 1].x, point_list[i + 1].y);

136 g.stroke();

137}

138

139function draw_cursor(state) {

140 o.clearRect(0, 0, window.innerWidth, window.innerHeight);

141 if(state.point) {

142 if(!active) {

143 o.fillStyle = COLORS[state.color] + '';

144 o.beginPath();

145 o.arc(state.point.x, state.point.y, 2 * state.radius / 2, 0, Math.PI * 2, !0);

146 o.closePath();

147 o.fill();

148 }

149 o.strokeStyle = 'white';

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.stroke();

154 }

155}

156

157export default {

158 init,

159 get_canvas,

160 resize,

161 scale_canvas,

162 clear,

163 on_press,

164 on_move,

165 on_release,

166 draw_curve,

167 draw_cursor

168};