我在尝试在动力学层上创建一个圆圈时遇到了很多问题。基本上我正在尝试创建一个绘画应用程序,但我认为我需要一次解决这个问题。所以我只想能够从字面上拖放一个圆圈。我有一些东西,但我知道它绝对充满了错误。拜托,我真的需要一些指导,我很长时间没有使用 KineticJs 或 javascript,所以我真的很想了解这一切是如何工作的。谢谢你,有一个美好的一天!
<!DOCTYPE html>
<html>
<head>
<meta chartset="utf-8">
<title>Creating a Cirlce</title>
<style type="text/css">
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<script type="text/javascript" src="kinetic-v4.5.4.min.js"></script>
<div id="container"></div>
<script type="text/javascript">
window.onload = function() {
var stage = new Kinectic.Stage({
container: "container",
width: 400,
height: 400
});
var layer = new Kinetic.Layer();
background = new Kinetic.Rect({
x: 0,
y: 0,
width: stage.getWidth(),
height: stage.getHeight(),
fill: "white"
});
var Circle = new Kinetic.Circle({
x: 0,
y: 0,
radius: 0,
fill: "red",
stroke: "black",
strokeWidth: 4
});
layer.add(background);
layer.add(Circle);
stage.add(layer);
var moving = false;
stage.on("mousedown", function(){
if (moving) {
moving = false;
layer.draw();
}
else {
var pos = stage.getMousePosition();
moving = true;
Circle.x = pos.x;
Circle.y = pos.y
Circle.radius = 0;
layer.drawScene();
}
});
stage.on("mousemove", function(){
if (moving) {
var pos = stage.getMousePosition();
var x = pos.x;
var y = pos.y;
Circle.x = pos.x;
Cirlce.y = pos.y;
Cirlce.radius = 0.5 * Math.sqrt(Math.pow(x,2)+Math.pow(y,2));
Cirlce.fill = "red";
Circle.stroke = "black";
Cirlce.strokeWidth = 1;
}
});
stage.on("mouseup", function() {
moving = false;
})
}
</script>
</body>