so://phie

outline

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

blob: 5033f9c8d29ca13283448065fe99346f70e4f49b (5937 bytes)[doc][blame][raw]

1import { uuid } from './util.js';

2

3export default {

4 init: function(app) {

5 this.app = app;

6 },

7 bind: function(element, args) {

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

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

10 let [ key, value ] = entry;

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

12 this.bindType(element, key, value);

13 }

14 }

15 }

16 },

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

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

19 if(action.before) {

20 action.before();

21 }

22 func(event);

23 if(action.after) {

24 action.after();

25 }

26 }, args);

27 },

28 bindType: function(element, key, value) {

29 let that = this;

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

31 if(Array.isArray(element)) {

32 for(let item of element) {

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

34 if(that[key]) {

35 that[key](event);

36 }

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

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

39 }

40 });

41 }

42 }

43 else {

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

45 if(that[key]) {

46 that[key](event);

47 }

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

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

50 }

51 });

52 }

53 }

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

55 if(Array.isArray(value)) {

56 for(let item of value) {

57 this.bindType(element, key, item);

58 }

59 }

60 else {

61 let { type } = value;

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

63 delete value.type;

64 if(Array.isArray(element)) {

65 for(let item of element) {

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

67 if(that[key]) {

68 that[key](event);

69 }

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

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

72 }

73 }, value);

74 }

75 }

76 else {

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

78 if(that[key]) {

79 that[key](event);

80 }

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

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

83 }

84 }, value);

85 }

86 }

87 }

88 }

89 },

90 prevent: function(event) {

91 event.preventDefault();

92 },

93 begin: function(event) {

94 this.app.state.dragging = true;

95 let state = this.state(event);

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

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

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

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

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 },

112 end: function(event) {

113 this.state(event);

114 this.app.state.dragging = false;

115 this.app.state.last = null;

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

117 },

118 move: function(event) {

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

120 },

121 out: function(event) {

122 this.app.state.cursor = null;

123 },

124 scroll: function(event) {

125 if(event.ctrlKey) {

126 event.preventDefault();

127 return;

128 }

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

130 if(event.deltaY < 0) {

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

132 }

133 else {

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

135 }

136 }

137 },

138 key: function(event) {

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

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

141 let index;

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

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

144 }

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

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

147 }

148 index--;

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

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

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

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

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

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

155 }

156 else {

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

158 }

159 }

160 }

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

162 for(let item of this.app.node.color) {

163 item.classList[this.app.state.color === item.dataset.color ? 'add' : 'remove']('active');

164 }

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

166 }

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

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

169 }

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

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

172 }

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

174 this.app.modal.get('save').element.querySelector('input.action-file').value = 'outline-image-' + uuid();

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

176 }

177 }

178 }

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

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

181 this.app.modal.close();

182 }

183 }

184 },

185 button: function(event) {

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

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

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

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

190 }

191 else {

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

193 }

194 }

195 },

196 resize: function(event) {

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

198 let that = this;

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

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

201 that.delayResize();

202 }, 100);

203 }

204 else {

205 this.delayResize();

206 }

207 },

208 delayResize: function(event) {

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

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

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

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

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

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

215 this.app.socket.request();

216 },

217 state: function(event) {

218 let pos = null;

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

220 pos = {

221 x: event.layerX,

222 y: event.layerY

223 };

224 }

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

226 pos = {

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

228 y: event.touches[0].pageY

229 };

230 }

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

232 }

233}