so://phie

outline

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

blob: f7cce557701e6fbe3458c0f7560303ba37d48e76 (5674 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 },

99 drag: function(event) {

100 let state = this.state(event);

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

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

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

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

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

106 this.prevent(event);

107 }

108 }

109 },

110 end: function(event) {

111 this.state(event);

112 this.app.state.dragging = false;

113 this.app.state.last = null;

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

115 },

116 move: function(event) {

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

118 },

119 out: function(event) {

120 this.app.state.cursor = null;

121 },

122 scroll: function(event) {

123 if(event.ctrlKey) {

124 event.preventDefault();

125 return;

126 }

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

128 if(event.deltaY < 0) {

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

130 }

131 else {

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

133 }

134 }

135 },

136 key: function(event) {

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

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

139 let index;

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

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

142 }

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

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

145 }

146 index--;

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

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

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

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

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

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

153 }

154 else {

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

156 }

157 }

158 }

159 console.log(event.code);

160 if(event.code === 'KeyC') {

161 this.app.modal.open('color');

162 }

163 if(event.code === 'KeyV') {

164 this.app.modal.open('scaling');

165 }

166 if(event.code === 'KeyT') {

167 this.app.modal.open('clear');

168 }

169 if(event.code === 'KeyS') {

170 this.app.modal.open('save');

171 }

172 }

173 }

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

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

176 this.app.modal.close();

177 }

178 }

179 },

180 button: function(event) {

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

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

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

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

185 }

186 else {

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

188 }

189 }

190 },

191 resize: function(event) {

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

193 let that = this;

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

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

196 that.delayResize();

197 }, 100);

198 }

199 else {

200 this.delayResize();

201 }

202 },

203 delayResize: function(event) {

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

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

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

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

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

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

210 this.app.socket.request();

211 },

212 state: function(event) {

213 let pos = null;

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

215 pos = {

216 x: event.layerX,

217 y: event.layerY

218 };

219 }

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

221 pos = {

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

223 y: event.touches[0].pageY

224 };

225 }

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

227 }

228}