Skip to content

Commit df726a5

Browse files
committed
eyeDrop
1 parent 41aa577 commit df726a5

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
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.1.4",
4+
"version": "0.2.0",
55
"homepage": "https://github.com/disjukr/croquis.js",
66
"authors": [
77
"JongChan Choi <disjukr@naver.com>"

croquis.js

+36
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,42 @@ function Croquis(imageDataList, properties) {
587587
pushLayerVisibleUndo(index);
588588
layers[index].style.visibility = visible ? 'visible' : 'hidden';
589589
};
590+
function makeColorData(imageData1x1) {
591+
var data = imageData1x1.data;
592+
var r = data[0];
593+
var g = data[1];
594+
var b = data[2];
595+
var a = data[3];
596+
return {
597+
r: r, g: g, b: b, a: a,
598+
htmlColor: 'rgba(' + [r, g, b, a / 0xff].join(',') + ')'
599+
};
600+
}
601+
self.pickColor = function (x, y, index) {
602+
if ((x < 0) || (x >= size.width) || (y < 0) || (y >= size.height))
603+
return null;
604+
index = (index == null) ? layerIndex : index;
605+
return makeColorData(getLayerContext(index).getImageData(x, y, 1, 1));
606+
};
607+
self.eyeDrop = function (x, y, baseColor) {
608+
if (self.pickColor(x, y) == null)
609+
return null;
610+
baseColor = (baseColor == null) ? '#fff' : baseColor;
611+
var plane = document.createElement('canvas');
612+
plane.width = 1;
613+
plane.height = 1;
614+
var planeContext = plane.getContext('2d');
615+
planeContext.fillStyle = baseColor;
616+
planeContext.fillRect(0, 0, 1, 1);
617+
for (var i = 0; i < layers.length; ++i) {
618+
if (!self.getLayerVisible(i))
619+
continue;
620+
planeContext.globalAlpha = self.getLayerOpacity(i);
621+
planeContext.fillStyle = self.pickColor(x, y, i).htmlColor;
622+
planeContext.fillRect(0, 0, 1, 1);
623+
}
624+
return makeColorData(planeContext.getImageData(0, 0, 1, 1));
625+
};
590626
var tool;
591627
var toolStabilizeLevel = 0;
592628
var toolStabilizeWeight = 0.8;

0 commit comments

Comments
 (0)