Skip to content

Commit 2bca9a9

Browse files
committed
cache
1 parent df726a5 commit 2bca9a9

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "croquis.js",
33
"main": "croquis.js",
4-
"version": "0.2.0",
4+
"version": "0.2.1",
55
"homepage": "https://github.com/disjukr/croquis.js",
66
"authors": [
77
"JongChan Choi <disjukr@naver.com>"

croquis.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ function Croquis(imageDataList, properties) {
184184
self.lockHistory();
185185
self.addLayer(index);
186186
self.unlockHistory();
187+
cacheLayer(index);
187188
return remove;
188189
}
189190
var remove = function () {
@@ -205,6 +206,7 @@ function Croquis(imageDataList, properties) {
205206
var layerContext = getLayerContext(index);
206207
layerContext.putImageData(snapshotData, 0, 0);
207208
self.unlockHistory();
209+
cacheLayer(index);
208210
return remove;
209211
}
210212
var remove = function () {
@@ -247,6 +249,7 @@ function Croquis(imageDataList, properties) {
247249
var tempData = layerContext.getImageData(x, y, width, height);
248250
layerContext.putImageData(snapshotData, x, y);
249251
snapshotData = tempData;
252+
cacheLayer(index);
250253
return swap;
251254
}
252255
pushUndo(swap);
@@ -272,6 +275,7 @@ function Croquis(imageDataList, properties) {
272275
var tempData = layerContext.getImageData(0, 0, w, h);
273276
layerContext.putImageData(snapshotDatas[index], 0, 0);
274277
snapshotDatas[index] = tempData;
278+
cacheLayer(index);
275279
}
276280
var swapAll = function () {
277281
for (var i = 0; i < layers.length; ++i)
@@ -497,20 +501,23 @@ function Croquis(imageDataList, properties) {
497501
pushContextUndo(index);
498502
var context = getLayerContext(index);
499503
context.clearRect(0, 0, size.width, size.height);
504+
cacheLayer(index);
500505
};
501506
self.fillLayer = function (fillColor, index) {
502507
index = (index == null) ? layerIndex : index;
503508
pushContextUndo(index);
504509
var context = getLayerContext(index);
505510
context.fillStyle = fillColor;
506511
context.fillRect(0, 0, size.width, size.height);
512+
cacheLayer(index);
507513
};
508514
self.fillLayerRect = function (fillColor, x, y, width, height, index) {
509515
index = (index == null) ? layerIndex : index;
510516
pushDirtyRectUndo(x, y, width, height, index);
511517
var context = getLayerContext(index);
512518
context.fillStyle = fillColor;
513519
context.fillRect(x, y, width, height);
520+
cacheLayer(index);
514521
};
515522
self.floodFill = function (x, y, r, g, b, a, index) {
516523
index = (index == null) ? layerIndex : index;
@@ -565,6 +572,7 @@ function Croquis(imageDataList, properties) {
565572
}
566573
}
567574
context.putImageData(imageData, 0, 0);
575+
cacheLayer(index);
568576
};
569577
self.getLayerOpacity = function (index) {
570578
index = (index == null) ? layerIndex : index;
@@ -587,6 +595,18 @@ function Croquis(imageDataList, properties) {
587595
pushLayerVisibleUndo(index);
588596
layers[index].style.visibility = visible ? 'visible' : 'hidden';
589597
};
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+
};
590610
function makeColorData(imageData1x1) {
591611
var data = imageData1x1.data;
592612
var r = data[0];
@@ -602,7 +622,14 @@ function Croquis(imageDataList, properties) {
602622
if ((x < 0) || (x >= size.width) || (y < 0) || (y >= size.height))
603623
return null;
604624
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});
606633
};
607634
self.eyeDrop = function (x, y, baseColor) {
608635
if (self.pickColor(x, y) == null)
@@ -741,6 +768,7 @@ function Croquis(imageDataList, properties) {
741768
self.onUpped(x, y, pressure, dirtyRect);
742769
window.clearInterval(knockoutTick);
743770
window.clearInterval(tick);
771+
cacheLayer(self.getCurrentLayerIndex());
744772
}
745773
self.down = function (x, y, pressure) {
746774
if (isDrawing || isStabilizing)

0 commit comments

Comments
 (0)