so://phie

outline

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

blob: cb91bfff37e0a7f95dd8bd693f4e19c3c46fbb61 (5707 bytes)[doc][blame][raw]

1export default {

2 init: function(app) {

3 this.app = app;

4 },

5 bind: function(element, args) {

6 if(typeof args === 'object') {

7 for(let entry of Object.entries(args)) {

8 let [ key, value ] = entry;

9 if(typeof this[key] === 'function' || typeof this.app.state.action[key] === 'object') {

10 this._bindType(element, key, value);

11 }

12 }

13 }

14 },

15 action: function(action, type, func, args) {

16 action.element.addEventListener(type, function(event) {

17 if(action.before) {

18 action.before();

19 }

20 func(event);

21 if(action.after) {

22 action.after();

23 }

24 }, args);

25 },

26 _bindType: function(element, key, value) {

27 let that = this;

28 if(typeof value === 'string') {

29 if(Array.isArray(element)) {

30 for(let item of element) {

31 item.addEventListener(value, function(event) {

32 if(that[key]) {

33 that[key](event);

34 }

35 if(that.app.state.action[key]) {

36 that.app.state.action[key].trigger();

37 }

38 });

39 }

40 }

41 else {

42 element.addEventListener(value, function(event) {

43 if(that[key]) {

44 that[key](event);

45 }

46 if(that.app.state.action[key]) {

47 that.app.state.action[key].trigger();

48 }

49 });

50 }

51 }

52 if(typeof value === 'object') {

53 if(Array.isArray(value)) {

54 for(let item of value) {

55 this._bindType(element, key, item);

56 }

57 }

58 else {

59 let { type } = value;

60 if(typeof type === 'string') {

61 delete value.type;

62 if(Array.isArray(element)) {

63 for(let item of element) {

64 item.addEventListener(type, function(event) {

65 if(that[key]) {

66 that[key](event);

67 }

68 if(that.app.state.action[key]) {

69 that.app.state.action[key].trigger();

70 }

71 }, value);

72 }

73 }

74 else {

75 element.addEventListener(type, function(event) {

76 if(that[key]) {

77 that[key](event);

78 }

79 if(that.app.state.action[key]) {

80 that.app.state.action[key].trigger();

81 }

82 }, value);

83 }

84 }

85 }

86 }

87 },

88 prevent: function(event) {

89 event.preventDefault();

90 },

91 begin: function(event) {

92 this.app.state.dragging = true;

93 let state = this._state(event);

94 this.app.socket.data(state);

95 this.app.paint.draw(state);

96 this.app.state.last = state.pos;

97 this.app.node.toolbar.style.pointerEvents = 'none';

98 this.app.socket.cursor();

99 this.app.update();

100 },

101 drag: function(event) {

102 let state = this._state(event);

103 if(this.app.state.dragging && document.hasFocus()) {

104 this.app.socket.data(state);

105 this.app.paint.draw(state);

106 this.app.state.last = state.pos;

107 if(event.type === 'touchmove') {

108 this.prevent(event);

109 }

110 }

111 this.app.socket.cursor();

112 this.app.update();

113 },

114 end: function(event) {

115 this._state(event);

116 this.app.state.dragging = false;

117 this.app.state.last = null;

118 this.app.node.toolbar.style.pointerEvents = '';

119 this.app.socket.cursor();

120 this.app.update();

121 },

122 move: function(event) {

123 this.app.state.cursor = event.target === this.app.node.canvas ? this.app.state.pos : null;

124 this.app.socket.cursor();

125 this.app.update();

126 },

127 out: function(event) {

128 this.app.state.cursor = null;

129 this.app.socket.cursor();

130 this.app.update();

131 },

132 scroll: function(event) {

133 if(event.ctrlKey) {

134 event.preventDefault();

135 return;

136 }

137 if(!this.app.modal.opened) {

138 if(event.deltaY < 0) {

139 this.app.state.radius += this.app.state.radius < 15 ? 1 : 0;

140 }

141 else {

142 this.app.state.radius -= this.app.state.radius > 1 ? 1 : 0;

143 }

144 }

145 this.app.socket.cursor();

146 this.app.update();

147 },

148 key: function(event) {

149 if(!this.app.state.opened) {

150 if(!event.getModifierState("CapsLock")) {

151 let index;

152 if(event.code.startsWith('Digit')) {

153 index = parseInt(event.code.substr(5));

154 }

155 if(event.code.startsWith('Numpad')) {

156 index = parseInt(event.code.substr(6));

157 }

158 index--;

159 if(Number.isInteger(index) && index < 7) {

160 for(let i in this.app.node.bucket) {

161 let item = this.app.node.bucket[i];

162 if(index === parseInt(i)) {

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

164 this.app.state.color = item.dataset.color;

165 }

166 else {

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

168 }

169 }

170 }

171 }

172 }

173 else if(this.app.state.current && this.app.state.current.action.cancel) {

174 if(event.keyCode === 27) {

175 this.app.modal.close();

176 }

177 }

178 this.app.socket.cursor();

179 this.app.update();

180 },

181 button: function(event) {

182 for(let item of this.app.node.bucket) {

183 if(item === event.target) {

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

185 this.app.state.color = item.dataset.color;

186 }

187 else {

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

189 }

190 }

191 },

192 resize: function(event) {

193 if(typeof event !== 'undefined') {

194 let that = this;

195 clearTimeout(this.app.state.delay);

196 this.app.state.delay = setTimeout(function() {

197 that._delayResize();

198 }, 100);

199 }

200 else {

201 this._delayResize();

202 }

203 },

204 _delayResize: function(event) {

205 this.app.node.canvas.width = window.innerWidth;

206 this.app.node.canvas.height = window.innerHeight;

207 let cache = this.app.paint.o.getImageData(0, 0, this.app.node.overlay.width, this.app.node.overlay.height);

208 this.app.node.overlay.width = window.innerWidth;

209 this.app.node.overlay.height = window.innerHeight;

210 this.app.paint.o.putImageData(cache, 0, 0);

211 this.app.socket.request();

212 },

213 _state: function(event) {

214 let pos = null;

215 if(event.type === 'mousemove' || event.type === 'mousedown') {

216 pos = {

217 x: event.layerX,

218 y: event.layerY

219 };

220 }

221 if(event.type === 'touchmove' || event.type === 'touchstart') {

222 pos = {

223 x: event.touches[0].pageX,

224 y: event.touches[0].pageY

225 };

226 }

227 return Object.assign(this.app.state, { pos: pos });

228 }

229}