so://phie

outline

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

blob: f3fecd974f15ca0d02922769df049dfcbea05149 (7119 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 });

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

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

85 });

86 this.node.bucket = [];

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

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

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

90 render(this.node.toolbar);

91 }

92 this.node.color = [];

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

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

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

96 render(this.node.list);

97 }

98

99 // init

100 this.event.init(this);

101 this.paint.init(this);

102 this.modal.init(this);

103 this.socket.init(this);

104 let that = this;

105

106 // events

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

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

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

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

111 });

112 this.event.bind(document, {

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

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

115 key: 'keydown',

116 out: 'mouseout',

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

118 });

119 this.event.bind(window, {

120 resize: 'resize'

121 });

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

123 button: 'mousedown'

124 });

125 this.event.resize();

126

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

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

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

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

131 }

132 else {

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

134 }

135 }

136 }

137

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

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

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

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

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

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

144 that.socket.connect();

145 that.socket.cursor();

146 that.modal.close();

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 that.modal.close();

157 }

158 });

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

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

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

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

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

164 }

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

166 setTimeout(function() {

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

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

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

170 }, 10);

171 });

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

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

174 that.socket.cursor();

175 that.update();

176 that.modal.close();

177 });

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

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

180 that.socket.clear();

181 that.modal.close();

182 });

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

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

185 }

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

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

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

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

190 that.modal.close();

191 }

192 });

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

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

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

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

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

198 }

199 that.modal.close();

200 });

201 }

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

203 that.modal.close();

204 that.socket.reconnect();

205 });

206 },

207 update: function() {

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

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

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

211 this.paint.cursor(client);

212 }

213 }

214};