mirror of
https://github.com/scratchfoundation/paper.js.git
synced 2025-01-05 20:32:00 -05:00
Add support for hit testing Raster items and getting the color of the pixel that was hit.
This commit is contained in:
parent
af9df0e1b8
commit
a96a92caba
2 changed files with 24 additions and 1 deletions
|
@ -39,7 +39,7 @@ HitResult = Base.extend(/** @lends HitResult# */{
|
|||
* @property
|
||||
* @name HitResult#type
|
||||
* @type String('segment', 'handle-in', 'handle-out', 'stroke', 'fill',
|
||||
* 'bounds', 'center')
|
||||
* 'bounds', 'center', 'pixel')
|
||||
*/
|
||||
|
||||
/**
|
||||
|
@ -69,6 +69,15 @@ HitResult = Base.extend(/** @lends HitResult# */{
|
|||
* @type CurveLocation
|
||||
*/
|
||||
|
||||
/**
|
||||
* If the HitResult has a type of 'pixel', this property refers to the color
|
||||
* of the pixel on the {@link Raster} that was hit.
|
||||
*
|
||||
* @property
|
||||
* @name HitResult#color
|
||||
* @type RgbColor
|
||||
*/
|
||||
|
||||
/**
|
||||
* If the HitResult has a type of 'stroke', 'segment', 'handle-in' or
|
||||
* 'handle-out', this property refers to the Segment that was hit or that
|
||||
|
|
|
@ -397,6 +397,20 @@ var Raster = this.Raster = PlacedItem.extend(/** @lends Raster# */{
|
|||
return this.getStrokeBounds(arguments[0]);
|
||||
},
|
||||
|
||||
_hitTest: function(point, options) {
|
||||
point = this._matrix._inverseTransform(point);
|
||||
if (point.isInside(new Rectangle(this._size).setCenter(0, 0))) {
|
||||
var that = this;
|
||||
return new HitResult('pixel', that, {
|
||||
offset: point.add(that._size.divide(2)).round(),
|
||||
// Becomes HitResult#color
|
||||
getColor: function() {
|
||||
return that.getPixel(this.offset);
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
draw: function(ctx, param) {
|
||||
if (param.selection) {
|
||||
var bounds = new Rectangle(this._size).setCenter(0, 0);
|
||||
|
|
Loading…
Reference in a new issue