添加一個div id作爲「容器」,包括腳本標記中的以下腳本:
var stage = new Kinetic.Stage({
container: 'container',
width: 1000,
height: 1000
});
var layer = new Kinetic.Layer();
var height = 200;
var width = 300;
var x = 400;
var y = 300;
var group = new Kinetic.Group({
x: x,
y: y,
offset:[x + width/2,y + height/2],
draggable: true
});
var yellowRect = new Kinetic.Rect({
x: x,
y: y,
width: width,
height: height,
fill: 'yellow',
stroke: 'black',
strokeWidth: 4,
});
var greenCircle = new Kinetic.Circle({
x: x,
y: y,
radius:5,
fill: 'green',
stroke: 'black',
strokeWidth: 4
});
var orangeCircle = new Kinetic.Circle({
x: x + width,
y: y,
radius:5,
fill: 'orange',
stroke: 'black',
strokeWidth: 4,
});
var redCircle = new Kinetic.Circle({
x: x,
y: y + height,
radius:5,
fill: 'red',
stroke: 'black',
strokeWidth: 4,
});
var blueCircle = new Kinetic.Circle({
x: x + width,
y: y + height,
radius:5,
fill: 'blue',
stroke: 'black',
strokeWidth: 4,
});
group.add(yellowCircle);
group.add(greenCircle);
group.add(orangeCircle);
group.add(redCircle);
group.add(blueCircle);
layer.add(group);
stage.add(layer);
// one revolution per 4 seconds
var angularSpeed = Math.PI/2;
var anim = new Kinetic.Animation(function(frame) {
var angleDiff = frame.timeDiff * angularSpeed/500;
group.rotate(angleDiff);
}, layer);
anim.start();
您可以使用變換來做到這一點,而無需創建額外的羣體 - http://kineticjs.com/docs/Kinetic.Transform.html – AlexFoxGill