so://phie

outline

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

blob: c26a3f9678873eac145a5c4dca5879503cb1f177 (4721 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 size;

10let ratio;

11let backing_store_ratio;

12

13let canvas;

14let overlay;

15let memory;

16let g;

17let o;

18let m;

19

20function 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}

32

33function get_canvas() {

34 return canvas;

35}

36

37function 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 || 1

50 )

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}

66

67function 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}

82

83function clear() {

84 g.clearRect(0, 0, size, size);

85 m.clearRect(0, 0, size, size);

86}

87

88function on_press(event, state) {

89 point_list.push({

90 x: event.layerX,

91 y: event.layerY

92 });

93 active = true;

94}

95

96function 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.layerY

103 });

104 draw_curve(state);

105 }

106}

107

108function 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}

118

119function 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}

151

152function 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 = '#E28787';

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}

187

188function clear_cursor() {

189 o.clearRect(0, 0, size, size);

190}

191

192export 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_cursor

204};