Paper.JS - Unable to resize rectangle after reaching minimum size

I created a small script based on the HitTest example from paperjs.org. The idea is to have a Rectangle and be able to move and resize it. It almost works, but when I resize it to the minimum size I specified (10x10), for some reason I can't resize it anymore, even if I canceled the drag event and try again.

var hitOptions = {
	segments: true,
	stroke: true,
	fill: true,
	tolerance: 1
};

project.currentStyle = {
    fillColor: 'green',
    strokeColor: 'black'
};

var rect_a = new Path.Rectangle(new Point(50, 50), 50);

var segment, path, hitType;
var clickPos = null;
var movePath = false;
var minHeight = 10;
var minWidth = 10;

function onMouseDown(event) {
	segment = path = null;
	var hitResult = project.hitTest(event.point, hitOptions);
	if (!hitResult)
		return;

    hitType = hitResult.type;

	if (event.modifiers.shift) {
		if (hitResult.type == 'segment') {
			hitResult.segment.remove();
		};
		return;
	}

	if (hitResult) {
		path = hitResult.item;
		if (hitResult.type == 'segment') {
			segment = hitResult.segment;
		}
	}
	movePath = hitResult.type == 'fill';
	if (movePath) {
		project.activeLayer.addChild(hitResult.item);
	}

	clickPos = checkHitPosition(event);
}

function onMouseMove(event) {
    changeCursor(event);
	project.activeLayer.selected = false;
	if (event.item)
		event.item.selected = true;
}

function onMouseDrag(event) {
	if (hitType == "stroke" || hitType == "segment") {
        resizeRectangle(path, event);
	} else {
		path.position += event.delta;
	}
}

function resizeRectangle(path, event) {
    switch(clickPos) {
        case "SE" :
            resizeBottom(path, event);
            resizeRight(path, event);
            break;
        case "NE" :
            resizeTop(path, event);
            resizeRight(path, event);
            break;
        case "SW" :
            resizeBottom(path, event);
            resizeLeft(path, event);
            break;
        case "NW" :
            resizeTop(path, event);
            resizeLeft(path, event);
            break;
        case "S"  :
            resizeBottom(path, event);
            break;
        case "N"  :
            resizeTop(path, event);
            break;
        case "E"  :
            resizeRight(path, event);
            break;
        case "W"  :
            resizeLeft(path, event);
            break;
    }
}

function resizeTop(path, event) {
    if(path.bounds.height >= minHeight) {
        path.bounds.top += event.delta.y;
    }
}

function resizeBottom(path, event) {
    if(path.bounds.height >= minHeight) {
        path.bounds.bottom += event.delta.y;
    }
}

function resizeLeft(path, event) {
    if(path.bounds.width >= minWidth) {
        path.bounds.left  += event.delta.x;
    }
}

function resizeRight(path, event) {
    if(path.bounds.width >= minWidth) {
        path.bounds.right  += event.delta.x;
    }
}

function checkHitPosition(event) {
    var hitResult = project.hitTest(event.point, hitOptions);
    var clickPosition = null;

    if (hitResult) {
        if (hitResult.type == 'stroke' || hitResult.type == 'segment') {
            var bounds = hitResult.item.bounds;
            var point = hitResult.point;

            if (bounds.top == point.y) {
                clickPosition = "N";
            }

            if (bounds.bottom == point.y) {
                clickPosition = "S";
            }

            if (bounds.left == point.x) {
                clickPosition = "W";
            }

            if (bounds.right == point.x) {
                clickPosition = "E";
            }

            if (bounds.top == point.y && bounds.left == point.x) {
                clickPosition = "NW";
            }

            if (bounds.top == point.y && bounds.right == point.x) {
                clickPosition = "NE";
            }

            if (bounds.bottom == point.y && bounds.left == point.x) {
                clickPosition = "SW";
            }

            if (bounds.bottom == point.y && bounds.right == point.x) {
                clickPosition = "SE";
            }
        } else {
            clickPosition = "C";
        }
    }
    return clickPosition;
};

function changeCursor(event) {
    var hitPosition = checkHitPosition(event);
    if(hitPosition == null ) {
        document.body.style.cursor = "auto";
    } else {
        if (hitPosition == "C") {
            document.body.style.cursor = "all-scroll";
        } else {
            document.body.style.cursor = hitPosition + "-resize";
        }
    }
}
      

Run code


+3


source to share


1 answer


The problem is that the code shrinks the rectangle below the minimum dimensions (minWidth and minHeight). You can add the line

console.log(path.bounds.width, path.bounds.height)

      

at the bottom of the resizeRectangle to see how the rectangle gets smaller than minWidth and minHeight.

Then, when each function resizeXYZZY

is called, the check if(path.bounds.height >= minHeight)

(or width) fails and no change occurs, so the rectangle cannot be made large again.



To fix this, the resizeTop function should work differently:

var adj = min(event.delta.y, path.bounds.height-minHeight);
path.bounds.top += adj;

      

And corresponding changes need to be made for resizeBottom, Left and Right.

This avoids the rectangle being smaller than the minimum and also removes any check that prevents it from growing again.

0


source







All Articles