@@ -35,8 +35,10 @@ var coordinate_overlay = true;
3535// Store the total path distance
3636var 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"
4244var 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