so://phie

outline

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

blob: 50e5c28e96b09592632537f8428246d419da0393 (7829 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 last;

12let ratio;

13let backing_store_ratio;

14

15let canvas;

16let overlay;

17let memory;

18let temporary;

19let g;

20let o;

21let m;

22let t;

23

24function init() {

25 width = window.innerWidth;

26 height = window.innerHeight;

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

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

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

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

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

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

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

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

35 resize();

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

37}

38

39function get_canvas() {

40 return canvas;

41}

42

43function resize() {

44 width = window.innerWidth;

45 height = window.innerHeight;

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

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

48 last = size;

49 size = max;

50 }

51 else {

52 last = null;

53 }

54 backing_store_ratio = (

55 g.webkitBackingStorePixelRatio ||

56 g.mozBackingStorePixelRatio ||

57 g.msBackingStorePixelRatio ||

58 g.oBackingStorePixelRatio ||

59 g.backingStorePixelRatio || 1

60 )

61 ratio = DEVICE_PIXEL_RATIO / backing_store_ratio;

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

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

64 scale_canvas(buffer, b, last || size);

65 b.imageSmoothingEnabled = false;

66 b.clearRect(0, 0, size, size);

67 b.drawImage(canvas, 0, 0, last || size, last || size);

68 scale_canvas(canvas, g, size);

69 g.imageSmoothingEnabled = false;

70 g.drawImage(buffer, 0, 0, last || size, last || size);

71 scale_canvas(overlay, o, size);

72 o.imageSmoothingEnabled = false;

73 scale_canvas(memory, m, size);

74 m.imageSmoothingEnabled = false;

75 m.drawImage(canvas, 0, 0, last || size, last || size);

76 b.clearRect(0, 0, size, size);

77 b.drawImage(temporary, 0, 0, last || size, last || size);

78 scale_canvas(temporary, t, size);

79 t.imageSmoothingEnabled = false;

80 t.drawImage(buffer, 0, 0, last || size, last || size);

81}

82

83function scale_canvas(canvas, g, size, height) {

84 if(DEVICE_PIXEL_RATIO !== backing_store_ratio) {

85 canvas.width = size * ratio;

86 canvas.height = (height || size) * ratio;

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

88 canvas.style.height = (height || size) + 'px';

89 }

90 else {

91 canvas.width = size;

92 canvas.height = height || size;

93 canvas.style.width = '';

94 canvas.style.height = '';

95 }

96 g.scale(ratio, ratio);

97}

98

99function clear(state) {

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

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

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

103 state.history = [];

104 state.redo_history = [];

105}

106

107function on_press(event, state) {

108 point_list.push({

109 x: event.layerX,

110 y: event.layerY

111 });

112 active = true;

113 state.redo_history = [];

114 draw_curve(g, state, point_list);

115}

116

117function on_move(event, state) {

118 if(active) {

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

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

121 point_list.push({

122 x: event.layerX,

123 y: event.layerY

124 });

125 draw_curve(g, state, point_list);

126 }

127}

128

129function on_release(event, state, undrag) {

130 if(active) {

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

132 active = false;

133 }

134 if(point_list.length > 0) {

135 let { color, radius, pencil } = state;

136 state.history.push({

137 list: point_list,

138 state: {

139 color,

140 radius,

141 pencil

142 }

143 });

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

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

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

147 }

148 }

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

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

151 point_list = [];

152 }

153}

154

155function undo(state) {

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

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

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

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

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

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

162 for(let line of state.history) {

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

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

165 }

166 }

167}

168

169function redo(state) {

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

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

172 state.history.push(line);

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

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

175 }

176}

177

178function draw_curve(g, state, point_list) {

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

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

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

182 }

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

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

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

186 }

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

188 g.strokeStyle = 'white';

189 g.fillStyle = 'white';

190 }

191 g.lineCap = 'round';

192 g.lineJoin = 'round';

193 g.lineWidth = 2 * state.radius;

194 if(point_list.length < 3) {

195 g.beginPath();

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

197 g.closePath();

198 g.fill();

199 return;

200 }

201 g.beginPath();

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

203 let i;

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

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

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

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

208 }

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

210 g.stroke();

211}

212

213function draw_cursor(state) {

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

215 o.lineWidth = 2;

216 if(state.point) {

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

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

219 }

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

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

222 }

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

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

225 }

226 o.beginPath();

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

228 o.closePath();

229 o.fill();

230 o.strokeStyle = 'white';

231 o.beginPath();

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

233 o.closePath();

234 o.stroke();

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

236 o.strokeStyle = 'white';

237 o.beginPath();

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

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

240 o.stroke();

241 o.beginPath();

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

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

244 o.stroke();

245 }

246 }

247}

248

249function draw_crop(state) {

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

251 if(state.option.crop_mode) {

252 o.fillStyle = 'rgba(0, 0, 0, 0.3)';

253 o.beginPath();

254 o.rect(0, 0, size, size);

255 o.fill();

256 if(state.region && state.region.length == 2) {

257 let [ begin, end ] = state.region;

258 if(begin && end) {

259 let delta = {

260 x: end.x - begin.x,

261 y: end.y - begin.y

262 };

263 if(delta.x != 0 && delta.y != 0) {

264 o.clearRect(begin.x, begin.y, delta.x, delta.y);

265 o.strokeStyle = 'white';

266 o.beginPath();

267 o.rect(begin.x, begin.y, delta.x, delta.y);

268 o.stroke();

269 }

270 }

271 }

272 }

273}

274

275function clear_cursor() {

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

277}

278

279function set_data(data) {

280 let image = new Image();

281 image.onload = function() {

282 g.drawImage(image, 0, 0);

283 m.drawImage(image, 0, 0);

284 t.drawImage(image, 0, 0);

285 }

286 image.src = data;

287}

288

289function get_data() {

290 return canvas.toDataURL();

291}

292

293function create_capture(x, y, width, height, offset) {

294 if(!offset) {

295 offset = 0;

296 }

297 let cache = document.createElement('canvas');

298 let c = cache.getContext('2d');

299 scale_canvas(cache, c, width * ratio, (height - offset) * ratio);

300 c.translate(-x * ratio, -(y * ratio + offset));

301 c.fillStyle = 'white';

302 c.fillRect(0, 0, window.innerWidth * ratio, window.innerHeight * ratio);

303 c.drawImage(canvas, 0, 0 - offset);

304 let data = cache.toDataURL() || null;

305 cache.remove();

306 return data;

307}

308

309export default {

310 init,

311 get_canvas,

312 resize,

313 scale_canvas,

314 clear,

315 on_press,

316 on_move,

317 on_release,

318 undo,

319 redo,

320 draw_curve,

321 draw_cursor,

322 draw_crop,

323 clear_cursor,

324 set_data,

325 get_data,

326 create_capture

327};