回答編集履歴
2
ちょっと修正
test
CHANGED
@@ -4,9 +4,199 @@
|
|
4
4
|
|
5
5
|
```js
|
6
6
|
|
7
|
-
|
7
|
+
/*
|
8
|
+
|
8
|
-
|
9
|
+
<!DOCTYPE html>
|
10
|
+
|
11
|
+
<meta charset="UTF-8">
|
12
|
+
|
13
|
+
<title>Table Cross Cursor</title>
|
14
|
+
|
15
|
+
<body>
|
16
|
+
|
17
|
+
*/
|
18
|
+
|
19
|
+
<script>
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
function work (e, c) { e.style.background = c;}
|
24
|
+
|
25
|
+
function* toRight (e) { while (e) yield e = e.nextElementSibling }
|
26
|
+
|
27
|
+
function* toLeft (e) { while (e) yield e = e.previousElementSibling }
|
28
|
+
|
29
|
+
function* toTop (e) {
|
30
|
+
|
31
|
+
let i = e.cellIndex, p = e.parentNode;
|
32
|
+
|
33
|
+
while (p = p.previousElementSibling) yield p.cells[i]
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
function* toBottom (e) {
|
38
|
+
|
39
|
+
let i = e.cellIndex, p = e.parentNode;
|
40
|
+
|
41
|
+
while (p = p.nextElementSibling) yield p.cells[i]
|
42
|
+
|
43
|
+
}
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
|
48
|
+
|
9
|
-
//________________________________
|
49
|
+
//________________________________
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
class Color {
|
54
|
+
|
55
|
+
constructor (red = 0, green = 0, blue = 0, a = -1) {
|
56
|
+
|
57
|
+
this.r = red; this.g = green; this.b = blue; this.a = a;
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
get color () { return this.a === -1 ? 'transparent': `rgba(${this.r},${this.g},${this.b},${this.a})`}
|
62
|
+
|
63
|
+
copy () { return new Color (this.r, this.g, this.b, this.a) }
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
//________________________________
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
class Cell {
|
76
|
+
|
77
|
+
constructor (work, way, gain = 1, c = new Color) {
|
78
|
+
|
79
|
+
this.work = work;
|
80
|
+
|
81
|
+
this.way = way;
|
82
|
+
|
83
|
+
this.gain = gain;
|
84
|
+
|
85
|
+
this.color = c;
|
86
|
+
|
87
|
+
this.life = 1;
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
this.action ();
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
action () {
|
98
|
+
|
99
|
+
let e = this.way.next().value;
|
100
|
+
|
101
|
+
if (e) {
|
102
|
+
|
103
|
+
this.work (e, this.color.color);
|
104
|
+
|
105
|
+
this.life *= this.gain;
|
106
|
+
|
107
|
+
this.color.a *= this.gain;
|
108
|
+
|
109
|
+
if (0.01 < this.life)
|
110
|
+
|
111
|
+
this.action ();
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
//________________________________
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
class CrossCursor {
|
126
|
+
|
127
|
+
constructor (table, gain = .8, ...c) {
|
128
|
+
|
129
|
+
let n = c.length, [t,r,b,l] = c || [];
|
130
|
+
|
131
|
+
if (1 > n) throw new Error;
|
132
|
+
|
133
|
+
this.table = table;
|
134
|
+
|
135
|
+
this.buf = null;
|
136
|
+
|
137
|
+
this.gain = gain;
|
138
|
+
|
139
|
+
this.color = [[],[t,t,t,t],[t,r,t,r],[t,r,b,r], c][n];
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
table.addEventListener ('mousemove', this, false);
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
|
148
|
+
|
149
|
+
setColor (event) {
|
150
|
+
|
151
|
+
let
|
152
|
+
|
153
|
+
g = this.gain,
|
154
|
+
|
155
|
+
[t,r,b,l] = this.color, e;
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
if (e = this.buf) {
|
160
|
+
|
161
|
+
new Cell (work, toTop(e), g);
|
162
|
+
|
163
|
+
new Cell (work, toRight(e), g);
|
164
|
+
|
165
|
+
new Cell (work, toBottom(e),g);
|
166
|
+
|
167
|
+
new Cell (work, toLeft(e), g);
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
this.buf = e = event.target;
|
172
|
+
|
173
|
+
new Cell (work, toTop(e), g, t.copy());
|
174
|
+
|
175
|
+
new Cell (work, toRight(e), g, r.copy());
|
176
|
+
|
177
|
+
new Cell (work, toBottom(e), g, b.copy());
|
178
|
+
|
179
|
+
new Cell (work, toLeft(e), g, l.copy());
|
180
|
+
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
handleEvent (event) {
|
186
|
+
|
187
|
+
if ('TD' === event.target.nodeName)
|
188
|
+
|
189
|
+
this.setColor (event);
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
//________________________________
|
198
|
+
|
199
|
+
//demo
|
10
200
|
|
11
201
|
|
12
202
|
|
@@ -32,193 +222,23 @@
|
|
32
222
|
|
33
223
|
|
34
224
|
|
35
|
-
//________________________________
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
function work (e, c) { e.style.background = c;}
|
40
|
-
|
41
|
-
function* toRight (e) { while (e) yield e = e.nextElementSibling }
|
42
|
-
|
43
|
-
function* toLeft (e) { while (e) yield e = e.previousElementSibling }
|
44
|
-
|
45
|
-
function* toTop (e) {
|
46
|
-
|
47
|
-
let i = e.cellIndex, p = e.parentNode;
|
48
|
-
|
49
|
-
while (p = p.previousElementSibling) yield p.cells[i]
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
function* toBottom (e) {
|
54
|
-
|
55
|
-
let i = e.cellIndex, p = e.parentNode;
|
56
|
-
|
57
|
-
while (p = p.nextElementSibling) yield p.cells[i]
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
//________________________________
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
class Color {
|
68
|
-
|
69
|
-
constructor (red = 0, green = 0, blue = 0, a = -1) {
|
70
|
-
|
71
|
-
this.r = red; this.g = green; this.b = blue; this.a = a;
|
72
|
-
|
73
|
-
}
|
74
|
-
|
75
|
-
get color () { return this.a === -1 ? 'transparent': `rgba(${this.r},${this.g},${this.b},${this.a})`}
|
76
|
-
|
77
|
-
copy () { return new Color (this.r, this.g, this.b, this.a) }
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
//________________________________
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
class Cell {
|
88
|
-
|
89
|
-
constructor (work, way, gain, life = 1, c = new Color) {
|
90
|
-
|
91
|
-
this.work = work;
|
92
|
-
|
93
|
-
this.way = way;
|
94
|
-
|
95
|
-
this.gain = gain;
|
96
|
-
|
97
|
-
this.life = life;
|
98
|
-
|
99
|
-
this.color = c;
|
100
|
-
|
101
|
-
this.action ();
|
102
|
-
|
103
|
-
}
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
action () {
|
108
|
-
|
109
|
-
let e = this.way.next().value;
|
110
|
-
|
111
|
-
if (e) {
|
112
|
-
|
113
|
-
this.work (e, this.color.color);
|
114
|
-
|
115
|
-
this.life *= this.gain;
|
116
|
-
|
117
|
-
this.color.a *= this.gain;
|
118
|
-
|
119
|
-
if (0.01 < this.life)
|
120
|
-
|
121
|
-
this.action ();
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
|
-
}
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
//________________________________
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
class CrossCursor {
|
136
|
-
|
137
|
-
constructor (table, gain = .8, ...c) {
|
138
|
-
|
139
|
-
this.table = table;
|
140
|
-
|
141
|
-
this.buf = null;
|
142
|
-
|
143
|
-
this.gain = gain;
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
let [t,r,b,l] = c, z = [[],[t,t,t,t],[t,r,t,r],[t,r,b,r], c][c.length];
|
148
|
-
|
149
|
-
console.log(z);
|
150
|
-
|
151
|
-
this.color = z.map (y => y.copy());
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
table.addEventListener ('mousemove', this, false);
|
156
|
-
|
157
|
-
}
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
setColor (event) {
|
162
|
-
|
163
|
-
let g = this.gain, [t,r,b,l] = this.color, e;
|
164
|
-
|
165
|
-
if (this.buf) {
|
166
|
-
|
167
|
-
e = this.buf;
|
168
|
-
|
169
|
-
new Cell (work, toTop(e), g,1, new Color());
|
170
|
-
|
171
|
-
new Cell (work, toRight(e), g,1, new Color());
|
172
|
-
|
173
|
-
new Cell (work, toBottom(e),g,1, new Color());
|
174
|
-
|
175
|
-
new Cell (work, toLeft(e), g,1, new Color());
|
176
|
-
|
177
|
-
}
|
178
|
-
|
179
|
-
this.buf = e = event.target;
|
180
|
-
|
181
|
-
new Cell (work, toTop(e), g,1, t.copy());
|
182
|
-
|
183
|
-
new Cell (work, toRight(e), g,1, r.copy());
|
184
|
-
|
185
|
-
new Cell (work, toBottom(e), g,1, b.copy());
|
186
|
-
|
187
|
-
new Cell (work, toLeft(e), g,1, l.copy());
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
handleEvent (event) {
|
194
|
-
|
195
|
-
let e = event.target;
|
196
|
-
|
197
|
-
if ('TD' === e.nodeName)
|
198
|
-
|
199
|
-
this.setColor (event);
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
}
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
//________________________________
|
208
|
-
|
209
|
-
|
210
|
-
|
211
225
|
let [a,b,c,d] = document.querySelectorAll ('table');
|
212
226
|
|
213
227
|
let colors = [new Color (0,255,0,1), new Color (255,255,0,1), new Color(255,0,0,1), new Color (0,0,255,1)];
|
214
228
|
|
215
|
-
new CrossCursor (a,
|
229
|
+
new CrossCursor (a, 1, colors[0]);
|
216
230
|
|
217
231
|
new CrossCursor (b, .8, ...colors.slice (0,2));
|
218
232
|
|
219
|
-
new CrossCursor (c, .
|
233
|
+
new CrossCursor (c, .6, ...colors.slice (0,3));
|
220
|
-
|
234
|
+
|
221
|
-
new CrossCursor (d, .
|
235
|
+
new CrossCursor (d, .4, ...colors.slice (0,4));
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
</script>
|
240
|
+
|
241
|
+
|
222
242
|
|
223
243
|
|
224
244
|
|
1
余計なものを削除
test
CHANGED
@@ -166,25 +166,25 @@
|
|
166
166
|
|
167
167
|
e = this.buf;
|
168
168
|
|
169
|
-
|
169
|
+
new Cell (work, toTop(e), g,1, new Color());
|
170
|
-
|
170
|
+
|
171
|
-
|
171
|
+
new Cell (work, toRight(e), g,1, new Color());
|
172
|
-
|
172
|
+
|
173
|
-
|
173
|
+
new Cell (work, toBottom(e),g,1, new Color());
|
174
|
-
|
174
|
+
|
175
|
-
|
175
|
+
new Cell (work, toLeft(e), g,1, new Color());
|
176
176
|
|
177
177
|
}
|
178
178
|
|
179
179
|
this.buf = e = event.target;
|
180
180
|
|
181
|
-
|
181
|
+
new Cell (work, toTop(e), g,1, t.copy());
|
182
|
-
|
182
|
+
|
183
|
-
|
183
|
+
new Cell (work, toRight(e), g,1, r.copy());
|
184
|
-
|
184
|
+
|
185
|
-
|
185
|
+
new Cell (work, toBottom(e), g,1, b.copy());
|
186
|
-
|
186
|
+
|
187
|
-
|
187
|
+
new Cell (work, toLeft(e), g,1, l.copy());
|
188
188
|
|
189
189
|
}
|
190
190
|
|