我已经创建了一个我遇到的问题的小例子 - 我正在向图层添加一个矩形,如下所示(按下“添加”按钮时)。然后我可以通过按删除按钮删除相同的项目...
......到目前为止一切都很好......
现在我尝试添加另一个项目.. 但是,它不会再添加到图层中,尽管图层似乎仍然存在。
谁能看到我做错了什么?
<html>
<head>
<title>Add / Remove Blocks</title>
<style>
body {
margin: 0px;
padding: 0px;
}
#buttons > input {
padding: 10px;
display: block;
margin-top: 5px;
}
</style>
<!-- Include script files -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="scripts/kinetic-v4.2.0.min.js"></script>
<script type="text/javascript">
var layer;
var stage;
$(document).ready(function() {
// Create the stage
stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
// Now add a new layer
layer = new Kinetic.Layer();
// add the layer to the stage
stage.add(layer);
});
// Add a block to the screen
function AddBlock()
{
// Create the name item
var newItemName = "Block1";
// Create the first block
var rect = new Kinetic.Rect({
x: 100,
y: 100,
width: 10,
height: 10,
fill: 'black',
strokeWidth: 4,
id: newItemName,
name: newItemName
});
// Add the block and draw it as well.
layer.add(rect);
layer.draw();
}
// Remove a block
function RemoveBlock()
{
// Get the name
var itemName = "Block1";
// Get the shape
var shape = stage.get(itemName)[0];
// Now remove it
layer.remove(shape);
}
</script>
</head>
<body>
<div id="buttons">
<input type="button" value="Add" id="Add" onclick="AddBlock();">
<input type="button" value="Remove" id="Remove" onclick="RemoveBlock();">
</div>
<div id="container"> </div>
</body>
</html>
TIA 寻求帮助!
保罗
编辑于:
仔细研究了一下,我的脚本中似乎有一些错误(如下所示) - 话虽如此,我只能通过更改脚本和我使用的 Kinetic 版本来解决这个问题:
所以 - 使用动力学版本 4.0.1...
我已将代码更改为:
// Remove a block
function RemoveBlock()
{
// Get the name
var itemName = ".Block1";
// Get the shape
var shape = layer.get(itemName)[0];
// Now remove it
layer.remove(shape);
layer.draw();
}
但问题仍然存在 - 新版本的 Kinetic 会破坏事物吗?还是我做错了什么?