@@ -184,6 +184,7 @@ function Croquis(imageDataList, properties) {
184
184
self . lockHistory ( ) ;
185
185
self . addLayer ( index ) ;
186
186
self . unlockHistory ( ) ;
187
+ cacheLayer ( index ) ;
187
188
return remove ;
188
189
}
189
190
var remove = function ( ) {
@@ -205,6 +206,7 @@ function Croquis(imageDataList, properties) {
205
206
var layerContext = getLayerContext ( index ) ;
206
207
layerContext . putImageData ( snapshotData , 0 , 0 ) ;
207
208
self . unlockHistory ( ) ;
209
+ cacheLayer ( index ) ;
208
210
return remove ;
209
211
}
210
212
var remove = function ( ) {
@@ -247,6 +249,7 @@ function Croquis(imageDataList, properties) {
247
249
var tempData = layerContext . getImageData ( x , y , width , height ) ;
248
250
layerContext . putImageData ( snapshotData , x , y ) ;
249
251
snapshotData = tempData ;
252
+ cacheLayer ( index ) ;
250
253
return swap ;
251
254
}
252
255
pushUndo ( swap ) ;
@@ -272,6 +275,7 @@ function Croquis(imageDataList, properties) {
272
275
var tempData = layerContext . getImageData ( 0 , 0 , w , h ) ;
273
276
layerContext . putImageData ( snapshotDatas [ index ] , 0 , 0 ) ;
274
277
snapshotDatas [ index ] = tempData ;
278
+ cacheLayer ( index ) ;
275
279
}
276
280
var swapAll = function ( ) {
277
281
for ( var i = 0 ; i < layers . length ; ++ i )
@@ -497,20 +501,23 @@ function Croquis(imageDataList, properties) {
497
501
pushContextUndo ( index ) ;
498
502
var context = getLayerContext ( index ) ;
499
503
context . clearRect ( 0 , 0 , size . width , size . height ) ;
504
+ cacheLayer ( index ) ;
500
505
} ;
501
506
self . fillLayer = function ( fillColor , index ) {
502
507
index = ( index == null ) ? layerIndex : index ;
503
508
pushContextUndo ( index ) ;
504
509
var context = getLayerContext ( index ) ;
505
510
context . fillStyle = fillColor ;
506
511
context . fillRect ( 0 , 0 , size . width , size . height ) ;
512
+ cacheLayer ( index ) ;
507
513
} ;
508
514
self . fillLayerRect = function ( fillColor , x , y , width , height , index ) {
509
515
index = ( index == null ) ? layerIndex : index ;
510
516
pushDirtyRectUndo ( x , y , width , height , index ) ;
511
517
var context = getLayerContext ( index ) ;
512
518
context . fillStyle = fillColor ;
513
519
context . fillRect ( x , y , width , height ) ;
520
+ cacheLayer ( index ) ;
514
521
} ;
515
522
self . floodFill = function ( x , y , r , g , b , a , index ) {
516
523
index = ( index == null ) ? layerIndex : index ;
@@ -565,6 +572,7 @@ function Croquis(imageDataList, properties) {
565
572
}
566
573
}
567
574
context . putImageData ( imageData , 0 , 0 ) ;
575
+ cacheLayer ( index ) ;
568
576
} ;
569
577
self . getLayerOpacity = function ( index ) {
570
578
index = ( index == null ) ? layerIndex : index ;
@@ -587,6 +595,18 @@ function Croquis(imageDataList, properties) {
587
595
pushLayerVisibleUndo ( index ) ;
588
596
layers [ index ] . style . visibility = visible ? 'visible' : 'hidden' ;
589
597
} ;
598
+ function cacheLayer ( index ) {
599
+ index = ( index == null ) ? layerIndex : index ;
600
+ var w = size . width ;
601
+ var h = size . height ;
602
+ layers [ index ] . cache = getLayerContext ( index ) . getImageData ( 0 , 0 , w , h ) ;
603
+ }
604
+ self . getLayerImageDataCache = function ( index ) {
605
+ index = ( index == null ) ? layerIndex : index ;
606
+ if ( layers [ index ] . cache == null )
607
+ cacheLayer ( index ) ;
608
+ return layers [ index ] . cache ;
609
+ } ;
590
610
function makeColorData ( imageData1x1 ) {
591
611
var data = imageData1x1 . data ;
592
612
var r = data [ 0 ] ;
@@ -602,7 +622,14 @@ function Croquis(imageDataList, properties) {
602
622
if ( ( x < 0 ) || ( x >= size . width ) || ( y < 0 ) || ( y >= size . height ) )
603
623
return null ;
604
624
index = ( index == null ) ? layerIndex : index ;
605
- return makeColorData ( getLayerContext ( index ) . getImageData ( x , y , 1 , 1 ) ) ;
625
+ var cache = self . getLayerImageDataCache ( index ) ;
626
+ var position = ( y * size . width + x ) * 4 ;
627
+ var data = [ ] ;
628
+ data [ 0 ] = cache . data [ position ] ;
629
+ data [ 1 ] = cache . data [ ++ position ] ;
630
+ data [ 2 ] = cache . data [ ++ position ] ;
631
+ data [ 3 ] = cache . data [ ++ position ] ;
632
+ return makeColorData ( { data : data } ) ;
606
633
} ;
607
634
self . eyeDrop = function ( x , y , baseColor ) {
608
635
if ( self . pickColor ( x , y ) == null )
@@ -741,6 +768,7 @@ function Croquis(imageDataList, properties) {
741
768
self . onUpped ( x , y , pressure , dirtyRect ) ;
742
769
window . clearInterval ( knockoutTick ) ;
743
770
window . clearInterval ( tick ) ;
771
+ cacheLayer ( self . getCurrentLayerIndex ( ) ) ;
744
772
}
745
773
self . down = function ( x , y , pressure ) {
746
774
if ( isDrawing || isStabilizing )
0 commit comments