我喜欢使用单击以任意角度旋转画布中的文本,但此处文本将顺时针旋转并逆时针旋转。我喜欢将它顺时针旋转到我想要的角度。
代码:
<html>
<head>
<style>
body { margin: 0px; padding: 0px; }
canvas { border: 1px solid #9C9898; }
</style>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-0.2.js">
</script>
<script>
window.onload = function() {
var angle = 0;
var stage = new Kinetic.Stage({
container: "container",
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var text = new Kinetic.Text({
x:225,
y: 80,
text: "Simple",
fontSize: 30,
fontFamily: "Calibri",
textFill: "black",
draggable: true
});
text.on("click", function(){
angle=getRandomInt(1,4);
text.transitionTo({
rotation:Math.PI*angle/2,
duration:1
});
});
layer.add(text);
stage.add(layer);
}
function getRandomInt (min, max) {
return (Math.random() * (max - min + 1)) + min;
}
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>
答案 0 :(得分:2)
你得到一个随机的角度。
您必须获取鼠标位置,然后计算鼠标坐标与文本位置之间的角度
看看是否有帮助:
<html>
<head>
<style>
body { margin: 0px; padding: 0px; }
canvas { border: 1px solid #9C9898; }
</style>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.0.1.js"> </script>
<script>
window.onload = function() {
var angle = 0;
var stage = new Kinetic.Stage({
container: "container",
width: 578,
height: 200
});
var layer = new Kinetic.Layer();
var rect = new Kinetic.Rect({x:0,y:0, width:stage.getWidth(), height:stage.getHeight()});
var text = new Kinetic.Text({
x:225,
y: 80,
text: "Simple",
fontSize: 30,
fontFamily: "Calibri",
textFill: "black",
draggable: true
});
rect.on("click", function(){
var mousePos = stage.getMousePosition(); /*gets the mouse position*/
angle = getAngle(text.getX(), text.getY(), mousePos.x, mousePos.y);
text.transitionTo({
rotation: angle,
duration: 1
});
});
layer.add(rect);
layer.add(text);
stage.add(layer);
}
function getAngle (cx, cy, px, py) { /*calculate the angle between two points*/
var x = cx - px;
var y = cy - py;
return Math.atan2(-y, -x);
}
</script>
</head>
<body>
<div id="container"></div>
</body>
</html>