aboutsummaryrefslogtreecommitdiffstats
path: root/layout.js
blob: 297d7bc038aaf7c47ca1df903cf167aa807af084 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const panel = document.querySelector('aside.layout');

const svg = document.getElementById('diagram');
const arrows = Array.from(svg.querySelectorAll('.arrow'));

const preset = panel.querySelector('.control--preset select');
const controls = Array.from(panel.querySelectorAll('.control--axis'));
const steps = Array.from(panel.querySelectorAll('.control--axis > input'));
const dirs = Array.from(panel.querySelectorAll('.control--axis .dir input'));
const turnCCW = document.getElementById('turn-ccw');
const turnCW = document.getElementById('turn-cw');

let state = [ 7, 2, null ];
let last = 0;
let selected = null;

let rot = 0;
let targetRot = 30;

const completeState = ([a, b, c]) => {
  if (a == null) {
    a = b - c;
  } else if (b == null) {
    b = c + a;
  } else {
    c = b - a;
  }
  return [a, b, c, -a, -b, -c];
};

const PRESETS = {
  'wicki-hayden': [ 7, 2, null ],
  'janko': [ 1, 2, null ],
  'harmonic': [ 7, 4, null ],
  'gerhard': [ 4, 3, null ],
};

const updateValues = () => {
  const full = completeState(state);

  full.forEach((val, i) => {
    arrows[i].querySelector('text').textContent = val;

    if (i < steps.length) steps[i].value = Math.abs(val);
    if (i < dirs.length) {
      dirs[i].checked = val < 0;
      dirs[i].disabled = val == 0;
    }
  });

  const presetName = Object.keys(PRESETS).find(k => completeState(PRESETS[k]).join(',') === full.join(','));
  preset.value = presetName ?? 'custom';

  updateFocus();
};

const updateFocus = () => {
  const full = completeState(state);

  controls.forEach((control, i) => {
    let className = 'control control--axis';
    if (state[i] == null) className += ' driven';
    else if (i == selected) className += ' selected';
    if (state[i] == 0) className += ' dir-disabled';
    control.className = className;
  });

  full.forEach((val, i) => {
    const positive = val > 0 || (val == 0 && i < 3);

    let className = 'arrow';
    if (state[i%3] == null) className += ' driven';
    if (!positive) className += ' negative'; 
    if (i%3 == selected) className += ' selected';
    arrows[i].className.baseVal = className;
  });
};

const select = (i) => {
  if (state[i] != null) return false;

  const newState = [null, null, null];
  newState[i] = completeState(state)[i];
  newState[last] = state[last];
  state = newState;
  last = i;

  return true;
};

export const turn = (dir) => {
  targetRot += dir * 30;
}

arrows.forEach((arrow, i) => {
  arrow.onclick = () => steps[i%3].focus();
});

controls.forEach((control, i) => {
  control.addEventListener('focusin', () => { selected = i; select(i); updateFocus(); });
  control.addEventListener('focusout', () => { selected = null; updateFocus(); });
});

steps.forEach((input, i) => {
  input.onchange = () => {
    select(i);
    state[i] = +input.value;
    if (dirs[i].checked) state[i] = -state[i];
    updateValues();
  };
});

dirs.forEach((input, i) => {
  input.onchange = () => {
    select(i);
    state[i] = state[i] * -1;
    updateValues();
  };
});

preset.onchange = () => {
  const nextState = PRESETS[preset.value];
  if (nextState) {
    state = nextState.slice();
    last = state.findIndex(s => s != null);
    updateValues();
  }
};
preset.value = 'wicki-hayden';
preset.onchange();

turnCCW.onclick = () => turn(-1);
turnCW.onclick = () => turn(1);

export const update = () => {
  const delta = targetRot - rot;
  if (Math.abs(delta) < 1) rot = targetRot;
  else rot += delta * 0.1;
  
  if (rot < 60 && rot > -60) return;

  const full = completeState(state);
  let driven = state.indexOf(null);

  // cw rotation
  while (rot >= 60) {
    rot -= 60;
    targetRot -= 60;
    full.unshift(full.pop());
    driven++;
  }

  // cw rotation
  while (rot <= -60) {
    rot += 60;
    targetRot += 60;
    full.push(full.shift());
    driven = (driven-1+3) % 3;
  }

  state = full.slice(0, 3);
  state[driven%3] = null;

  updateValues();
};

export const getSteps = () => completeState(state);
export const getRot = () => rot / 180 * Math.PI;