admin管理员组文章数量:1023827
I am Trying to tweak this example .html to restrict the drag to within the container svg.
Somewhat like or Basically I want to restrict objects moving out from the bounding box while dragging.
Here is the tweaked code added to move function ( / )
if(thisBox.x < 0){
ddx = 0;
}else if(thisBox.x>width-thisBox.width ){
ddx = width-thisBox.width;
}else {
ddx = this.ox + dx;
}
if(thisBox.y < 0){
ddy = 0;
}else if(thisBox.y>height-thisBox.height ){
ddy = height-thisBox.height;
}else{
ddy = this.oy + dy;
}
This partially works, when rectangle is moved beyond boundary it kind of dances on the edge! while the circle and ellipse get stuck to the edge.
So how to restrict element drag with constrained movement within parent bounding box in this case svg
I am Trying to tweak this example http://raphaeljs./graffle.html to restrict the drag to within the container svg.
Somewhat like http://bl.ocks/1557377 or http://jqueryui./demos/draggable/#constrain-movement Basically I want to restrict objects moving out from the bounding box while dragging.
Here is the tweaked code added to move function ( http://jsfiddle/f4mFQ/1/ )
if(thisBox.x < 0){
ddx = 0;
}else if(thisBox.x>width-thisBox.width ){
ddx = width-thisBox.width;
}else {
ddx = this.ox + dx;
}
if(thisBox.y < 0){
ddy = 0;
}else if(thisBox.y>height-thisBox.height ){
ddy = height-thisBox.height;
}else{
ddy = this.oy + dy;
}
This partially works, when rectangle is moved beyond boundary it kind of dances on the edge! while the circle and ellipse get stuck to the edge.
So how to restrict element drag with constrained movement within parent bounding box in this case svg
Share Improve this question asked Sep 22, 2012 at 23:38 Prashant BhatePrashant Bhate 11.1k8 gold badges51 silver badges83 bronze badges2 Answers
Reset to default 4 +50One issue is that you're when you set ddx
or ddy
to be positioned on a side you then feed that value back as the center point of the ellipses so they 'snap' off to the side.
The second thing is you do your putations on the top left of the bounding box of this, not the actual calculated position of this (ox + dx). That's what's causing the confusion.
This isn't the prettiest implementation but I wanted to minimize the changes to your code, replace the move function with this one:
move = function (dx, dy) {
var width =this.paper.width,
height = this.paper.height,
thisBox = this.getBBox()
//, box = {
// "x" :thisBox.width,
// "y" :thisBox.height,
// "x2" :width-thisBox.width,
// "y2" :height-thisBox.height,
// "width" :width-thisBox.width,
// "height" :height-thisBox.height
// };
// var outOfBound=!Raphael.isBBoxIntersect(thisBox,box)
;
console.log(thisBox.width,width,thisBox.x,thisBox
,this.ox);
var ddx = this.ox + dx;
var ddy = this.oy + dy;
if (this.type == 'ellipse') {
ddx -= thisBox.width / 2;
ddy -= thisBox.height / 2;
}
if(ddx < 0){
ddx = 0;
}else if(ddx>width-thisBox.width ){
ddx = width-thisBox.width;
}
if(ddy < 0){
ddy = 0;
}else if(ddy>height-thisBox.height ){
ddy = height-thisBox.height;
}
var att = this.type == "rect" ? {x: ddx, y: ddy} : {cx: ddx + (thisBox.width / 2), cy: ddy + (thisBox.height / 2)};
this.attr(att);
for (var i = connections.length; i--;) {
r.connection(connections[i]);
}
r.safari();
}
At the start we calculate ddx
and ddy
as the top left based on the position of this
. (Adjusted if it's a ellipse, as ox
and oy
are the center co-ordinates).
Then it's much the same as before, although when we set the value back we re-adjust the values for the ellipse. You could change what values are stored/used to avoid the duplicate calculation.
That works for me.
Edit
Copied code into it's own fiddle.
Hi I have updated the fiddle with this code, this seems working although not perfect,
The code changed
if (thisBox.x < 0) {
ddx = 0 + thisBox.width/2;//the position 0 was problem
} else if (thisBox.x > width - thisBox.width) {
ddx = width - thisBox.width;
} else {
ddx = this.ox +dx;
}
if (thisBox.y < 0) {
ddy = 0+thisBox.height/2;//the position 0 was problem
} else if (thisBox.y > height - thisBox.height) {
ddy = height - thisBox.height;
} else {
ddy = this.oy + dy;
}
I have updated the fiddle
Also I have updated some CSS to make it more clear and error free.
Update: Vibration,
I have updated fiddle2
that removes vibration on the top and left side,
Please check and reply
Also see this fiddle3
that works with the rounds/ellipses well but have problem with rectangle's I think there is something I must have missed, but working fine with ellipses so that should help
I am Trying to tweak this example .html to restrict the drag to within the container svg.
Somewhat like or Basically I want to restrict objects moving out from the bounding box while dragging.
Here is the tweaked code added to move function ( / )
if(thisBox.x < 0){
ddx = 0;
}else if(thisBox.x>width-thisBox.width ){
ddx = width-thisBox.width;
}else {
ddx = this.ox + dx;
}
if(thisBox.y < 0){
ddy = 0;
}else if(thisBox.y>height-thisBox.height ){
ddy = height-thisBox.height;
}else{
ddy = this.oy + dy;
}
This partially works, when rectangle is moved beyond boundary it kind of dances on the edge! while the circle and ellipse get stuck to the edge.
So how to restrict element drag with constrained movement within parent bounding box in this case svg
I am Trying to tweak this example http://raphaeljs./graffle.html to restrict the drag to within the container svg.
Somewhat like http://bl.ocks/1557377 or http://jqueryui./demos/draggable/#constrain-movement Basically I want to restrict objects moving out from the bounding box while dragging.
Here is the tweaked code added to move function ( http://jsfiddle/f4mFQ/1/ )
if(thisBox.x < 0){
ddx = 0;
}else if(thisBox.x>width-thisBox.width ){
ddx = width-thisBox.width;
}else {
ddx = this.ox + dx;
}
if(thisBox.y < 0){
ddy = 0;
}else if(thisBox.y>height-thisBox.height ){
ddy = height-thisBox.height;
}else{
ddy = this.oy + dy;
}
This partially works, when rectangle is moved beyond boundary it kind of dances on the edge! while the circle and ellipse get stuck to the edge.
So how to restrict element drag with constrained movement within parent bounding box in this case svg
Share Improve this question asked Sep 22, 2012 at 23:38 Prashant BhatePrashant Bhate 11.1k8 gold badges51 silver badges83 bronze badges2 Answers
Reset to default 4 +50One issue is that you're when you set ddx
or ddy
to be positioned on a side you then feed that value back as the center point of the ellipses so they 'snap' off to the side.
The second thing is you do your putations on the top left of the bounding box of this, not the actual calculated position of this (ox + dx). That's what's causing the confusion.
This isn't the prettiest implementation but I wanted to minimize the changes to your code, replace the move function with this one:
move = function (dx, dy) {
var width =this.paper.width,
height = this.paper.height,
thisBox = this.getBBox()
//, box = {
// "x" :thisBox.width,
// "y" :thisBox.height,
// "x2" :width-thisBox.width,
// "y2" :height-thisBox.height,
// "width" :width-thisBox.width,
// "height" :height-thisBox.height
// };
// var outOfBound=!Raphael.isBBoxIntersect(thisBox,box)
;
console.log(thisBox.width,width,thisBox.x,thisBox
,this.ox);
var ddx = this.ox + dx;
var ddy = this.oy + dy;
if (this.type == 'ellipse') {
ddx -= thisBox.width / 2;
ddy -= thisBox.height / 2;
}
if(ddx < 0){
ddx = 0;
}else if(ddx>width-thisBox.width ){
ddx = width-thisBox.width;
}
if(ddy < 0){
ddy = 0;
}else if(ddy>height-thisBox.height ){
ddy = height-thisBox.height;
}
var att = this.type == "rect" ? {x: ddx, y: ddy} : {cx: ddx + (thisBox.width / 2), cy: ddy + (thisBox.height / 2)};
this.attr(att);
for (var i = connections.length; i--;) {
r.connection(connections[i]);
}
r.safari();
}
At the start we calculate ddx
and ddy
as the top left based on the position of this
. (Adjusted if it's a ellipse, as ox
and oy
are the center co-ordinates).
Then it's much the same as before, although when we set the value back we re-adjust the values for the ellipse. You could change what values are stored/used to avoid the duplicate calculation.
That works for me.
Edit
Copied code into it's own fiddle.
Hi I have updated the fiddle with this code, this seems working although not perfect,
The code changed
if (thisBox.x < 0) {
ddx = 0 + thisBox.width/2;//the position 0 was problem
} else if (thisBox.x > width - thisBox.width) {
ddx = width - thisBox.width;
} else {
ddx = this.ox +dx;
}
if (thisBox.y < 0) {
ddy = 0+thisBox.height/2;//the position 0 was problem
} else if (thisBox.y > height - thisBox.height) {
ddy = height - thisBox.height;
} else {
ddy = this.oy + dy;
}
I have updated the fiddle
Also I have updated some CSS to make it more clear and error free.
Update: Vibration,
I have updated fiddle2
that removes vibration on the top and left side,
Please check and reply
Also see this fiddle3
that works with the rounds/ellipses well but have problem with rectangle's I think there is something I must have missed, but working fine with ellipses so that should help
本文标签:
版权声明:本文标题:javascript - raphaeljs How to restrict element drag with constrained movement (within parent bounding box) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745577298a2157098.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论