Paper.js通过拖动调整栅格/ TextItem /路径

时间:2015-10-13 20:48:04

标签: javascript javascript-events resize mouseevent paperjs

我知道我可以scale Raster Paper.js TextItem以及PathRaster

但是,我想在拖动TextItemPathRectangle的选择线或边界框时执行此操作,就像调整大小时一样像Word这样的程序中的图像。这些边界形成sketch对象。我可以使用fitBounds方法挂钩吗?或者更广泛地说,如何在Raster,TextItem或Path的选择线上捕获鼠标拖动事件?我想,一旦我能做到这一点,我就可以使用scale方法来增长/缩小对象。

这是一个Paper.js documentation,可以帮助你开始和实验,借用@Christoph。另请参阅Paper.js的http://jsfiddle.net/und26dep/

1 个答案:

答案 0 :(得分:3)

构建实际的实现将是麻烦的,但这是一个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

只需计算画布的位置,然后拖走:)