Is There A Way To Resize A Circle By Dragging The Circumference Outward / Inward Using Konva Js?
Using Konva js, Is there a way to drag a circle's circumference without showing the resizers elements, in order to resize the circle ( make the radius grow)? Using a Transformer -
Solution 1:
You may need to use two circles for that. One circle is your main shape, another circle for detecting events on stroke (the second circle can be transparent if you don't want to see it on the screen).
const stage = newKonva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight
});
const layer = newKonva.Layer();
stage.add(layer);
const circle = newKonva.Circle({
x: stage.width() / 2,
y: stage.height() / 2,
radius: 50,
fill: 'green'
});
layer.add(circle);
const border = newKonva.Circle({
x: stage.width() / 2,
y: stage.height() / 2,
radius: 50,
stroke: 'black',
strokeWidth: 6,
fillEnabled: false
});
layer.add(border);
functiondistance(p1, p2) {
returnMath.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2));
}
border.on('mouseenter', () => {
border.stroke('red');
layer.batchDraw();
})
border.on('mouseleave', () => {
border.stroke('black');
layer.batchDraw();
})
border.on('mousedown', () => {
// attach move event
stage.on('mousemove.resizer', () => {
const center = border.position();
const pointer = stage.getPointerPosition();
const radius = distance(center, pointer);
border.radius(radius);
circle.radius(radius)
layer.batchDraw();
});
// remove all events at end
stage.on('mouseup.resizer', () => {
stage.off('.resizer')
});
})
layer.draw();
<scriptsrc="https://unpkg.com/konva@^2/konva.min.js"></script><divid="container"></div>
Post a Comment for "Is There A Way To Resize A Circle By Dragging The Circumference Outward / Inward Using Konva Js?"