mirror of
https://github.com/scratchfoundation/scratch-paint.git
synced 2024-12-22 21:42:30 -05:00
Tighten bitmap selection bounds
This commit is contained in:
parent
b0164e7783
commit
de502d6843
2 changed files with 19 additions and 8 deletions
|
@ -320,11 +320,18 @@ const columnBlank_ = function (imageData, width, x, top, bottom) {
|
|||
return true;
|
||||
};
|
||||
|
||||
// Adapted from Tim Down's https://gist.github.com/timdown/021d9c8f2aabc7092df564996f5afbbf
|
||||
// Get bounds, trimming transparent pixels from edges.
|
||||
const getHitBounds = function (raster) {
|
||||
const width = raster.width;
|
||||
const imageData = raster.getImageData(raster.bounds);
|
||||
/**
|
||||
* Get bounds around the contents of a raster, trimming transparent pixels from edges.
|
||||
* Adapted from Tim Down's https://gist.github.com/timdown/021d9c8f2aabc7092df564996f5afbbf
|
||||
* @param {paper.Raster} raster The raster to get the bounds around
|
||||
* @param {paper.Rectangle} [rect] Optionally, an alternative bounding rectangle to limit the check to.
|
||||
* @returns {paper.Rectangle} The bounds around the opaque area of the passed raster
|
||||
* (or opaque within the passed rectangle)
|
||||
*/
|
||||
const getHitBounds = function (raster, rect) {
|
||||
const bounds = rect || raster.bounds;
|
||||
const width = bounds.width;
|
||||
const imageData = raster.getImageData(bounds);
|
||||
let top = 0;
|
||||
let bottom = imageData.height;
|
||||
let left = 0;
|
||||
|
@ -343,7 +350,7 @@ const getHitBounds = function (raster) {
|
|||
left = right = imageData.width / 2;
|
||||
}
|
||||
|
||||
return new paper.Rectangle(left, top, right - left, bottom - top);
|
||||
return new paper.Rectangle(left + bounds.left, top + bounds.top, right - left, bottom - top);
|
||||
};
|
||||
|
||||
const trim_ = function (raster) {
|
||||
|
|
|
@ -3,6 +3,7 @@ import {rectSelect} from '../guides';
|
|||
import {clearSelection, processRectangularSelection} from '../selection';
|
||||
import {getRaster} from '../layer';
|
||||
import {ART_BOARD_WIDTH, ART_BOARD_HEIGHT} from '../view';
|
||||
import {getHitBounds} from '../../helper/bitmap';
|
||||
|
||||
/** Tool to handle drag selection. A dotted line box appears and everything enclosed is selected. */
|
||||
class SelectionBoxTool {
|
||||
|
@ -44,8 +45,8 @@ class SelectionBoxTool {
|
|||
}
|
||||
onMouseUpBitmap (event) {
|
||||
if (event.event.button > 0) return; // only first mouse button
|
||||
if (this.selectionRect && this.selectionRect.bounds.intersects(getRaster().bounds)) {
|
||||
const rect = new paper.Rectangle({
|
||||
if (this.selectionRect) {
|
||||
let rect = new paper.Rectangle({
|
||||
from: new paper.Point(
|
||||
Math.max(0, Math.round(this.selectionRect.bounds.topLeft.x)),
|
||||
Math.max(0, Math.round(this.selectionRect.bounds.topLeft.y))),
|
||||
|
@ -54,6 +55,9 @@ class SelectionBoxTool {
|
|||
Math.min(ART_BOARD_HEIGHT, Math.round(this.selectionRect.bounds.bottomRight.y)))
|
||||
});
|
||||
|
||||
// Trim/tighten selection bounds inwards to only the opaque region, excluding transparent pixels
|
||||
rect = getHitBounds(getRaster(), rect);
|
||||
|
||||
if (rect.area) {
|
||||
// Pull selected raster to active layer
|
||||
const raster = getRaster().getSubRaster(rect);
|
||||
|
|
Loading…
Reference in a new issue