-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShiftGenJS.html
233 lines (185 loc) · 8.34 KB
/
ShiftGenJS.html
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Magicshifter JS Sim</title>
<style>
body {
background: #000;
}
</style>
<script type="text/javascript" src="js/libs/snap.svg-min.js"></script>
<script type="text/javascript" src="js/libs/dat.gui.js"></script>
<script type="text/javascript" src="js/libs/three/three.js"></script>
<script type="text/javascript" src="js/libs/Colour.js"></script>
<script type="text/javascript" src="js/libs/FileSaver.js"></script>
</head>
<body>
<div style="position: fixed; top: 0px; left: 0px;">
<input type="file" id="filePicture" accept="image/*">
<canvas id="canvasActiveBitmap" height="16"></canvas>
</div>
<div style="position: fixed; bottom: 0px; left: 0px;">
<textarea id="svgSource"></textarea>
</div>
<script>
window.addEventListener("load", function() {
document.getElementById("filePicture").addEventListener("change", function(event) {
var img = new Image;
img.onload = function() {
var canvas = document.getElementById('canvasActiveBitmap');
var w = img.width;
var h = img.height;
canvas.width = w;
canvas.height = h;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0);
imgData = ctx.getImageData(0,0,w,h);
simState.steps = w;
refreshGui();
DoSim();
};
img.src = URL.createObjectURL(event.target.files[0]);
});
var imgData;
var width = window.innerWidth;
var height = window.innerHeight;
var s = Snap(width, height);
var simState = {
steps: 50,
startStep: 0,
ledRadius: 2,
axisX: 0.1,
axisY: 0.9,
axisZ: 0.11,
startX: -100,
startY: 0,
startZ: 200,
shifterDirX: 0,
shifterDirY: 10,
shifterDirZ: 0,
transX: 0.01,
transY: -4.01,
transZ: 0.01,
rotSpeed: 0.035,
preview: false,
save: function () {
if (simState.preview) {
simState.preview = false;
refreshGui();
DoSim();
}
var svgText = s.outerSVG();
var svgBlob = new Blob([svgText], {type: "image/svg+xml"});
saveAs(svgBlob, "export.svg");
document.getElementById("svgSource").value = svgText;
},
};
var gui = new dat.GUI();
function refreshGui() {
for (var i in gui.__controllers) {
gui.__controllers[i].updateDisplay();
}
}
function SetupOnChange(controller) {
controller.onChange(function (value) {
// Fires on every change, drag, keypress, etc.
DoSim();
});
// controller.onFinishChange(function(value) {
// // Fires when a controller loses focus.
// alert("The new value is " + value);
// });
}
SetupOnChange(gui.add(simState, 'steps', 1, 350));
SetupOnChange(gui.add(simState, 'startStep', 0, 249));
SetupOnChange(gui.add(simState, 'ledRadius', 1, 10));
SetupOnChange(gui.add(simState, 'rotSpeed', 0, 0.1));
SetupOnChange(gui.add(simState, 'axisX', -1, 1));
SetupOnChange(gui.add(simState, 'axisY', -1, 1));
SetupOnChange(gui.add(simState, 'axisZ', -1, 1));
SetupOnChange(gui.add(simState, 'startX', -500, 500));
SetupOnChange(gui.add(simState, 'startY', -500, 500));
SetupOnChange(gui.add(simState, 'startZ', -500, 500));
SetupOnChange(gui.add(simState, 'shifterDirX', -10, 10));
SetupOnChange(gui.add(simState, 'shifterDirY', -10, 10));
SetupOnChange(gui.add(simState, 'shifterDirZ', -10, 10));
SetupOnChange(gui.add(simState, 'transX', -10, 10));
SetupOnChange(gui.add(simState, 'transY', -10, 10));
SetupOnChange(gui.add(simState, 'transZ', -10, 10));
SetupOnChange(gui.add(simState, 'preview'));
gui.add(simState, 'save');
// initial run
DoSim();
function DoSim() {
s.clear();
var LEDS = simState.preview ? 3 : 16;
var ledDist = 15.0 / (LEDS-1);
var ledPositions = [];
var tempPositions = [];
var tempNormal;
var pos = new THREE.Vector4(simState.startX, simState.startY, simState.startZ, 1);
var dir = new THREE.Vector4(simState.shifterDirX, simState.shifterDirY, simState.shifterDirZ, 0);
var norm = new THREE.Vector3(1, 0, 0);
norm.crossVectors(norm, dir);
norm.normalize();
for (var i = 0; i < LEDS; i++) {
var newPos = dir.clone();
newPos.multiplyScalar(i * ledDist);
newPos.add(pos);
ledPositions[i] = newPos;
tempPositions[i] = ledPositions[i];
}
var axis = new THREE.Vector3(simState.axisX, simState.axisY, simState.axisZ);
axis.normalize();
var m = new THREE.Matrix4();
m.makeRotationAxis(axis, simState.rotSpeed);
var t = new THREE.Matrix4();
t.makeTranslation(simState.transX, simState.transY, simState.transZ);
m.multiply(t);
for (var step = 0; step < simState.steps; step++) {
for (var i = 0; i < LEDS; i++) {
var v = tempPositions[i];
// show it if start is invalid or if its in range
if (simState.startStep >= simState.steps ||
step >= simState.startStep) {
var color = new HSVColour((360 * Math.sin(step / 40)) % 360, 100, 100);
if (imgData) {
var x = step - Math.floor(simState.startStep);
x = x % imgData.width;
var y = i;
var raw = imgData.data;
var idx = x*4 + imgData.width*4*y;
if (idx >= 0 && idx < raw.length) {
color = new RGBColour(raw[idx], raw[idx + 1], raw[idx + 2], raw[idx + 3] / 255.0);
}
}
var c = color.getRGB();
//if (c.r != 0 || c.g != 0 || c.b != 0) {
var zz = norm.z;
var xx = v.x + width/2;
var yy = v.y + height/2;
//var angle = Math.atan2(norm.x, norm.y);
var ledCirc = s.ellipse(xx, yy, simState.ledRadius, simState.ledRadius*Math.abs(zz));
var ledCirc = s.circle(xx, yy, simState.ledRadius);
ledCirc.attr({
fill: color.getCSSHexadecimalRGB(),
stroke: "#000",
strokeWidth: 0,
});
// ledCirc.transform( 'r' + (180*angle) + ',' + xx + ',' + yy );
//}
}
vT = v.clone();
vT.applyMatrix4(m);
tempPositions[i] = vT;
}
//norm.transformDirection(m);
}
var svgText = s.outerSVG();
document.getElementById("svgSource").value = svgText;
}
});
</script>
</body>
</html>