@@ -198,7 +198,7 @@ export class Turtle extends EventEmitter {
198
198
/**
199
199
* The delay in ms between each steps.
200
200
*/
201
- speed ?: number ;
201
+ delay ?: number ;
202
202
203
203
/**
204
204
* The timer identifier for the step interval.
@@ -296,6 +296,23 @@ export class Turtle extends EventEmitter {
296
296
return this ;
297
297
}
298
298
299
+ /**
300
+ * Resets the interval with the current delay.
301
+ */
302
+ private resetInterval ( ) {
303
+ if ( this . interval ) clearInterval ( this . interval ) ;
304
+ this . interval = setInterval ( this . nextStep . bind ( this ) , this . delay ) ;
305
+ }
306
+
307
+ /**
308
+ * Add a step to the queue.
309
+ */
310
+ private addStep ( step : Step ) : Turtle {
311
+ this . steps . push ( step ) ;
312
+ if ( ! this . interval ) this . resetInterval ( ) ;
313
+ return this ;
314
+ }
315
+
299
316
/**
300
317
* Wipes out the canvas.
301
318
*
@@ -306,7 +323,7 @@ export class Turtle extends EventEmitter {
306
323
this . emit ( 'clear' ) ;
307
324
clearContext ( this . ctx ) ;
308
325
this . draw ( ) ;
309
- } else this . steps . push ( { type : StepType . Clear } ) ;
326
+ } else this . addStep ( { type : StepType . Clear } ) ;
310
327
311
328
return this ;
312
329
}
@@ -330,7 +347,7 @@ export class Turtle extends EventEmitter {
330
347
this . hidden = true ;
331
348
this . restoreImageData ( ) ;
332
349
this . draw ( ) ;
333
- } else this . steps . push ( { type : StepType . Hide } ) ;
350
+ } else this . addStep ( { type : StepType . Hide } ) ;
334
351
return this ;
335
352
}
336
353
@@ -352,7 +369,7 @@ export class Turtle extends EventEmitter {
352
369
this . emit ( 'show' ) ;
353
370
this . hidden = false ;
354
371
this . draw ( ) ;
355
- } else this . steps . push ( { type : StepType . Show } ) ;
372
+ } else this . addStep ( { type : StepType . Show } ) ;
356
373
return this ;
357
374
}
358
375
@@ -373,7 +390,7 @@ export class Turtle extends EventEmitter {
373
390
this . setAngle ( 0 ) ;
374
391
this . goto ( 0 , 0 ) ;
375
392
this . clear ( ) ;
376
- } else this . steps . push ( { type : StepType . Reset } ) ;
393
+ } else this . addStep ( { type : StepType . Reset } ) ;
377
394
return this ;
378
395
}
379
396
@@ -388,7 +405,7 @@ export class Turtle extends EventEmitter {
388
405
this . emit ( 'setShape' , shape ) ;
389
406
this . shape = shape ;
390
407
this . draw ( ) ;
391
- } else this . steps . push ( { type : StepType . SetShape , args : [ shape ] } ) ;
408
+ } else this . addStep ( { type : StepType . SetShape , args : [ shape ] } ) ;
392
409
return this ;
393
410
}
394
411
@@ -409,12 +426,10 @@ export class Turtle extends EventEmitter {
409
426
if ( this . inStep ) {
410
427
this . emit ( 'setDelay' , ms ) ;
411
428
this . stepByStep = ms > 0 ;
412
- this . speed = ms ;
413
-
414
- if ( this . interval ) clearInterval ( this . interval ) ;
429
+ this . delay = ms ;
415
430
416
- this . interval = setInterval ( this . nextStep . bind ( this ) , ms ) ;
417
- } else this . steps . push ( { type : StepType . SetDelay , args : [ ms ] } ) ;
431
+ this . resetInterval ( ) ;
432
+ } else this . addStep ( { type : StepType . SetDelay , args : [ ms ] } ) ;
418
433
return this ;
419
434
}
420
435
@@ -441,7 +456,7 @@ export class Turtle extends EventEmitter {
441
456
if ( this . inStep ) {
442
457
this . emit ( 'putPenUp' ) ;
443
458
this . penDown = false ;
444
- } else this . steps . push ( { type : StepType . PenUp } ) ;
459
+ } else this . addStep ( { type : StepType . PenUp } ) ;
445
460
return this ;
446
461
}
447
462
@@ -468,7 +483,7 @@ export class Turtle extends EventEmitter {
468
483
if ( this . inStep ) {
469
484
this . emit ( 'putPenDown' ) ;
470
485
this . penDown = true ;
471
- } else this . steps . push ( { type : StepType . PenDown } ) ;
486
+ } else this . addStep ( { type : StepType . PenDown } ) ;
472
487
return this ;
473
488
}
474
489
@@ -532,7 +547,7 @@ export class Turtle extends EventEmitter {
532
547
this . color = convertToColor ( color ) ;
533
548
this . restoreImageData ( ) ;
534
549
this . draw ( ) ;
535
- } else this . steps . push ( { type : StepType . SetColor , args : [ color ] } ) ;
550
+ } else this . addStep ( { type : StepType . SetColor , args : [ color ] } ) ;
536
551
return this ;
537
552
}
538
553
@@ -561,7 +576,7 @@ export class Turtle extends EventEmitter {
561
576
this . width = size ;
562
577
this . restoreImageData ( ) ;
563
578
this . draw ( ) ;
564
- } else this . steps . push ( { type : StepType . SetWidth , args : [ size ] } ) ;
579
+ } else this . addStep ( { type : StepType . SetWidth , args : [ size ] } ) ;
565
580
return this ;
566
581
}
567
582
@@ -586,7 +601,7 @@ export class Turtle extends EventEmitter {
586
601
if ( this . inStep ) {
587
602
this . emit ( 'setLineCap' , cap ) ;
588
603
this . lineCap = cap ;
589
- } else this . steps . push ( { type : StepType . SetLineCap , args : [ cap ] } ) ;
604
+ } else this . addStep ( { type : StepType . SetLineCap , args : [ cap ] } ) ;
590
605
return this ;
591
606
}
592
607
@@ -609,7 +624,7 @@ export class Turtle extends EventEmitter {
609
624
this . angle = ang ;
610
625
this . restoreImageData ( ) ;
611
626
this . draw ( ) ;
612
- } else this . steps . push ( { type : StepType . SetAngle , args : [ ang ] } ) ;
627
+ } else this . addStep ( { type : StepType . SetAngle , args : [ ang ] } ) ;
613
628
return this ;
614
629
}
615
630
@@ -638,7 +653,7 @@ export class Turtle extends EventEmitter {
638
653
this . angle -= ang ;
639
654
this . restoreImageData ( ) ;
640
655
this . draw ( ) ;
641
- } else this . steps . push ( { type : StepType . Left , args : [ ang ] } ) ;
656
+ } else this . addStep ( { type : StepType . Left , args : [ ang ] } ) ;
642
657
return this ;
643
658
}
644
659
@@ -667,7 +682,7 @@ export class Turtle extends EventEmitter {
667
682
this . angle += ang ;
668
683
this . restoreImageData ( ) ;
669
684
this . draw ( ) ;
670
- } else this . steps . push ( { type : StepType . Right , args : [ ang ] } ) ;
685
+ } else this . addStep ( { type : StepType . Right , args : [ ang ] } ) ;
671
686
return this ;
672
687
}
673
688
@@ -697,7 +712,7 @@ export class Turtle extends EventEmitter {
697
712
this . position . y = y ;
698
713
this . restoreImageData ( ) ;
699
714
this . draw ( ) ;
700
- } else this . steps . push ( { type : StepType . Goto , args : [ x , y ] } ) ;
715
+ } else this . addStep ( { type : StepType . Goto , args : [ x , y ] } ) ;
701
716
return this ;
702
717
}
703
718
@@ -778,7 +793,7 @@ export class Turtle extends EventEmitter {
778
793
*/
779
794
forward ( distance : number ) : Turtle {
780
795
if ( ! this . inStep ) {
781
- this . steps . push ( { type : StepType . Forward , args : [ distance ] } ) ;
796
+ this . addStep ( { type : StepType . Forward , args : [ distance ] } ) ;
782
797
return this ;
783
798
}
784
799
0 commit comments