在鼠标移动时绘制一个矩形,并使用 kineticjs 在鼠标向上选择该矩形内的所有形状
Drawing a rectangle on mousemove and selecting all shapes within that rectangle on mouseup using kineticjs?
我已经selectionrectangle
mousemove
(工作正常)。任何人都可以建议我,如何在mouseup
事件中选择矩形范围内的形状?我的代码是这样的(只有主要函数):
function onMouseDown() {
if (!isMouseDown) {
return
}
isMouseDown = true;
if (selectionrectangle) {
selectionrectangle.remove();
}
pointerPosition = stage.getPointerPosition();
x = Math.floor(pointerPosition.x)
y = Math.floor(pointerPosition.y )
originX = [];
originY = [];
originX.push(x);
originY.push(y);
selectionrectangle = new Kinetic.Rect({
x: originX[0],
y: originY[0],
width: 0,
height: 0,
stroke: 'pink',
strokeWidth: 3,
name: 'SelectionRectangle'
});
refRect = selectionrectangle;
refRect1 = selectionrectangle;
selectionrectangle.setListening(true);
mainLayer.add(selectionrectangle);
}
function onMouseMove() {
if (!isMouseDown) {
return;
};
isMouseDown = true;
pointerPosition = stage.getPointerPosition();
x = Math.floor(pointerPosition.x )
y = Math.floor(pointerPosition.y)
refRect.setWidth(x - refRect.getX());
refRect.setHeight(y - refRect.getY());
mainLayer.drawScene();
}
function onMouseUp() {
isMouseDown = false;
stage.add(mainLayer);
}
使用边界框是测试外壳的绝佳方法。
演示:http://jsfiddle.net/m1erickson/f9pe4/
边界框由选区的最左边、最右边、顶部和底部坐标组成。
refRect 的边界框可以放入这样的对象中:
// calculate a bounding box for the refRect
refRect.BoundingBox=refBoundingBox(refRect);
function refBoundingBox(refRect){
return({
left:refRect.getX(),
top:refRect.getY(),
right:refRect.getX()+refRect.getWidth(),
bottom:refRect.getY()+refRect.getHeight()
});
}
然后,您可以针对该边界框测试所有形状。
所有核心动力学形状分为 4 类:
- 矩形
- :矩形、图像、文本、精灵、 圆形
- :圆形,楔形,
- 多角点:直线、多边形、斑点、样条
- 无法定义的结构:自定义(本答案未检查)
测试矩形形状:矩形、图像、文本、精灵:
// Test enclosure for rectangular shapes:
// Rect,Image,Text,Sprite
function isRectInBB(refBB,shape){
var x=shape.getX();
var y=shape.getY();
return(
x>refBB.left &&
x+shape.getWidth()<refBB.right &&
y>refBB.top &&
y+shape.getHeight()<refBB.bottom
);
}
测试圆形:圆形、楔形:
// Test enclusure for circular shapes:
// Circle,Wedge
function isCircleInBB(refBB,shape){
var thisX=shape.getX();
var thisY=shape.getY();
var thisRadius=shape.getRadius();
return(
thisX-thisRadius>refBB.left &&
thisX+thisRadius<refBB.right &&
thisY-thisRadius>refBB.top &&
thisY+thisRadius<refBB.bottom
);
}
测试多段形状:直线、多边形、斑点、样条:
// Test enclosure for polypoint shapes:
// Line,Polygon,Blob,Spline
function isPolyInBB(refBB,shape){
var x=shape.getX();
var y=shape.getY();
return(
x+shape.minX>refBB.left &&
y+shape.minY>refBB.top &&
x+shape.maxX<refBB.right &&
y+shape.maxY<refBB.bottom);
}
检查这个老问题:KineticJS和区域内的形状
我提供了一些关于如何解决您的问题的信息和背景,OP有一个JSFiddle示例可以帮助您入门。
相关文章:
- KineticJS-用鼠标绘制线条
- KineticJS鼠标事件问题
- 在鼠标移动时绘制一个矩形,并使用 kineticjs 在鼠标向上选择该矩形内的所有形状
- kineticjs - 鼠标悬停不准确
- 使用鼠标滚动 kineticjs 旋转矩形
- KineticJS中的鼠标悬停事件问题
- 画布 KineticJS - 当我在形状上方释放鼠标时出错
- 停止动画,在鼠标放下时使用KineticJS增加圆半径
- KineticJS青少年没有'当鼠标悬停在阶段内时,不要运行
- KineticJS 5.1版用鼠标绘制线条
- Kineticjs和hammerjs:拖动舞台时正确更新鼠标位置
- 改变鼠标悬停时的KineticJS文本颜色
- Angularjs + Kineticjs鼠标事件
- 碰撞检测+鼠标事件在kineticjs
- Kineticjs文本填充干扰鼠标悬停事件
- KineticJS:获取鼠标按钮,x和y从'点击'圆圈上的事件
- Canvas Kineticjs -鼠标位置错误
- KineticJS -使用url自定义鼠标光标样式
- 当我使用数组时,kineticjs 鼠标事件不会侦听
- KineticJS -创建一个可拖动的线使用鼠标