so://phie

outline

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

blob: ab0d73c235609efaa4af8605926bbc17950dee1b (13426 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 gamepad from './addon/gamepad.js';

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

9

10let state = {

11 color: 54,

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

13 radius: 14,

14 index: 0,

15 device: null,

16 device_fallback: null,

17 device_list: {

18 mouse: false,

19 touch: false,

20 pen: false,

21 gamepad: false

22 }

23};

24

25let node;

26

27// state variables

28let notification_code;

29let left_modal = false;

30let current_modal;

31let current_tab;

32

33// node elements

34let ad;

35let hotbar;

36let toolbar;

37let actionbar;

38let actions;

39let tools;

40let device_empty;

41let colors_modal;

42let capture_modal;

43let clear_modal;

44let scale_range;

45

46window.toggle_modal = toggle_modal;

47window.open_tab = open_tab;

48window.close_tab = close_tab;

49window.clear_screen = clear_screen;

50

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

52

53function init() {

54 canvas.init();

55 locale.init('en');

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

57 gamepad.init();

58 ripple.init();

59 // detect gamepad

60 gamepad.on_connect(function(gamepad) {

61 state.device_list.gamepad = true;

62 show_notification('notification.input-device.gamepad.add');

63 if(!state.device) {

64 state.device = 'gamepad';

65 state.device_fallback = 'gamepad';

66 }

67 update_device_list();

68 });

69 // gamepad removed

70 gamepad.on_disconnect(function(gamepad) {

71 state.device_list.gamepad = false;

72 show_notification('notification.input-device.gamepad.remove');

73 if(state.device === 'gamepad') {

74 if(state.device !== state.device_fallback) {

75 state.device = state.device_fallback;

76 }

77 else {

78 state.device = null;

79 }

80 // point = { x: null, y: null };

81 // last = { x: null, y: null };

82 }

83 update_device_list();

84 });

85 init_custom();

86

87 node = query({

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

89 hotbar_color_list: { query: '.hotbar .color', all: true },

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

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

92 });

93

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

95 let { target } = event;

96 if(target !== scale_range) {

97 event.preventDefault();

98 }

99 }, { passive: false });

100

101 device_empty = document.querySelector('.settings-modal .device-tab .item.empty');

102 ad = document.querySelector('.controls .ad');

103 hotbar = document.querySelector('.controls .hotbar');

104 toolbar = document.querySelector('.controls .toolbar');

105 actionbar = document.querySelector('.controls .actionbar');

106 actions = document.querySelectorAll('.actionbar .action');

107 tools = document.querySelectorAll('.toolbar .tool');

108 colors_modal = document.querySelector('.toolbar .tool[data-modal="colors"]');

109 capture_modal = document.querySelector('.toolbar .tool[data-modal="capture"]');

110 clear_modal = document.querySelector('.toolbar .tool[data-modal="clear"]');

111 scale_range = document.querySelector('.scale-modal input[type="range"]');

112 for(let i in node.hotbar_color_list) {

113 if(Object.keys(state.color_list).length > i) {

114 if(i == state.index) {

115 node.hotbar_color_list[i].classList.add('active');

116 }

117 node.hotbar_color_list[i].style.background = COLORS[state.color_list[i]];

118 node.hotbar_color_list[i].addEventListener('pointerdown', function(event) {

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

120 for(let j in node.hotbar_color_list) {

121 if(Object.keys(state.color_list).length > j) {

122 if(j == i) {

123 state.index = j;

124 }

125 node.hotbar_color_list[j].classList[j == i ? 'add' : 'remove']('active');

126 }

127 }

128 });

129 }

130 }

131 for(let i in node.modal_color_list) {

132 if(Object.keys(COLORS).length > i) {

133 node.modal_color_list[i].style.background = COLORS[i];

134 node.modal_color_list[i].addEventListener('pointerdown', function(event) {

135 state.color = i;

136 });

137 }

138 }

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

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

141 scale_range.addEventListener('input', function(event) {

142 state.radius = parseInt(scale_range.value);

143 left_modal = false;

144 // point.x = parseInt(window.innerWidth / 2);

145 // point.y = parseInt(window.innerHeight / 2);

146 });

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

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

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

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

151 state.radius += y;

152 scale_range.value = state.radius;

153 }

154 }

155 });

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

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

158 if(event.ctrlKey) {

159 event.preventDefault();

160 return;

161 }

162 }

163 }, { passive: false });

164

165 document.addEventListener('pointerdown', function(event) {

166 if(state.device !== 'gamepad') {

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

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

169 canvas.on_press(event);

170

171 ad.style.pointerEvents = 'none';

172 hotbar.style.pointerEvents = 'none';

173 toolbar.style.pointerEvents = 'none';

174 actionbar.style.pointerEvents = 'none';

175 for(let modal of node.modal_list) {

176 modal.style.pointerEvents = 'none';

177 }

178 }

179 }

180 }

181 });

182 document.addEventListener('pointermove', function(event) {

183 if(state.device !== 'gamepad') {

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

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

186 canvas.on_move(event, state);

187 }

188 }

189 }

190 });

191 document.addEventListener('pointerup', function(event) {

192 if(state.device !== 'gamepad') {

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

194 canvas.on_release(event);

195

196 ad.style.pointerEvents = '';

197 hotbar.style.pointerEvents = '';

198 toolbar.style.pointerEvents = '';

199 actionbar.style.pointerEvents = '';

200 for(let modal of node.modal_list) {

201 modal.style.pointerEvents = '';

202 }

203 }

204 }

205 });

206 document.addEventListener('pointerout', function(event) {

207 if(state.device !== 'gamepad') {

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

209 canvas.on_release(event);

210 }

211 }

212 });

213

214 document.addEventListener('keydown', function(event) {

215 if(state.device !== 'gamepad') {

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

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

218 if(!lock) {

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

220 let digit = parseInt(event.code.substr(5)) - 1;

221 if(digit >= 0 && digit < state.color_list.length) {

222 state.color = state.color_list[digit];

223 state.index = digit;

224 for(let i in node.hotbar_color_list) {

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

226 node.hotbar_color_list[i].classList[i == state.index ? 'add' : 'remove']('active');

227 }

228 }

229 }

230 }

231 }

232 }

233 }

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

235 if(current_tab) {

236 close_tab(current_tab);

237 }

238 else if(current_modal) {

239 toggle_modal(current_modal);

240 }

241 }

242 });

243 // point = { x: null, y: null };

244 // last = { x: null, y: null };

245 // start = { x: null, y: null };

246}

247

248/*

249function render() {

250 if(state.radius < 2) {

251 state.radius = 2;

252 }

253 o.clearRect(0, 0, window.innerWidth, window.innerHeight);

254 if(point.x !== null && point.y !== null) {

255 let touch_device = (state.device === 'touch' || state.device === 'pen');

256 if(touch_device) {

257 if(!left_modal) {

258 o.fillStyle = 'white';

259 o.beginPath();

260 o.arc(point.x, point.y, parseInt(state.radius) + 2, 2 * Math.PI, 0);

261 o.fill();

262 o.fillStyle = COLOR[state.index];

263 o.beginPath();

264 o.arc(point.x, point.y, parseInt(state.radius), 2 * Math.PI, 0);

265 o.fill();

266 }

267 }

268 else {

269 o.fillStyle = 'white';

270 o.beginPath();

271 o.arc(point.x, point.y, parseInt(state.radius) + 2, 2 * Math.PI, 0);

272 o.fill();

273 o.fillStyle = COLOR[state.index];

274 o.beginPath();

275 o.arc(point.x, point.y, parseInt(state.radius), 2 * Math.PI, 0);

276 o.fill();

277 }

278 }

279 if(!left_modal) {

280 if(erase && !draw) {

281 o.beginPath();

282 o.lineWidth = 2;

283 o.strokeStyle = 'white';

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

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

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

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

288 o.stroke();

289 }

290 }

291 if(lock_x || lock_y) {

292 o.beginPath();

293 o.lineWidth = 2;

294 o.strokeStyle = 'white';

295 if(lock_x) {

296 o.moveTo(point.x, point.y - parseInt(state.radius));

297 o.lineTo(point.x, point.y + parseInt(state.radius));

298 }

299 if(lock_y) {

300 o.moveTo(point.x - parseInt(state.radius), point.y);

301 o.lineTo(point.x + parseInt(state.radius), point.y);

302 }

303 o.stroke();

304 }

305}

306*/

307

308function init_custom() {

309 // language

310 let language_elements = document.querySelectorAll('[data-event="change.language"]');

311 for(let item of language_elements) {

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

313 let code = item.dataset.code;

314 locale.change(code);

315 for(let sub_item of language_elements) {

316 sub_item.classList[item === sub_item ? 'add' : 'remove']('active');

317 }

318 });

319 }

320 document.addEventListener('pointerdown', function(event) {

321 // detect touch

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

323 state.device_list.touch = true;

324 if(state.device) {

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

326 }

327 else {

328 state.device = 'touch';

329 state.device_fallback = 'touch';

330 }

331 update_device_list();

332 }

333 // detect pen

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

335 state.device_list.pen = true;

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

337 if(!state.device) {

338 state.device = 'pen';

339 state.device_fallback = 'pen';

340 }

341 update_device_list();

342 }

343 });

344 document.addEventListener('pointermove', function(event) {

345 // detect mouse

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

347 state.device_list.mouse = true;

348 if(state.device) {

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

350 }

351 else {

352 state.device = 'mouse';

353 state.device_fallback = 'mouse';

354 }

355 update_device_list();

356 }

357 });

358 // device

359 let device_elements = document.querySelectorAll('[data-event="change.device"]');

360 for(let item of device_elements) {

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

362 let code = item.dataset.code;

363 if(state.device_list[code]) {

364 state.device = code;

365 if(state.device !== 'gamepad') {

366 // point = { x: null, y: null };

367 // last = { x: null, y: null };

368 }

369 for(let sub_item of device_elements) {

370 sub_item.classList[item === sub_item ? 'add' : 'remove']('active');

371 }

372 update_device_list();

373 }

374 });

375 }

376}

377

378function update_device_list() {

379 for(let item of node.device_list) {

380 let code = item.dataset.code;

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

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

383 }

384}

385

386function toggle_modal(element) {

387 let code = element.dataset.modal;

388 let modal = document.querySelector(`.${code}-modal`);

389 if(modal) {

390 let open = modal.classList.contains('hidden');

391 if(open) {

392 current_modal = element;

393 for(let item of actions) {

394 if(item !== element) {

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

396 }

397 }

398 for(let item of tools) {

399 if(item !== element) {

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

401 }

402 }

403 for(let item of node.modal_list) {

404 if(item !== modal) {

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

406 }

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

408 if(main) {

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

410 main.style.marginLeft = '0';

411 }

412 let tab_list = item.querySelector('.tab-list');

413 if(tab_list) {

414 let tabs = tab_list.querySelectorAll('.tab');

415 for(let tab of tabs) {

416 tab.classList.add('hidden');

417 }

418 }

419 }

420 if(code === 'colors') {

421 if(state.device === 'gamepad') {

422 node.modal_color_list[0].classList.add('focus');

423 }

424 }

425 }

426 else {

427 current_modal = null;

428 }

429 element.classList[open ? 'add' : 'remove']('active');

430 modal.classList[open ? 'remove' : 'add']('hidden');

431 modal.animate([

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

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

434 ], {

435 duration: 100,

436 fill: 'both'

437 });

438 }

439}

440

441function open_tab(element) {

442 let code = element.dataset.tab;

443 let main = element.parentNode.parentNode

444 let tab = document.querySelector(`.${code}-tab`);

445 if(tab) {

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

447 current_tab = tab.querySelector('.back');

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

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

450 }

451}

452

453function close_tab(element) {

454 let modal = element.parentNode.parentNode.parentNode.parentNode;

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

456 let tab_list = modal.querySelector('.tab-list');

457 let tabs = tab_list.querySelectorAll('.tab');

458 for(let tab of tabs) {

459 tab.classList.add('hidden');

460 }

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

462 main.style.marginLeft = '0';

463 current_tab = null;

464}

465

466function show_notification(message) {

467 let code = uuid();

468 notification_code = code;

469 let element = document.querySelector('.notification');

470 let content = element.querySelector('.content');

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

472 element.animate([

473 { opacity: 0 },

474 { opacity: 1}

475 ], {

476 duration: 200,

477 fill: 'both'

478 });

479 setTimeout(function() {

480 if(notification_code === code) {

481 element.animate([

482 { opacity: 1 },

483 { opacity: 0}

484 ], {

485 duration: 200,

486 fill: 'both'

487 });

488 }

489 }, 2000);

490}

491

492function clear_screen() {

493 canvas.clear();

494 show_notification('notification.clear');

495 toggle_modal(clear_modal);

496}