不要偏移图层,而是将线条放在一个组中并旋转该组。
我假设你的全屏是 800x600 并且你想围绕中心旋转 400x300
方法
- 把你的台词放在一个组上
- 将您的组宽 x 高设置为 800x600
- 将您的组 x/y 设置为 400/300
- 将您的组偏移量设置为 400,300
- 要旋转,请调用 group.rotateDeg(degrees)
- 确保在每次旋转后使用 layer.draw()。
您的地图组示例:
var map=new Kinetic.Group({
x:400,
y:300,
width:800,
height:600,
offset:[400,300]
});
layer.add(map);
旋转功能示例
function rotateMap(t_angle) {
map.rotateDeg(t_angle);
layer.draw();
};
这是代码和小提琴:http: //jsfiddle.net/m1erickson/wDxcT/
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prototype</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.0.min.js"></script>
<style>
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:400px;
height:400px;
}
</style>
<script>
$(function(){
var stage = new Kinetic.Stage({
container: 'container',
width: 400,
height: 400
});
var layer = new Kinetic.Layer();
stage.add(layer);
var t_angle=30;
function rotateMap(t_angle) {
map.rotateDeg(t_angle);
layer.draw();
};
var map=new Kinetic.Group({
x:200,
y:200,
width:200,
height:200,
offset:[100,100]
});
layer.add(map);
var rect = new Kinetic.Rect({
x: 0,
y: 0,
width: 200,
height: 200,
fill: 'skyblue',
stroke: 'lightgray',
strokeWidth: 3
});
map.add(rect);
var north = new Kinetic.Text({
x: map.getWidth()/2-15,
y: 25,
text: 'N',
fontSize: 42,
fontFamily: 'Calibri',
fill: 'green'
});
map.add(north);
var c=new Kinetic.Circle({
x:200,
y:200,
radius:5,
fill:"green"
});
layer.add(c);
layer.draw();
$("#rotate").click(function(){
rotateMap(t_angle);
});
}); // end $(function(){});
</script>
</head>
<body>
<button id="rotate">rotate</button>
<div id="container"></div>
</body>
</html>