即使小组扩大了,如何缩小小组以适应舞台
此图显示了起始“蓝色”组 - 未加宽且未缩放。
此图显示了蓝色组扩大到舞台之外并缩小后的情况。
以下是如何在不“浮动”的情况下缩小您的组:
- 撤消任何先前的组偏移量(按任何先前缩放因子的量)。
- 计算新的比例因子(例如:按比例缩小)。
- 保存新的缩放因子以用于将来的任何缩放。
- 根据新的比例因子计算新的组偏移量。
- 将组的偏移量设置为新缩放的偏移量。
- 重绘图层。
您将需要向 drawingGroup 添加与缩放相关的几个属性:
// the current SCALED offset
drawingGroup.offsetX=0;
drawingGroup.offsetY=0;
// the point from which all scaling will occur
drawingGroup.scalePtX=0;
drawingGroup.scalePtY=0;
// the amount of current scaling applied to the group
// ==1.00 is the beginning un-scaled group
// >1.00 scales the group larger
// <1.00 scales the group smaller
drawingGroup.scaleFactor=1.00;
然后将此方法添加到 drawingGroup 以实际进行缩放:
drawingGroup.scaleBy=function(scaleChange){
// undo previous offset
this.offsetX += this.scalePtX/this.scaleFactor;
this.offsetY += this.scalePtY/this.scaleFactor;
// calc new scaling factor
var newScaleFactor = this.scaleFactor*(1+scaleChange);
// create new offset
this.offsetX -= this.scalePtX/newScaleFactor;
this.offsetY -= this.scalePtY/newScaleFactor;
// set new scale factor
this.scaleFactor=newScaleFactor;
// do the new offset
this.setOffset(this.offsetX,this.offsetY);
// do the new scale
this.setScale(this.scaleFactor);
layer.draw();
};
这是一个测试代码,它既扩大了组的范围,又自动缩小它以适应舞台:
此测试代码还在缩减后重新定位组。
// widen by 20px
// if wider than stage, scale down by 15%
$("#wider").click(function(){
drawingGroup.setWidth(drawingGroup.getWidth()+20);
rect.setWidth(drawingGroup.getWidth());
var width=drawingGroup.getWidth()*drawingGroup.scaleFactor;
if(drawingGroup.getX()+width>stageWidth){
drawingGroup.scaleBy(-0.15);
recenter();
}
setStatus();
layer.draw();
});
function recenter(){
var w=drawingGroup.getWidth()*drawingGroup.scaleFactor;
var h=drawingGroup.getHeight()*drawingGroup.scaleFactor;
drawingGroup.setX(stage.getWidth()/2-w/2);
drawingGroup.setY(stage.getHeight()/2-h/2);
}
请注意,setX()/setY() 可以正常用于组。
只需确保通过乘以 scalingFactor来计算缩放组的宽度/高度。
这是完整的代码和小提琴:http: //jsfiddle.net/m1erickson/UyDbW/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.5.4.min.js"></script>
<style>
body{ padding:15px; }
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:300px;
height:300px;
}
</style>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 300,
height: 300
});
var layer = new Kinetic.Layer();
stage.add(layer);
// objects used in jquery events
var drawingGroup;
var rect,circle;
var stageWidth=stage.getWidth();
var stageHeight=stage.getHeight();
drawingGroup=new Kinetic.Group({
x:stageWidth/2-50,
y:stageHeight/2-50,
width:100,
height:100,
});
drawingGroup.offsetX=0;
drawingGroup.offsetY=0;
drawingGroup.scalePtX=0;
drawingGroup.scalePtY=0;
drawingGroup.scaleFactor=1.00;
drawingGroup.scaleBy=function(scaleChange){
// undo previous offset
this.offsetX += this.scalePtX/this.scaleFactor;
this.offsetY += this.scalePtY/this.scaleFactor;
// calc new scaling factor
var newScaleFactor = this.scaleFactor*(1+scaleChange);
// create new offset
this.offsetX -= this.scalePtX/newScaleFactor;
this.offsetY -= this.scalePtY/newScaleFactor;
// set new scale factor
this.scaleFactor=newScaleFactor;
// do the new offset
this.setOffset(this.offsetX,this.offsetY);
// do the new scale
this.setScale(this.scaleFactor);
layer.draw();
};
layer.add(drawingGroup);
var rect=new Kinetic.Rect({
x:0,
y:0,
width:100,
height:100,
stroke:"lightgray",
fill:"skyblue"
});
drawingGroup.add(rect);
var circle=new Kinetic.Circle({
x:50,
y:50,
radius:20,
fill:"blue"
});
drawingGroup.add(circle);
var status=new Kinetic.Text({
x:15,
y:15,
text:"width=100, scale=1.00",
fontSize:18,
fill:"red"
});
layer.add(status);
setStatus();
layer.draw();
function setStatus(){
var width=drawingGroup.getWidth();
var scale=Math.round(drawingGroup.scaleFactor*100);
status.setText("Width: "+width+", Scale: "+scale+"%");
}
// widen by 20px
// if wider than stage, scale down by 15%
$("#wider").click(function(){
drawingGroup.setWidth(drawingGroup.getWidth()+20);
rect.setWidth(drawingGroup.getWidth());
var width=drawingGroup.getWidth()*drawingGroup.scaleFactor;
if(drawingGroup.getX()+width>stageWidth){
drawingGroup.scaleBy(-0.15);
recenter();
}
setStatus();
layer.draw();
});
function recenter(){
var w=drawingGroup.getWidth()*drawingGroup.scaleFactor;
var h=drawingGroup.getHeight()*drawingGroup.scaleFactor;
drawingGroup.setX(stage.getWidth()/2-w/2);
drawingGroup.setY(stage.getHeight()/2-h/2);
}
}); // end $(function(){});
</script>
</head>
<body>
<p>Blue group will scale if extending past stage</p>
<button id="wider">Wider</button>
<div id="container"></div>
</body>
</html>