so://phie

outline

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

blob: b1d98e5b6458cc475d1d8e1663618f6350554f5b (5896 bytes)[doc][blame][raw]

1const DEVICE_PIXEL_RATIO = window.devicePixelRatio || 1;

2const MAX_HISTORY_STEP = 10;

3

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

5

6let active;

7let point_list = [];

8let width;

9let height;

10let size;

11let ratio;

12let backing_store_ratio;

13

14let canvas;

15let overlay;

16let memory;

17let temporary;

18let g;

19let o;

20let m;

21let t;

22

23function init() {

24 width = window.innerWidth;

25 height = window.innerHeight;

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

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

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

29 temporary = document.createElement('canvas');

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

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

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

33 t = temporary.getContext('2d');

34 resize();

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

36}

37

38function get_canvas() {

39 return canvas;

40}

41

42function resize() {

43 width = window.innerWidth;

44 height = window.innerHeight;

45 let max = width > height ? width : height;

46 if(!size || max > size) {

47 size = max;

48 }

49 backing_store_ratio = (

50 g.webkitBackingStorePixelRatio ||

51 g.mozBackingStorePixelRatio ||

52 g.msBackingStorePixelRatio ||

53 g.oBackingStorePixelRatio ||

54 g.backingStorePixelRatio || 1

55 )

56 ratio = DEVICE_PIXEL_RATIO / backing_store_ratio;

57 let buffer = document.createElement('canvas');

58 let b = buffer.getContext('2d');

59 scale_canvas(buffer, b);

60 b.imageSmoothingEnabled = false;

61 b.drawImage(canvas, 0, 0, size, size);

62 scale_canvas(canvas, g);

63 g.imageSmoothingEnabled = false;

64 g.drawImage(buffer, 0, 0, size, size);

65 scale_canvas(overlay, o);

66 o.imageSmoothingEnabled = false;

67 scale_canvas(memory, m);

68 m.imageSmoothingEnabled = false;

69 m.drawImage(canvas, 0, 0, size, size);

70 scale_canvas(temporary, t);

71 t.imageSmoothingEnabled = false;

72}

73

74function scale_canvas(canvas, g) {

75 if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {

76 canvas.width = size * ratio;

77 canvas.height = size * ratio;

78 canvas.style.width = size + 'px';

79 canvas.style.height = size + 'px';

80 }

81 else {

82 canvas.width = size;

83 canvas.height = size;

84 canvas.style.width = '';

85 canvas.style.height = '';

86 }

87 g.scale(ratio, ratio);

88}

89

90function clear(state) {

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

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

93 t.clearRect(0, 0, size, size);

94 state.history = [];

95 state.redo_history = [];

96}

97

98function on_press(event, state) {

99 point_list.push({

100 x: event.layerX,

101 y: event.layerY

102 });

103 active = true;

104 state.redo_history = [];

105}

106

107function on_move(event, state) {

108 if(active) {

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

110 g.drawImage(memory, 0, 0, size, size);

111 point_list.push({

112 x: event.layerX,

113 y: event.layerY

114 });

115 draw_curve(g, state, point_list);

116 }

117}

118

119function on_release(event, state) {

120 if(active) {

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

122 active = false;

123 }

124 if(point_list.length > 0) {

125 state.history.push({

126 list: point_list,

127 state: Object.assign({}, state)

128 });

129 if(state.history.length > MAX_HISTORY_STEP) {

130 let line = state.history.shift();

131 draw_curve(t, line.state, line.list);

132 }

133 }

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

135 m.drawImage(canvas, 0, 0, size, size);

136 point_list = [];

137 }

138}

139

140function undo(state) {

141 if(state.history.length > 0) {

142 state.redo_history.push(state.history.pop());

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

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

145 g.drawImage(temporary, 0, 0, size, size);

146 m.drawImage(temporary, 0, 0, size, size);

147 for(let line of state.history) {

148 draw_curve(g, line.state, line.list);

149 draw_curve(m, line.state, line.list);

150 }

151 }

152}

153

154function redo(state) {

155 if(state.redo_history.length > 0) {

156 let line = state.redo_history.pop();

157 state.history.push(line);

158 draw_curve(g, line.state, line.list);

159 draw_curve(m, line.state, line.list);

160 }

161}

162

163function draw_curve(g, state, point_list) {

164 if(state.pencil === 'pen') {

165 g.strokeStyle = COLORS[state.color];

166 }

167 if(state.pencil === 'marker') {

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

169 }

170 if(state.pencil === 'eraser') {

171 g.strokeStyle = 'white';

172 }

173 g.lineCap = 'round';

174 g.lineJoin = 'round';

175 g.lineWidth = 2 * state.radius;

176 if(point_list.length < 3) {

177 g.fillStyle = COLORS[state.color];

178 g.beginPath();

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

180 g.closePath();

181 g.fill();

182 return;

183 }

184 g.beginPath();

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

186 let i;

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

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

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

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

191 }

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

193 g.stroke();

194}

195

196function draw_cursor(state) {

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

198 o.lineWidth = 2;

199 if(state.point) {

200 if(state.pencil === 'pen') {

201 o.fillStyle = COLORS[state.color];

202 }

203 if(state.pencil === 'marker') {

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

205 }

206 if(state.pencil === 'eraser') {

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

208 }

209 o.beginPath();

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

211 o.closePath();

212 o.fill();

213 o.strokeStyle = 'white';

214 o.beginPath();

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

216 o.closePath();

217 o.stroke();

218 if(state.pencil === 'eraser') {

219 o.strokeStyle = 'white';

220 o.beginPath();

221 o.moveTo(state.point.x - state.radius * 0.5, state.point.y);

222 o.lineTo(state.point.x + state.radius * 0.5, state.point.y);

223 o.stroke();

224 o.beginPath();

225 o.moveTo(state.point.x, state.point.y - state.radius * 0.5);

226 o.lineTo(state.point.x, state.point.y + state.radius * 0.5);

227 o.stroke();

228 }

229 }

230}

231

232function clear_cursor() {

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

234}

235

236export default {

237 init,

238 get_canvas,

239 resize,

240 scale_canvas,

241 clear,

242 on_press,

243 on_move,

244 on_release,

245 undo,

246 redo,

247 draw_curve,

248 draw_cursor,

249 clear_cursor

250};