Skip to content

Commit a96a92c

Browse files
committed
Add support for hit testing Raster items and getting the color of the pixel that was hit.
1 parent af9df0e commit a96a92c

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

src/item/HitResult.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ HitResult = Base.extend(/** @lends HitResult# */{
3939
* @property
4040
* @name HitResult#type
4141
* @type String('segment', 'handle-in', 'handle-out', 'stroke', 'fill',
42-
* 'bounds', 'center')
42+
* 'bounds', 'center', 'pixel')
4343
*/
4444

4545
/**
@@ -69,6 +69,15 @@ HitResult = Base.extend(/** @lends HitResult# */{
6969
* @type CurveLocation
7070
*/
7171

72+
/**
73+
* If the HitResult has a type of 'pixel', this property refers to the color
74+
* of the pixel on the {@link Raster} that was hit.
75+
*
76+
* @property
77+
* @name HitResult#color
78+
* @type RgbColor
79+
*/
80+
7281
/**
7382
* If the HitResult has a type of 'stroke', 'segment', 'handle-in' or
7483
* 'handle-out', this property refers to the Segment that was hit or that

src/item/Raster.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,20 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
397397
return this.getStrokeBounds(arguments[0]);
398398
},
399399

400+
_hitTest: function(point, options) {
401+
point = this._matrix._inverseTransform(point);
402+
if (point.isInside(new Rectangle(this._size).setCenter(0, 0))) {
403+
var that = this;
404+
return new HitResult('pixel', that, {
405+
offset: point.add(that._size.divide(2)).round(),
406+
// Becomes HitResult#color
407+
getColor: function() {
408+
return that.getPixel(this.offset);
409+
}
410+
});
411+
}
412+
},
413+
400414
draw: function(ctx, param) {
401415
if (param.selection) {
402416
var bounds = new Rectangle(this._size).setCenter(0, 0);

0 commit comments

Comments
 (0)