so://phie

outline

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

blob: 48367ceeb6267e45cdbc60614ccc1a031a3cd131 (6906 bytes)[doc][blame][raw]

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

2import { uuid, download, switchTheme } 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 dark: false

25 },

26 template: {},

27 modal: {},

28 init: function() {

29 this.constant = constant;

30 this.event = event;

31 this.paint = paint;

32 this.modal = modal;

33 this.socket = socket;

34

35 // build

36 this.node = define({

37 canvas: '.canvas',

38 overlay: '.overlay',

39 toolbar: '.toolbar',

40 wrapper: '.wrapper',

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

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

43 list: '.bucket-list',

44 spinner: '.spinner'

45 });

46

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

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

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

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

51 });

52 this.node.bucket = [];

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

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

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

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

57 }

58 this.node.color = [];

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

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

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

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

63 }

64

65 // init

66 this.event.init(this);

67 this.paint.init(this);

68 this.modal.init(this);

69 this.socket.init(this);

70 let that = this;

71

72 // cache

73 if(typeof localStorage !== 'undefined') {

74 if(typeof localStorage.getItem('outline.theme.dark') !== 'undefined') {

75 this.state.dark = (localStorage.getItem('outline.theme.dark') === 'true');

76 for(let item of this.node.action) {

77 if(item.dataset.action === 'theme') {

78 switchTheme(item, that);

79 }

80 }

81 }

82 }

83

84 // events

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

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

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

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

89 });

90 this.event.bind(document, {

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

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

93 key: 'keydown',

94 out: 'mouseout',

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

96 });

97 this.event.bind(window, {

98 resize: 'resize'

99 });

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

101 button: 'mousedown'

102 });

103 this.event.resize();

104

105 // actions

106 for(let item of this.node.action) {

107 if(item.dataset.action === 'theme') {

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

109 switchTheme(item, that, true);

110 });

111 }

112 }

113

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

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

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

117 }

118 }

119

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

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

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

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

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

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

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

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

128 that.socket.connect();

129 that.modal.load();

130 }

131 else {

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

133 }

134 }

135 });

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

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

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

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

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

141 that.socket.connect();

142 that.modal.load();

143 }

144 else {

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

146 }

147 });

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

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

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

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

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

153 }

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

155 setTimeout(function() {

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

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

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

159 }, 10);

160 });

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

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

163 that.modal.close();

164 });

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

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

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

168 that.socket.clear();

169 that.modal.close();

170 });

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

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

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

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

175 }

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

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

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

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

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

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

182 that.modal.close();

183 }

184 else {

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

186 }

187 });

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

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

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

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

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

193 }

194 that.modal.close();

195 });

196 }

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

198 that.modal.load();

199 that.socket.reconnect();

200 });

201 this.loop();

202 },

203 update: function() {

204 this.socket.cursor();

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

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

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

208 this.paint.cursor(client);

209 }

210 },

211 loop: function() {

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

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

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

215

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

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

218 this.update();

219 }

220 }

221};