Skip to content

Commit b138813

Browse files
committed
Added playback controls
1 parent 4e37619 commit b138813

File tree

5 files changed

+67
-12
lines changed

5 files changed

+67
-12
lines changed

package-lock.json

Lines changed: 7 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sand-table-pattern-maker",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "A web application for creating patterns to draw in a sand table using (G-code and THR files for Sisyphus tables)",
55
"author": "Mark Roland",
66
"license": "",

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ sand-pattern --pattern=circle > circle.json
3232
- Press "o" to toggle an overlay of the pattern settings in the canvas
3333
- Press "c" to toggle the live coordinates and plotter mechanism view
3434
- Press "d" to download a heightmap PNG of the pattern.
35+
- Press "p" to play/pause playback of pattern.
3536

3637
## How to Build a New Pattern
3738

src/index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ <h2>Pattern Input</h2>
4949
<h2>Pattern Output</h2>
5050
<table>
5151
<tr>
52+
<td>Playback</td>
53+
<td><input type="range" min="1" max="100" step="1" id="playbackController" name="playback" class="slider" style="width: 20em"><span id="playback-step"></span></td>
54+
</tr>
55+
<tr style="display: none;">
5256
<td>Instructions</td>
5357
<td><span id="pattern-instructions"></span></td>
5458
</tr>

src/js/sand_table_pattern_maker.js

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ var coordinate_overlay = true;
3535
// Store the total path distance
3636
var distance;
3737

38-
// A counter for the draw loop
39-
var draw_iteration = 0;
38+
// Playback control
39+
let play = true;
40+
let controllerActive = false;
41+
let pattern_step = 0;
4042

4143
// Set G-Code command, usually "GO" or "G1"
4244
var gCodeCommand = env.gcode.command;
@@ -142,6 +144,26 @@ new p5((sketch) => {
142144

143145
const selected_pattern = pattern_select.value();
144146
path = Patterns[selected_pattern].draw();
147+
148+
// Initialize playback controller
149+
const playbackController = document.querySelector('#playbackController');
150+
playbackController.max = path.length;
151+
152+
// Add change event handler for playbackController
153+
playbackController.addEventListener('input', () => {
154+
pattern_step = parseInt(playbackController.value);
155+
});
156+
157+
playbackController.addEventListener('mousedown', () => {
158+
controllerActive = true;
159+
play = false;
160+
});
161+
162+
playbackController.addEventListener('mouseup', () => {
163+
updatePlaybackController();
164+
controllerActive = false;
165+
play = true;
166+
});
145167
}
146168

147169
// Processing standard function that loops forever
@@ -171,6 +193,7 @@ new p5((sketch) => {
171193
path_preview = PathHelp.dividePathComplete(path, 10);
172194
}
173195
recalculate_pattern = env.recalculate_pattern;
196+
playbackController.max = path.length;
174197
}
175198

176199
// Reverse the path
@@ -218,8 +241,18 @@ new p5((sketch) => {
218241
draw_pattern_config(Patterns[selected_pattern]);
219242
}
220243

221-
// Increment draw loop counter
222-
draw_iteration++;
244+
// Update the playback controller
245+
if (!controllerActive) {
246+
updatePlaybackController();
247+
}
248+
249+
// Increment pattern step
250+
if (play) {
251+
pattern_step++;
252+
if (pattern_step > path.length) {
253+
pattern_step = 0;
254+
}
255+
}
223256
}
224257

225258
// Add event callback for mouse pressed
@@ -253,7 +286,7 @@ new p5((sketch) => {
253286
/**
254287
* Trigger actions when the pattern is changed
255288
*/
256-
function patternSelectEvent(recalculate_pattern = true) {
289+
function patternSelectEvent(recalc_pattern = true) {
257290

258291
// Clear controls
259292
sketch.select('#pattern-controls').html('');
@@ -266,7 +299,7 @@ new p5((sketch) => {
266299
loadPatternConfig(selected_pattern);
267300

268301
// Save the state of the Patterns object to Local Browser Storage
269-
if (recalculate_pattern) {
302+
if (recalc_pattern) {
270303
savePatternConfig(previous_pattern);
271304
}
272305

@@ -370,6 +403,7 @@ new p5((sketch) => {
370403

371404
// Recalculate the pattern
372405
path = Patterns[selected_pattern].draw();
406+
playbackController.max = path.length;
373407
}
374408

375409
function drawTable(plotter_exceeded = false) {
@@ -463,7 +497,7 @@ new p5((sketch) => {
463497

464498
let i_max = path.length;
465499
if (animated) {
466-
i_max = draw_iteration % path.length;
500+
i_max = pattern_step % path.length;
467501
}
468502

469503
// Draw entire path
@@ -817,6 +851,8 @@ window.addEventListener('keydown', (event) => {
817851
imagePath(path);
818852
} else if (event.key === 'o') {
819853
pattern_config_overlay = !pattern_config_overlay;
854+
} else if (event.key === 'p') {
855+
play = !play;
820856
}
821857
});
822858

@@ -1072,3 +1108,14 @@ function loadPatternConfig(selected_pattern)
10721108
Patterns[selected_pattern].config = loaded_state;
10731109
}
10741110
}
1111+
1112+
function updatePlaybackController() {
1113+
const playbackController = document.querySelector('#playbackController');
1114+
playbackController.value = pattern_step % path.length;
1115+
1116+
// Insert value after playbackController
1117+
const playbackValueSpan = document.querySelector('#playback-step');
1118+
playbackValueSpan.innerHTML = parseInt(playbackController.value).toLocaleString('en-US', { maximumFractionDigits: 0 })
1119+
+ " / "
1120+
+ path.length.toLocaleString('en-US', { maximumFractionDigits: 0 });
1121+
}

0 commit comments

Comments
 (0)