so://phie

outline

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

blob: 9ea5d3785b9b47e937854037d34783daf49b8494 (7094 bytes)[doc][blame][raw]

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

2import node from './mod/node.js';

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

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

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

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

7

8export default {

9 constant: {

10 color: [

11 { name: 'Salmon', code: '#FA8072' },

12 { name: 'Crimson', code: '#DC143C' },

13 { name: 'Firebrick', code: '#B22222' },

14 { name: 'Pink', code: '#FFC0CB' },

15 { name: 'Hotpink', code: '#FF69B4' },

16 { name: 'Coral', code: '#FF7F50' },

17 { name: 'Tomato', code: '#FF6347' },

18 { name: 'Orange', code: '#FFA500' },

19 { name: 'Gold', code: '#FFD700' },

20 { name: 'Khaki', code: '#F0E68C' },

21 { name: 'Lavender', code: '#E6E6FA' },

22 { name: 'Plum', code: '#DDA0DD' },

23 { name: 'Violet', code: '#EE82EE' },

24 { name: 'Magenta', code: '#FF00FF' },

25 { name: 'Purple', code: '#800080' },

26 { name: 'Indigo', code: '#4B0082' },

27 { name: 'Lime', code: '#00FF00' },

28 { name: 'Olive', code: '#808000' },

29 { name: 'Teal', code: '#008080' },

30 { name: 'Cyan', code: '#00FFFF' },

31 { name: 'Aquamarine', code: '#7FFFD4' },

32 { name: 'Turquoise', code: '#40E0D0' },

33 { name: 'Navy', code: '#000080' },

34 { name: 'Bisque', code: '#FFE4C4' },

35 { name: 'Tan', code: '#D2B48C' },

36 { name: 'Peru', code: '#CD853F' },

37 { name: 'Chocolate', code: '#D2691E' },

38 { name: 'Sienna', code: '#A0522D' },

39 { name: 'Brown', code: '#A52A2A' },

40 { name: 'Silver', code: '#C0C0C0' }

41 ],

42 basic: [

43 { name: 'Black', code: '#000000', active: true },

44 { name: 'White', code: '#FFFFFF' },

45 { name: 'Grey', code: '#808080' },

46 { name: 'Red', code: '#FF0000' },

47 { name: 'Blue', code: '#0000FF' },

48 { name: 'Green', code: '#00FF00' },

49 { name: 'Yellow', code: '#FFFF00' }

50 ]

51 },

52 state: {

53 last: null,

54 cursor: null,

55 radius: 3,

56 color: '#000000',

57 client: [],

58 opened: true,

59 current: undefined,

60 modal: {},

61 action: {}

62 },

63 template: {},

64 modal: {},

65 init: function() {

66 this.util = util;

67 this.node = node;

68 this.event = event;

69 this.paint = paint;

70 this.modal = modal;

71 this.socket = socket;

72

73 // build

74 this.node = node.define({

75 canvas: '.canvas',

76 overlay: '.overlay',

77 toolbar: '.toolbar',

78 wrapper: '.wrapper',

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

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

81 list: '.bucket-list',

82 spinner: '.spinner'

83 });

84 this.template.bucket = node.template(function(item) {

85 return `<li class="bucket tooltip${item.up ? ' up' : ''}${item.active ? ' active' : ''}" data-title="${item.name}" data-color="${item.code}" style="background: ${item.code}"></li>`;

86 });

87 this.node.bucket = [];

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

89 let render = this.template.bucket({ up: true, ...item});

90 this.node.bucket.push(render.element);

91 render(this.node.toolbar);

92 }

93 this.node.color = [];

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

95 let render = this.template.bucket({ up: false, ...item});

96 this.node.color.push(render.element);

97 render(this.node.list);

98 }

99

100 // init

101 this.event.init(this);

102 this.paint.init(this);

103 this.modal.init(this);

104 this.socket.init(this);

105 let that = this;

106

107 // events

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

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

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

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

112 });

113 this.event.bind(document, {

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

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

116 key: 'keydown',

117 out: 'mouseout',

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

119 });

120 this.event.bind(window, {

121 resize: 'resize'

122 });

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

124 button: 'mousedown'

125 });

126 this.event.resize();

127

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

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

130 if(that.state.color === item.dataset.color) {

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

132 }

133 else {

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

135 }

136 }

137 }

138

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

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

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

142 that.state.name = that.modal.get('join').element.querySelector('.action-uuid').value.replace(/\s+/g, '');

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

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

145 that.socket.connect();

146 that.socket.cursor();

147 }

148 }

149 });

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

151 that.state.name = that.modal.get('join').element.querySelector('.action-uuid').value.replace(/\s+/g, '');

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

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

154 that.socket.connect();

155 that.socket.cursor();

156 }

157 });

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

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

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

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

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

163 }

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

165 setTimeout(function() {

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

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

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

169 }, 10);

170 });

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

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

173 that.socket.cursor();

174 that.update();

175 that.modal.close();

176 });

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

178 that.paint.g.clearRect(0, 0, window.innerWidth, window.innerHeight);

179 that.socket.clear();

180 that.modal.close();

181 });

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

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

184 }

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

186 let file = that.modal.get('save').element.querySelector('.modal input.action-file').value.replace(/\s+/g, '');

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

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

189 that.modal.close();

190 }

191 });

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

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

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

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

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

197 }

198 that.modal.close();

199 });

200 }

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

202 that.modal.close();

203 that.socket.reconnect();

204 });

205 },

206 update: function() {

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

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

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

210 this.paint.cursor(client);

211 }

212 }

213};