-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.js
199 lines (168 loc) · 5.42 KB
/
App.js
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const chartScale = 1;
const chartBarWidth = 50;
const chartBottomPadding = 100;
const App = {
GOOD_EFFORT_TIME_MS: 2000,
init() {
$('addConstraintButton').click(() => {
const newC = addConstraint();
$('constraints').append(newC);
newC.focus();
});
let relaxButtonDown = false;
$('relaxButton').mousedown(() => { relaxButtonDown = true; });
$(document.body).mouseup(() => { relaxButtonDown = false; });
function setError(error) {
$('errorValue').text(error.toFixed(5));
}
let relaxingUntilTime = 0;
let toDoWhenSatisfiedOrDone = [];
function relaxForUpTo(millis, optFn) {
relaxingUntilTime = Date.now() + millis;
toDoWhenSatisfiedOrDone.push({
timeout: relaxingUntilTime,
fn: optFn || (() => {})
});
}
const errorFns = [];
const varDict = {};
function addConstraint() {
const constraint = App.Constraint.create();
$('constraints').append(constraint);
errorFns.push(constraint.errorFn);
relaxForUpTo(App.GOOD_EFFORT_TIME_MS);
$(constraint).change(() => {
errorFns.splice($(constraint).index(), 1, constraint.errorFn);
constraint.varNames.forEach((name) => {
if (!varDict[name]) {
addVar(name);
}
});
relaxForUpTo(App.GOOD_EFFORT_TIME_MS);
});
$(constraint).on('delete', () => deleteConstraint(constraint));
return constraint;
}
function addVar(name) {
const aVar = App.Var.create(name, 0);
$('vars').append(aVar);
varDict[name] = aVar;
let lockButtonClickCount = 0;
$(aVar).on('lockbuttonclick', () => {
lockButtonClickCount++;
if (!aVar.locked) {
relaxForUpTo(App.GOOD_EFFORT_TIME_MS);
}
});
$(aVar).change(() => {
let fn;
if (!aVar.locked) {
aVar.locked = true;
let oldLockButtonClickCount = lockButtonClickCount;
fn = () => {
if (lockButtonClickCount === oldLockButtonClickCount) {
aVar.locked = false;
relaxForUpTo(App.GOOD_EFFORT_TIME_MS);
}
};
}
relaxForUpTo(App.GOOD_EFFORT_TIME_MS, fn);
});
$(aVar).on('delete', () => {
$('aConstraint').each(function() {
this.inlineVar(aVar.name, aVar.value);
if (this.varNames.length === 0) {
deleteConstraint(this);
}
});
delete varDict[name];
$(aVar).remove();
});
}
function deleteConstraint(constraint) {
errorFns.splice($(constraint).index(), 1);
$(constraint).remove();
relaxForUpTo(App.GOOD_EFFORT_TIME_MS);
}
setError(0);
const relax = new Relax(varDict, errorFns);
let relaxing = false;
let relaxed = true;
const ctxt = canvas.getContext('2d');
function doSomeRelaxing() {
doBarStuff();
let currTime = Date.now();
relaxing = false;
if (currTime < relaxingUntilTime || relaxButtonDown) {
relaxing = true;
const error = relax.iterateForUpTo(1000 / 50);
setError(error);
if (error < relax.epsilon) {
relaxed = true;
relaxingUntilTime = 0;
while (toDoWhenSatisfiedOrDone.length > 0) {
const fn = toDoWhenSatisfiedOrDone.shift().fn;
fn();
}
} else {
relaxed = false;
}
}
while (toDoWhenSatisfiedOrDone.length > 0 && currTime >= toDoWhenSatisfiedOrDone[0].timeout) {
const fn = toDoWhenSatisfiedOrDone.shift().fn;
fn();
}
$('app').toggleClass('relaxing', relaxing);
$('app').toggleClass('relaxed', relaxed);
requestAnimationFrame(doSomeRelaxing);
}
function doBarStuff() {
canvas.width = innerWidth;
canvas.height = innerHeight;
if (targetVar) {
targetVar.value = targetVarDesiredValue;
relaxForUpTo(App.GOOD_EFFORT_TIME_MS);
}
let x = 0;
Object.keys(varDict).forEach(name => {
if (!varDict[name].hasBar) {
return;
}
const scaledValue = varDict[name].value * chartScale;
ctxt.fillStyle = 'cornflowerblue';
ctxt.fillRect(x, innerHeight - scaledValue - chartBottomPadding, chartBarWidth - 2, scaledValue);
ctxt.fillStyle = 'black';
ctxt.font = '10pt Avenir';
const w = ctxt.measureText(name).width;
const yOffset = scaledValue > 0 ? 6 : -18;
ctxt.fillText(name, x + (chartBarWidth - 2) / 2 - w / 2, innerHeight - scaledValue - chartBottomPadding - yOffset);
x += chartBarWidth;
});
}
let targetVar = null;
let targetVarOrigValue;
let targetVarDesiredValue;
let yOffset;
canvas.onmousedown = e => {
const idx = Math.floor(e.offsetX / chartBarWidth);
targetVar = Object.values(varDict).filter(v => v.hasBar)[idx] || null;
if (targetVar) {
console.log(targetVar.name);
targetVarOrigValue = targetVar.value;
targetVarDesiredValue = targetVarOrigValue;
yOffset = e.offsetY;
}
};
canvas.onmousemove = e => {
if (!targetVar) {
return;
}
const delta = (yOffset - e.offsetY) / chartScale;
targetVarDesiredValue = targetVarOrigValue + delta;
};
canvas.onmouseup = e => {
targetVar = null;
};
requestAnimationFrame(doSomeRelaxing);
}
};