so://phie

outline

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

blob: 53991fb0be58e6833bbbd03f514f2182c9c50b07 (5921 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 if(event.keyCode === 13) {

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

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

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

105 that.socket.connect();

106 that.modal.load();

107 }

108 }

109 });

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

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

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

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

114 that.socket.connect();

115 that.modal.load();

116 }

117 });

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

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

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

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

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

123 }

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

125 setTimeout(function() {

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

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

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

129 }, 10);

130 });

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

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

133 that.modal.close();

134 });

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

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

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

138 that.socket.clear();

139 that.modal.close();

140 });

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

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

143 }

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

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

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

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

148 that.modal.close();

149 }

150 });

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

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

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

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

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

156 }

157 that.modal.close();

158 });

159 }

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

161 that.modal.load();

162 that.socket.reconnect();

163 });

164 this.loop();

165 },

166 update: function() {

167 this.socket.cursor();

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

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

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

171 this.paint.cursor(client);

172 }

173 },

174 loop: function() {

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

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

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

178

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

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

181 this.update();

182 }

183 }

184};