Reputation: 2752
Here is my current state: 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>
Just trying to make this line draggable (Easy, set it to drag).
And I want the line to have two anchor points, both can be draged in any direction.
As you can see I have only got to making one "anchor" point.
I cannot figure out the logic to make this possible, without the anchors moving about..
Thanks for your help.
Upvotes: 3
Views: 3058
Reputation: 1655
Put this code at the end and remove the code for 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();
});
}));
Also change:
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();
});
}));
Upvotes: 0
Reputation: 11755
Take a look at http://jsfiddle.net/bxGMw/
The same logic can be copied right over from this page to what you want. You could just use a buildAnchors function to create the anchors for a shape, and an update function to redraw();
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;
}
Upvotes: 2