New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clipping with transformer or similar? #649
Comments
Move the image into a group and then use clip for the group instead of a layer. That way the Transformer will be visible. |
Had a similar Issue.
This approach works like i want to, but Konva throws warnings in the Dev Console: |
You can disable warning with But I think there is a better solution for such a case is using "fake" transparent shape where you want and making your own node structure. // noprotect
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth,
height: window.innerHeight
});
const layer = new Konva.Layer();
stage.add(layer);
Konva.Image.fromURL('https://i.imgur.com/ktWThtZ.png', img => {
var group = new Konva.Group({
clipFunc: (ctx) => {
ctx.save();
ctx.translate(fakeShape.x(), fakeShape.y())
ctx.rotate(Konva.getAngle(fakeShape.rotation()))
ctx.rect(0, 0, fakeShape.width() * fakeShape.scaleX(), fakeShape.height() * fakeShape.scaleY());
ctx.restore()
}
})
layer.add(group);
img.setAttrs({
width: stage.width(),
height: stage.height()
});
group.add(img);
var fakeShape = new Konva.Rect({
width: 100,
height: 100,
x: 100,
y: 100,
fill: 'rgba(0,0,0,0)',
draggable: true
})
layer.add(fakeShape);
var tr = new Konva.Transformer({
node: fakeShape
});
layer.add(tr);
layer.draw();
}); |
Hi, I'm facing a similar issue: I have two types of layers: text and image which I can drag and transform with a Transformer. The specificity is I need to apply different clipping settings to each layer depending on its type when I enable a "readonly" mode. When this mode is enabled, the Transformer is not available. Would you you still recommend to use a fake shape for this use case ? This would lead to a lot of extra work in my case. |
@bgondy why it is extra work? Yeah, it is probably recommended for now. |
@lavrton Because of my data structure and the way I persist shape configuration in my store but this has nothing to do with the clipping issue. I will implement your fake shape trick. Thanks for helping me |
BTW with the last version you can place the transformer anywhere and it will work without warnings. |
Amazing, you made my day :D |
how can we do similar thing in react-konva. |
Very useful demo for this use case, it could be great to have it as an example/tutorial on Konva website |
I am making a simple image layer manipulator with konva. Right now you can add images creating a new konva layer for each and use a transformer to select, scale, and rotate them individually.
I want to allow the user to clip the image layers using controls similar to the transformer. You'd click on a handle and as you move it it would crop (or reveal previously clipped parts of) the image from the side or corner that you are dragging instead of scaling the image.
I've experimented with this by adding a clip rect to a layer's image through code but afterwards the transformer no longer draws or functions correctly. If the image was previously scaled it gets even weirder.
What would be the best approach for implementing image clipping in a way that would work well with transformers?
The text was updated successfully, but these errors were encountered: