@@ -35,8 +35,10 @@ var coordinate_overlay = true;
35
35
// Store the total path distance
36
36
var distance ;
37
37
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 ;
40
42
41
43
// Set G-Code command, usually "GO" or "G1"
42
44
var gCodeCommand = env . gcode . command ;
@@ -142,6 +144,26 @@ new p5((sketch) => {
142
144
143
145
const selected_pattern = pattern_select . value ( ) ;
144
146
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
+ } ) ;
145
167
}
146
168
147
169
// Processing standard function that loops forever
@@ -171,6 +193,7 @@ new p5((sketch) => {
171
193
path_preview = PathHelp . dividePathComplete ( path , 10 ) ;
172
194
}
173
195
recalculate_pattern = env . recalculate_pattern ;
196
+ playbackController . max = path . length ;
174
197
}
175
198
176
199
// Reverse the path
@@ -218,8 +241,18 @@ new p5((sketch) => {
218
241
draw_pattern_config ( Patterns [ selected_pattern ] ) ;
219
242
}
220
243
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
+ }
223
256
}
224
257
225
258
// Add event callback for mouse pressed
@@ -253,7 +286,7 @@ new p5((sketch) => {
253
286
/**
254
287
* Trigger actions when the pattern is changed
255
288
*/
256
- function patternSelectEvent ( recalculate_pattern = true ) {
289
+ function patternSelectEvent ( recalc_pattern = true ) {
257
290
258
291
// Clear controls
259
292
sketch . select ( '#pattern-controls' ) . html ( '' ) ;
@@ -266,7 +299,7 @@ new p5((sketch) => {
266
299
loadPatternConfig ( selected_pattern ) ;
267
300
268
301
// Save the state of the Patterns object to Local Browser Storage
269
- if ( recalculate_pattern ) {
302
+ if ( recalc_pattern ) {
270
303
savePatternConfig ( previous_pattern ) ;
271
304
}
272
305
@@ -370,6 +403,7 @@ new p5((sketch) => {
370
403
371
404
// Recalculate the pattern
372
405
path = Patterns [ selected_pattern ] . draw ( ) ;
406
+ playbackController . max = path . length ;
373
407
}
374
408
375
409
function drawTable ( plotter_exceeded = false ) {
@@ -463,7 +497,7 @@ new p5((sketch) => {
463
497
464
498
let i_max = path . length ;
465
499
if ( animated ) {
466
- i_max = draw_iteration % path . length ;
500
+ i_max = pattern_step % path . length ;
467
501
}
468
502
469
503
// Draw entire path
@@ -817,6 +851,8 @@ window.addEventListener('keydown', (event) => {
817
851
imagePath ( path ) ;
818
852
} else if ( event . key === 'o' ) {
819
853
pattern_config_overlay = ! pattern_config_overlay ;
854
+ } else if ( event . key === 'p' ) {
855
+ play = ! play ;
820
856
}
821
857
} ) ;
822
858
@@ -1072,3 +1108,14 @@ function loadPatternConfig(selected_pattern)
1072
1108
Patterns [ selected_pattern ] . config = loaded_state ;
1073
1109
}
1074
1110
}
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