so://phie

outline

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

blob: f6b3bee72688f8e18516dda0a3536762fd2d332b (10623 bytes)[doc][blame][raw]

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

2import { uuid, query, prevent } from './util.js';

3

4// addons

5import canvas from './addon/canvas.js';

6import locale from './addon/locale.js';

7import ripple from './addon/ripple.js';

8// import gamepad from './addon/gamepad.js';

9

10let node;

11let state = {

12 color: 54,

13 color_list: [54, 2, 17, 27, 37, 12],

14 radius: 14,

15 index: 0,

16 device: null,

17 device_fallback: null,

18 device_list: {

19 mouse: false,

20 touch: false,

21 pen: false,

22 gamepad: false

23 },

24 timestamp: null,

25 modal: null,

26 tab: null

27};

28

29window.addEventListener('load', init);

30

31function init() {

32

33 canvas.init();

34 locale.init('en');

35 locale.load('../asset/lang', ['en', 'de']);

36 ripple.init();

37

38 node = query({

39 modal_list: { query: '.modal', all: true },

40 tab_list: { query: '.tab', all: true },

41 hotbar_list: { query: '.hotbar .color', all: true },

42 modal_color_list: { query: '.colors-modal .color', all: true },

43 language_list: { query: '.settings-modal .language-tab .item[data-event="change.language"]', all: true },

44 device_list: { query: '.settings-modal .device-tab .item[data-event="change.device"]', all: true },

45 modal_event_list: { query: '[data-event*=".modal"]', all: true },

46 tab_event_list: { query: '[data-event*=".tab"]', all: true },

47 tool_list: { query: '.tool', all: true },

48 action_list: { query: '.action', all: true },

49 notification: '.notification',

50 scale_input: 'input.scale[type="range"]',

51 clear: 'button.btn-apply[data-event="clear"]'

52 });

53

54 fill_hotbar_list();

55 fill_modal_color_list();

56

57 bind_events();

58}

59

60function bind_events() {

61 document.addEventListener('dragstart', prevent);

62 document.addEventListener('contextmenu', prevent);

63 document.addEventListener('pointerdown', on_pointerdown);

64 document.addEventListener('pointermove', on_pointermove);

65 document.addEventListener('pointerup', on_pointerup);

66 document.addEventListener('pointerout', on_pointerout);

67 document.addEventListener('keydown', on_keydown);

68 document.addEventListener('wheel', on_wheel);

69 document.addEventListener('wheel', function(event) {

70 if(event.ctrlKey) {

71 prevent(event);

72 }

73 }, { passive: false });

74 document.addEventListener('touchmove', function(event) {

75 if(event.target !== node.scale_input) {

76 event.preventDefault();

77 }

78 }, { passive: false });

79

80 for(let item of node.language_list) {

81 item.addEventListener('click', function(event) {

82 let code = item.dataset.code;

83 locale.change(code);

84 update_language_list();

85 });

86 }

87 for(let item of node.device_list) {

88 item.addEventListener('click', function(event) {

89 let code = item.dataset.code;

90 if(state.device_list[code]) {

91 state.device = code;

92 update_device_list();

93 }

94 });

95 }

96 for(let item of node.modal_event_list) {

97 item.addEventListener('click', function(event) {

98 let code = item.dataset.code;

99 let match = /^(toggle|open|close).modal$/.exec(item.dataset.event);

100 if(match) {

101 switch(match[1]) {

102 case 'toggle':

103 if(code) {

104 state.modal = state.modal !== code ? code : null;

105 update_modal_list();

106 }

107 break;

108 case 'open':

109 if(code && state.modal !== code) {

110 state.modal = code;

111 update_modal_list();

112 }

113 break;

114 case 'close':

115 if(state.modal !== null) {

116 state.modal = null;

117 update_modal_list();

118 }

119 break;

120 default:

121 break;

122 }

123 }

124 });

125 }

126 for(let item of node.tab_event_list) {

127 item.addEventListener('click', function(event) {

128 let code = item.dataset.code;

129 let match = /^(open|close).tab/.exec(item.dataset.event);

130 if(match) {

131 switch(match[1]) {

132 case 'open':

133 if(code && state.tab !== code) {

134 state.tab = code;

135 update_tab_list();

136 }

137 break;

138 case 'close':

139 if(state.tab !== null) {

140 state.tab = null;

141 update_tab_list();

142 }

143 break;

144 default:

145 break;

146 }

147 }

148 });

149 }

150 node.scale_input.addEventListener('input', function(event) {

151 state.radius = parseInt(node.scale_input.value);

152 state.point = {

153 x: parseInt(window.innerWidth / 2),

154 y: parseInt(window.innerHeight / 2)

155 };

156 canvas.draw_cursor(state);

157 });

158 node.clear.addEventListener('click', function(event) {

159 canvas.clear();

160 show_notification('notification.clear');

161 state.modal = null;

162 update_modal_list();

163 });

164}

165

166function update_language_list() {

167 let lang = locale.get_current();

168 for(let item of node.language_list) {

169 let code = item.dataset.code;

170 item.classList[code === lang ? 'add' : 'remove']('active');

171 }

172}

173

174function update_device_list() {

175 canvas.get_canvas().style.cursor = state.device === 'mouse' ? 'none' : '';

176 for(let item of node.device_list) {

177 let code = item.dataset.code;

178 item.classList[code === state.device ? 'add' : 'remove']('active');

179 item.classList[state.device_list[code] ? 'remove' : 'add']('inactive');

180 }

181}

182

183function update_hotbar() {

184 canvas.on_release(event);

185 for(let item of node.hotbar_list) {

186 item.classList[node.hotbar_list[state.index] === item ? 'add' : 'remove']('active');

187 }

188}

189

190function update_focus(bool) {

191 for(let item of node.hotbar_list) {

192 item.style.pointerEvents = bool ? '' : 'none';

193 }

194 for(let item of node.tool_list) {

195 item.style.pointerEvents = bool ? '' : 'none';

196 }

197 for(let item of node.action_list) {

198 item.style.pointerEvents = bool ? '' : 'none';

199 }

200}

201

202function update_modal_list() {

203 for(let item of node.modal_list) {

204 item.classList.add('hidden');

205 }

206 for(let item of node.tool_list) {

207 item.classList.remove('active');

208 }

209 for(let item of node.action_list) {

210 item.classList.remove('active');

211 }

212 state.tab = null;

213 update_tab_list();

214 if(state.modal) {

215 let modal = document.querySelector(`.modal.${state.modal}-modal`);

216 let button = document.querySelector(`[data-event*=".modal"][data-code="${state.modal}"]`);

217 if(modal) {

218 modal.classList.remove('hidden');

219 modal.animate([

220 { opacity: 0, marginTop: '-6px' },

221 { opacity: 1, marginTop: '0' }

222 ], {

223 duration: 100,

224 fill: 'both'

225 });

226 }

227 if(button) {

228 button.classList.add('active');

229 }

230 }

231}

232

233function update_tab_list() {

234 for(let item of node.modal_list) {

235 let main = item.querySelector('.main');

236 if(main) {

237 main.classList.remove('hidden');

238 main.style.marginLeft = '0';

239 }

240 }

241 for(let item of node.tab_list) {

242 item.classList.add('hidden');

243 }

244 if(state.modal && state.tab) {

245 let modal = document.querySelector(`.modal.${state.modal}-modal`);

246 let tab = document.querySelector(`.${state.tab}-tab`);

247 let main = modal.querySelector('.main');

248 if(modal && tab && main) {

249 tab.classList.remove('hidden');

250 main.style.marginLeft = '-100%';

251 main.classList.add('hidden');

252 }

253 }

254}

255

256function on_pointerdown(event) {

257 // detect touch

258 if(!state.device_list.touch && event.pointerType === 'touch') {

259 state.device_list.touch = true;

260 if(state.device) {

261 show_notification('notification.input-device.touch.add');

262 }

263 else {

264 state.device = 'touch';

265 state.device_fallback = 'touch';

266 }

267 update_device_list();

268 }

269 // detect pen

270 if(!state.device_list.pen && event.pointerType === 'pen') {

271 state.device_list.pen = true;

272 show_notification('notification.input-device.pencil.add');

273 if(!state.device) {

274 state.device = 'pen';

275 state.device_fallback = 'pen';

276 }

277 update_device_list();

278 }

279 // drawing

280 if(state.device === event.pointerType) {

281 if(event.target === canvas.get_canvas()) {

282 update_focus(false);

283 canvas.on_press(event, state);

284 state.point = {

285 x: event.layerX,

286 y: event.layerY

287 };

288 canvas.draw_cursor(state);

289 }

290 }

291}

292

293function on_pointermove(event) {

294 // detect mouse

295 if(!state.device_list.mouse && event.pointerType === 'mouse') {

296 state.device_list.mouse = true;

297 if(state.device) {

298 show_notification('notification.input-device.mouse.add');

299 }

300 else {

301 state.device = 'mouse';

302 state.device_fallback = 'mouse';

303 }

304 update_device_list();

305 }

306 // drawing

307 if(state.device === event.pointerType) {

308 if(event.target === canvas.get_canvas()) {

309 canvas.on_move(event, state);

310 state.point = {

311 x: event.layerX,

312 y: event.layerY

313 };

314 canvas.draw_cursor(state);

315 }

316 }

317}

318

319function on_pointerup(event) {

320 if(state.device === event.pointerType) {

321 canvas.on_release(event);

322 canvas.draw_cursor(state);

323 update_focus(true);

324 }

325}

326

327function on_pointerout(event) {

328 if(state.device === event.pointerType) {

329 canvas.on_release(event);

330 state.point = null;

331 canvas.draw_cursor(state);

332 }

333}

334

335function on_keydown(event) {

336 let lock = event.getModifierState("CapsLock");

337 if(!event.ctrlKey && !event.shiftKey && !event.altKey) {

338 if(!lock) {

339 if(event.code.startsWith('Digit')) {

340 let i = parseInt(event.code.substr(5)) - 1;

341 if(state.index !== i) {

342 if(i >= 0 && i < state.color_list.length) {

343 state.color = state.color_list[i];

344 state.index = i;

345 canvas.draw_cursor(state);

346 update_hotbar();

347 }

348 }

349 }

350 }

351 }

352 if(event.code === 'Escape') {

353 if(state.tab) {

354 state.tab = null;

355 update_tab_list();

356 return;

357 }

358 if(state.modal) {

359 state.modal = null;

360 update_modal_list();

361 return;

362 }

363 }

364}

365

366function on_wheel(event) {

367 if(state.device === 'mouse') {

368 let y = event.deltaY < 0 ? 1 : -1;

369 if(state.radius + y > 2 - 1 && state.radius + y < 50 + 1) {

370 state.radius += y;

371 node.scale_input.value = state.radius;

372 canvas.draw_cursor(state);

373 }

374 }

375}

376

377function fill_hotbar_list() {

378 for(let [ i, item ] of Object.entries(node.hotbar_list)) {

379 if(state.color_list.length > i) {

380 item.style.background = COLORS[state.color_list[i]];

381 item.addEventListener('pointerdown', function(event) {

382 state.color = state.color_list[i];

383 state.index = i;

384 canvas.draw_cursor(state);

385 update_hotbar();

386 });

387 if(state.index == i) {

388 item.classList.add('active');

389 }

390 }

391 }

392}

393

394function fill_modal_color_list() {

395 for(let [ i, item ] of Object.entries(node.modal_color_list)) {

396 if(COLORS.length > i) {

397 item.style.background = COLORS[i];

398 item.addEventListener('pointerdown', function(event) {

399 state.color = i;

400 state.modal = null;

401 update_modal_list();

402 canvas.draw_cursor(state);

403 });

404 }

405 }

406}

407

408function show_notification(message) {

409 let code = uuid();

410 state.timestamp = code;

411 let content = node.notification.querySelector('.content');

412 content.textContent = locale.get()[message];

413 node.notification.animate([

414 { opacity: 0 },

415 { opacity: 1 }

416 ], {

417 duration: 200,

418 fill: 'both'

419 });

420 setTimeout(function() {

421 if(state.timestamp === code) {

422 node.notification.animate([

423 { opacity: 1 },

424 { opacity: 0 }

425 ], {

426 duration: 200,

427 fill: 'both'

428 });

429 }

430 }, 2000);

431}