so://phie

outline

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

blob: 5ac52e11f652da5c33ba765b5c357ca8bc13c248 (6020 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 draw_curve(g, state, point_list);

106}

107

108function on_move(event, state) {

109 if(active) {

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

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

112 point_list.push({

113 x: event.layerX,

114 y: event.layerY

115 });

116 draw_curve(g, state, point_list);

117 }

118}

119

120function on_release(event, state, undrag) {

121 if(active) {

122 if((event && event.type === 'pointerup') || undrag) {

123 active = false;

124 }

125 if(point_list.length > 0) {

126 state.history.push({

127 list: point_list,

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

129 });

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

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

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

133 }

134 }

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

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

137 point_list = [];

138 }

139}

140

141function undo(state) {

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

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

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

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

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

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

148 for(let line of state.history) {

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

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

151 }

152 }

153}

154

155function redo(state) {

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

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

158 state.history.push(line);

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

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

161 }

162}

163

164function draw_curve(g, state, point_list) {

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

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

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

168 }

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

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

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

172 }

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

174 g.strokeStyle = 'white';

175 g.fillStyle = 'white';

176 }

177 g.lineCap = 'round';

178 g.lineJoin = 'round';

179 g.lineWidth = 2 * state.radius;

180 if(point_list.length < 3) {

181 g.beginPath();

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

183 g.closePath();

184 g.fill();

185 return;

186 }

187 g.beginPath();

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

189 let i;

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

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

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

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

194 }

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

196 g.stroke();

197}

198

199function draw_cursor(state) {

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

201 o.lineWidth = 2;

202 if(state.point) {

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

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

205 }

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

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

208 }

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

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

211 }

212 o.beginPath();

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

214 o.closePath();

215 o.fill();

216 o.strokeStyle = 'white';

217 o.beginPath();

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

219 o.closePath();

220 o.stroke();

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

222 o.strokeStyle = 'white';

223 o.beginPath();

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

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

226 o.stroke();

227 o.beginPath();

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

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

230 o.stroke();

231 }

232 }

233}

234

235function clear_cursor() {

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

237}

238

239export default {

240 init,

241 get_canvas,

242 resize,

243 scale_canvas,

244 clear,

245 on_press,

246 on_move,

247 on_release,

248 undo,

249 redo,

250 draw_curve,

251 draw_cursor,

252 clear_cursor

253};