so://phie

outline

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

blob: 1459d4dc6e23e91400921b751de4e6476f10784b (6346 bytes)[doc][blame][raw]

1import constant from './mod/constant.js';

2import { uuid, download } from './mod/util.js';

3import { define, html } from './mod/node.js';

4import event from './mod/event.js';

5import paint from './mod/paint.js';

6import modal from './mod/modal.js';

7import socket from './mod/socket.js';

8

9const GOOGLE_API_KEY = 'QUl6YVN5QzZVcFRpNVlYc1h6eFJ2aEo4Z1RWbnJtTkpCQ2JCMjBn';

10

11export default {

12 state: {

13 last: null,

14 cursor: null,

15 radius: 3,

16 color: '#000000',

17 client: [],

18 opened: true,

19 current: undefined,

20 modal: {},

21 action: {},

22 fps: 1000 / 60,

23 then: Date.now()

24 },

25 template: {},

26 modal: {},

27 init: function() {

28 this.constant = constant;

29 this.event = event;

30 this.paint = paint;

31 this.modal = modal;

32 this.socket = socket;

33

34 // build

35 this.node = define({

36 canvas: '.canvas',

37 overlay: '.overlay',

38 toolbar: '.toolbar',

39 wrapper: '.wrapper',

40 modal: { query: '.modal', all: true },

41 action: { query: '[data-action]', all: true },

42 list: '.bucket-list',

43 spinner: '.spinner'

44 });

45

46 this.template.bucket = html(function(item) {

47 return `<div class="bucket${item.active ? ' active' : ''}"

48 data-color="${item.code}"

49 style="background: ${item.code}"></div>`;

50 });

51 this.node.bucket = [];

52 for(let item of this.constant.basic) {

53 let result = this.template.bucket.build(item);

54 this.node.bucket.push(result.element);

55 result.render(this.node.toolbar);

56 }

57 this.node.color = [];

58 for(let item of this.constant.color) {

59 let result = this.template.bucket.build(item);

60 this.node.color.push(result.element);

61 result.render(this.node.list);

62 }

63

64 // init

65 this.event.init(this);

66 this.paint.init(this);

67 this.modal.init(this);

68 this.socket.init(this);

69 let that = this;

70

71 // events

72 this.event.bind(this.node.canvas, {

73 prevent: ['dragstart', 'contextmenu'],

74 begin: ['mousedown', 'touchstart'],

75 drag: ['mousemove', 'touchmove']

76 });

77 this.event.bind(document, {

78 move: ['mousemove', 'touchmove'],

79 end: ['mouseup', 'touchend', 'mouseleave'],

80 key: 'keydown',

81 out: 'mouseout',

82 scroll: { type: 'wheel', passive: false }

83 });

84 this.event.bind(window, {

85 resize: 'resize'

86 });

87 this.event.bind(this.node.bucket, {

88 button: 'mousedown'

89 });

90 this.event.resize();

91

92 // actions

93 this.modal.get('color').action.open.before = function(modal) {

94 for(let item of that.node.color) {

95 item.classList[that.state.color === item.dataset.color ? 'add' : 'remove']('active');

96 }

97 }

98

99 this.modal.get('join').element.querySelector('.action-uuid').focus();

100 this.event.action(this.modal.get('join').action.uuid, 'keydown', function(event) {

101 let input = that.modal.get('join').element.querySelector('.action-uuid');

102 input.classList.remove('invalid');

103 if(event.keyCode === 13) {

104 that.state.name = input.value.replace(/\s+/g, '');

105 if(that.state.name.length >= 3 && that.state.name.length <= 20) {

106 that.state.uuid = that.state.name + '-' + uuid();

107 that.socket.connect();

108 that.modal.load();

109 }

110 else {

111 input.classList.add('invalid');

112 }

113 }

114 });

115 this.event.action(this.modal.get('join').action.join, 'click', function(event) {

116 let input = that.modal.get('join').element.querySelector('.action-uuid');

117 that.state.name = input.value.replace(/\s+/g, '');

118 if(that.state.name.length >= 3 && that.state.name.length <= 20) {

119 that.state.uuid = that.state.name + '-' + uuid();

120 that.socket.connect();

121 that.modal.load();

122 }

123 else {

124 input.classList.add('invalid');

125 }

126 });

127 this.modal.get('scaling').action.open.before = function(modal) {

128 modal.element.querySelector('input.action-range').value = that.state.radius;

129 let circle = modal.element.querySelector('.circle');

130 circle.style.width = that.state.radius * 4 + 'px';

131 circle.style.height = that.state.radius * 4 + 'px';

132 }

133 this.event.action(this.modal.get('scaling').action.range, 'input', function(event) {

134 setTimeout(function() {

135 let circle = that.modal.get('scaling').element.querySelector('.modal .circle');

136 circle.style.width = event.target.value * 4 + 'px';

137 circle.style.height = event.target.value * 4 + 'px';

138 }, 10);

139 });

140 this.event.action(this.modal.get('scaling').action.scaling, 'click', function(event) {

141 that.state.radius = parseInt(that.modal.get('scaling').element.querySelector('.modal input.action-range').value);

142 that.modal.close();

143 });

144 this.event.action(this.modal.get('clear').action.clear, 'click', function(event) {

145 that.paint.g.fillStyle = 'white';

146 that.paint.g.fillRect(0, 0, window.innerWidth, window.innerHeight);

147 that.socket.clear();

148 that.modal.close();

149 });

150 this.modal.get('save').action.open.before = function(modal) {

151 modal.element.querySelector('input.action-file').value = 'outline-image-' + uuid();

152 let input = that.modal.get('save').element.querySelector('.modal input.action-file');

153 input.classList.remove('invalid');

154 }

155 this.event.action(this.modal.get('save').action.save, 'click', function(event) {

156 let input = that.modal.get('save').element.querySelector('.modal input.action-file');

157 input.classList.remove('invalid');

158 let file = input.value.replace(/\s+/g, '');

159 if(file.length >= 3 && file.length <= 40) {

160 download(file, that.node.canvas.toDataURL('image/jpg'));

161 that.modal.close();

162 }

163 else {

164 input.classList.add('invalid');

165 }

166 });

167 for(let item of this.node.color) {

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

169 that.state.color = item.dataset.color;

170 for(let _item of that.node.bucket) {

171 _item.classList.remove('active');

172 }

173 that.modal.close();

174 });

175 }

176 this.event.action(this.modal.get('error').action.reconnect, 'click', function(event) {

177 that.modal.load();

178 that.socket.reconnect();

179 });

180 this.loop();

181 },

182 update: function() {

183 this.socket.cursor();

184 this.paint.o.clearRect(0, 0, window.innerWidth, window.innerHeight);

185 this.paint.cursor(this.state);

186 for(let client of Object.values(this.state.client)) {

187 this.paint.cursor(client);

188 }

189 },

190 loop: function() {

191 requestAnimationFrame(this.loop.bind(this));

192 this.state.now = Date.now();

193 this.state.elapsed = this.state.now - this.state.then;

194

195 if(this.state.elapsed > this.state.fps) {

196 this.state.then = this.state.now - (this.state.elapsed % this.state.fps);

197 this.update();

198 }

199 }

200};