@@ -12,6 +12,8 @@ let brushSize = 3;
1212let autofill = false ;
1313
1414let trailPoints = [ ] ;
15+ let rectToolStart = null ;
16+ let rectToolPreview = null ;
1517let lineToolStart = null ;
1618let lineToolPreview = null ;
1719
@@ -268,6 +270,18 @@ function startStroke(e) {
268270 pickCanvasColorAtEvent ( e ) ;
269271 return ;
270272 }
273+ if ( tool === "rect" ) {
274+ isDrawing = true ;
275+ const hex = colorToHex ( currentColor ) ;
276+ strokeHex = activeLayer === LAYER . FILL ? fillWhite : hex ;
277+ activeSubColor [ activeLayer ] = strokeHex ;
278+ ensureSublayer ( activeLayer , strokeHex ) ;
279+ renderLayerSwatches ( activeLayer ) ;
280+ beginGlobalHistoryStep ( activeLayer , currentFrame , strokeHex ) ;
281+ rectToolStart = { x, y } ;
282+ rectToolPreview = { x, y } ;
283+ return ;
284+ }
271285 if ( tool === "rect-select" ) {
272286 isDrawing = true ;
273287 beginRectSelect ( e ) ;
@@ -347,6 +361,7 @@ function startStroke(e) {
347361 activeSubColor [ activeLayer ] = strokeHex ;
348362 ensureSublayer ( activeLayer , strokeHex ) ;
349363 renderLayerSwatches ( activeLayer ) ;
364+ beginGlobalHistoryStep ( activeLayer , currentFrame , strokeHex ) ;
350365 lineToolStart = { x, y } ;
351366 lineToolPreview = { x, y } ;
352367 return ;
@@ -409,6 +424,11 @@ function continueStroke(e) {
409424 x : x ,
410425 y : y
411426 } ;
427+ if ( tool === "rect" ) {
428+ rectToolPreview = { x, y } ;
429+ queueRenderAll ( ) ;
430+ return ;
431+ }
412432 if ( tool === "rect-select" ) {
413433 updateRectSelect ( e ) ;
414434 lastPt = {
@@ -481,11 +501,34 @@ function continueStroke(e) {
481501function endStroke ( ) {
482502 if ( ! isDrawing ) return ;
483503 isDrawing = false ;
484- commitGlobalHistoryStep ( ) ;
485504 const endKey = strokeHex ;
486- strokeHex = null ;
487- queueRenderAll ( ) ;
488- updateTimelineHasContent ( currentFrame ) ;
505+ const finishingRect = tool === "rect" && rectToolStart && rectToolPreview ;
506+ const finishingLine = tool === "line" && lineToolStart && lineToolPreview ;
507+ if ( ! finishingRect && ! finishingLine ) {
508+ commitGlobalHistoryStep ( ) ;
509+ }
510+ if ( tool === "rect" && rectToolStart && rectToolPreview ) {
511+ const hex = strokeHex || activeSubColor ?. [ activeLayer ] || colorToHex ( currentColor ) ;
512+ const off = getFrameCanvas ( activeLayer , currentFrame , hex ) ;
513+ const ctx = off . getContext ( "2d" ) ;
514+ ctx . strokeStyle = hex ;
515+ ctx . lineWidth = Math . max ( 1 , brushSize ) ;
516+ ctx . lineCap = "round" ;
517+ ctx . beginPath ( ) ;
518+ ctx . rect ( rectToolStart . x , rectToolStart . y , rectToolPreview . x - rectToolStart . x , rectToolPreview . y - rectToolStart . y ) ;
519+ ctx . stroke ( ) ;
520+ markFrameHasContent ( activeLayer , currentFrame , hex ) ;
521+ markGlobalHistoryDirty ( ) ;
522+ commitGlobalHistoryStep ( ) ;
523+ rectToolStart = null ;
524+ rectToolPreview = null ;
525+ strokeHex = null ;
526+ queueRenderAll ( ) ;
527+ updateTimelineHasContent ( currentFrame ) ;
528+ lastPt = null ;
529+ stabilizedPt = null ;
530+ return ;
531+ }
489532 if ( tool === "rect-select" ) {
490533 endRectSelect ( ) ;
491534 lastPt = null ;
@@ -505,12 +548,18 @@ function endStroke() {
505548 ctx . lineTo ( lineToolPreview . x , lineToolPreview . y ) ;
506549 ctx . stroke ( ) ;
507550 markFrameHasContent ( activeLayer , currentFrame , hex ) ;
551+ markGlobalHistoryDirty ( ) ;
552+ commitGlobalHistoryStep ( ) ;
508553 lineToolStart = null ;
509554 lineToolPreview = null ;
555+ strokeHex = null ;
510556 queueRenderAll ( ) ;
511557 updateTimelineHasContent ( currentFrame ) ;
512558 return ;
513559 }
560+ strokeHex = null ;
561+ queueRenderAll ( ) ;
562+ updateTimelineHasContent ( currentFrame ) ;
514563 if ( tool === "lasso-erase" && lassoActive ) {
515564 lassoActive = false ;
516565 applyLassoErase ( ) ;
@@ -1538,3 +1587,15 @@ function fillFromLineart(F) {
15381587 updateTimelineHasContent ( F ) ;
15391588 return true ;
15401589}
1590+
1591+ function drawRectToolPreview ( ctx ) {
1592+ if ( ! rectToolStart || ! rectToolPreview ) return ;
1593+ ctx . save ( ) ;
1594+ ctx . strokeStyle = colorToHex ( currentColor ) ;
1595+ ctx . lineWidth = Math . max ( 1 , brushSize ) ;
1596+ ctx . globalAlpha = 0.5 ;
1597+ ctx . beginPath ( ) ;
1598+ ctx . rect ( rectToolStart . x , rectToolStart . y , rectToolPreview . x - rectToolStart . x , rectToolPreview . y - rectToolStart . y ) ;
1599+ ctx . stroke ( ) ;
1600+ ctx . restore ( ) ;
1601+ }
0 commit comments