这是我目前的状态:http://jsfiddle.net/andrewgable/Xr6mc/
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
</style>
</head>
<body>
<div id="container"></div>
<script src="http://www.html5canvastutorials.com/libraries/kinetic-v4.3.0-beta2.js"></script>
<script>
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
var lineLayer = new Kinetic.Layer();
var flowerLayer = new Kinetic.Layer();
var centerLayer = new Kinetic.Layer();
var flower = new Kinetic.Group({
x: stage.getWidth() / 2,
y: stage.getHeight() / 2
});
// build stem
var stem = new Kinetic.Line({
strokeWidth: 10,
stroke: 'green',
points: [{
x: flower.getX(),
y: flower.getY()
}, {
x: stage.getWidth() / 2,
y: stage.getHeight() + 10
}]
});
// build center
var center = new Kinetic.Circle({
x: 0,
y: 0,
radius: 6,
fill: 'black',
draggable: true,
x: stage.getWidth() / 2,
y: stage.getHeight() / 2
});
center.on('mouseover', function() {
this.setFill('orange');
flowerLayer.draw();
document.body.style.cursor = 'pointer';
});
center.on('mouseout', function() {
this.setFill('black');
flowerLayer.draw();
document.body.style.cursor = 'default';
});
stage.on('mouseup', function() {
document.body.style.cursor = 'default';
});
lineLayer.add(stem);
flowerLayer.add(flower);
centerLayer.add(center);
stage.add(lineLayer);
stage.add(flowerLayer);
stage.add(centerLayer);
// keep step and flower position in sync with center
center.on('dragstart', (function() {
center.getLayer().afterDraw(function() {
stem.attrs.points[0] = center.getPosition();
flower.setPosition(center.getPosition());
lineLayer.draw();
flowerLayer.draw();
});
}));
</script>
</body>
</html>
试图让这条线可拖动(简单,设置拖动)。
我希望这条线有两个锚点,两个都可以向任何方向拖动。
正如你所看到的,我只需要制作一个“锚点”。
我无法找到使这成为可能的逻辑,而没有锚点移动......
感谢您的帮助。
答案 0 :(得分:2)
可以将相同的逻辑从此页面直接复制到您想要的内容。你可以使用buildAnchors函数来创建一个形状的锚点,并使用一个更新函数来重绘();
function buildAnchor(layer, x, y) {
var anchor = new Kinetic.Circle({
x: x,
y: y,
radius: 8,
stroke: '#666',
fill: '#ddd',
strokeWidth: 2,
draggable: true
});
// add hover styling
anchor.on('mouseover', function() {
document.body.style.cursor = 'pointer';
this.setStrokeWidth(4);
layer.draw();
});
anchor.on('mouseout', function() {
document.body.style.cursor = 'default';
this.setStrokeWidth(2);
layer.draw();
});
layer.add(anchor);
return anchor;
}
答案 1 :(得分:0)
将此代码放在最后并删除group1
stem.on('dragmove', (function () {
// stem.getLayer().afterDraw(function () {
center.setPosition([stem.getPosition().x + stem.getPoints()[0].x, stem.getPosition().y + stem.getPoints()[0].y]);
c2.setPosition([stem.getPosition().x + stem.getPoints()[1].x, stem.getPosition().y + stem.getPoints()[1].y]);
flower.setPosition(center.getPosition());
lineLayer.draw();
centerLayer.draw();
flowerLayer.draw();
// });
}));
var c2 = new Kinetic.Circle({
x: 100,
y: 100,
radius: 6,
fill: 'blue',
draggable: true,
});
centerLayer.add(c2);
c2.on('dragstart', (function () {
c2.getLayer().afterDraw(function () {
stem.attrs.points[1].x = c2.getX()-stem.getX();
stem.attrs.points[1].y = c2.getY()-stem.getY();
lineLayer.draw();
flowerLayer.draw();
});
}));
同样改变:
center.on('dragstart', (function () {
center.getLayer().afterDraw(function () {
stem.attrs.points[0].x = center.getX()-stem.getX();
stem.attrs.points[0].y = center.getY()-stem.getY();
flower.setPosition(center.getPosition());
lineLayer.draw();
flowerLayer.draw();
});
}));