Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paper.js Resize Raster/TextItem/Path by Dragging

I'm aware that I can scale a Raster object in Paper.js, as well as a TextItem and a Path.

However, I'd like to do this on dragging the selection lines or bounding box of a Raster, TextItem, or Path, just as you would when resizing an image in a program like Word. These bounds form a Rectangle object. Can I hook into this, perhaps using the fitBounds method? Or more broadly, how can I capture a mouse drag event on the selection lines of a Raster, TextItem, or Path? I suppose once I can do that I can use the scale method to grow/shrink the object.

Here's a Paper.js sketch to get you started and for experimentation, borrowed from @Christoph. See also the documentation for Paper.js.

like image 944
Scotty H Avatar asked Oct 13 '15 20:10

Scotty H


1 Answers

Building the actual implementation would be bothersome, but here is a POC https://jsfiddle.net/f8h3j7v4/

c.addEventListener('mousedown',function(e){//c = context, check the fiddle
//Calculate the position of the edges, currently hardcoded values for fiddle
//For example getPosition(c).y + y * scaleY
//I should mention that rotate starts at the top left corner; 
//the whole canvas gets rotated(+transform exists)
//There is actually a pretty clever way to handle rotation;
//rotate the mouse position
if(e.clientY > 15 && e.clientY < 25)
    dragNorth = true
else
    dragNorth = false
if(e.clientX > 15 && e.clientX < 25)
    dragWest = true
else
    dragWest = false
if(e.clientX > 165 && e.clientX < 175)
    dragEast = true
else
    dragEast = false
if(e.clientY > 165 && e.clientY < 175)
    dragSouth = true
else
    dragSouth = false
})

function getPosition(element) {
var xPosition = 0;
var yPosition = 0;

while(element) {
    xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
    yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
    element = element.offsetParent;
}
return { x: xPosition, y: yPosition };
}
//Thanks to  
//http://www.kirupa.com/html5/get_element_position_using_javascript.htm

Just calc the position of the canvas and then drag away :)

like image 91
Olavi Sau Avatar answered Sep 26 '22 12:09

Olavi Sau