Comments (2)
Please make a demo.
from react-konva.
I actually wrote a demo. But that was working correctly and helped me understand how Transformers (with multiple attached objects) work.
My orginal problem had to do with the way I handled onTransformEnd./ onDragEnd.
Maybe the documentation should describe that the events can be handled on ihe individual objects (even when using a Transformer to move or resize multiple objects). It also seems to be neccessary to reset scaleX and scaleY after during onEndtransform to 1
Here is my demo (as it might help others how it is supposed to work)
import React, {useState, useRef, useEffect} from "react";
import {Stage, Layer, Rect, Transformer, Group} from "react-konva";
import {Portal} from "react-konva-utils";
const initialConfigs = [
{x: 0, y: 0, width: 100, height: 100, fill: "red", selected: false},
{x: 200, y: 200, width: 100, height: 100, fill: "green", selected: false},
{x: 400, y: 400, width: 100, height: 100, fill: "orange", selected: false},
];
const App = ({}) =>{
const [configs, setConfigs] = useState(initialConfigs);
const transformerRef = useRef();
const handleUpdate = (index, values) => {
console.log(index, values);
setConfigs((oldConfigs) => {
var newConfigs = structuredClone(oldConfigs);
newConfigs[index] = {...newConfigs[index], ...values};
return newConfigs;
});
}
const handleOnlySelected = (newIndex) => {
setConfigs((oldConfigs) => {
var newConfigs = oldConfigs.map((config, index) => {
return {...config, selected:index==newIndex};
});
return newConfigs;
})
}
const boxes = configs.map((config, index) => {
return <Box key={index} index={index} {...config} transformerRef={transformerRef} onUpdate={handleUpdate} onOnlySelected={handleOnlySelected}/>;
});
return <div style= {{width: "95vw", height: "90vh", backgroundColor: "blue", overflow: "auto"}}>
<Stage width={1000} height={1000}>
<Layer>
{boxes}
<Group name={"top"} />
<Transformer ref={transformerRef} />
</Layer>
</Stage>
</div>;
}
const Box = ({transformerRef, index, selected, onUpdate, onOnlySelected, ...rest}) => {
const boxRef = useRef();
const handleClick = () => {
onUpdate(index,{selected: !selected});
}
const handleDragStart = (event) => {
if (!selected) {
onOnlySelected(index);
}
}
const handleDragEnd = (event) => {
const node = boxRef.current;
onUpdate(index, {x: node.x(), y: node.y()});
}
const handleTransformEnd = (event) => {
const node = boxRef.current;
const newPos = {x: node.x(), y: node.y(), width: node.width() *node.scaleX(), height: node.height()*node.scaleY() };
node.scaleX(1);
node.scaleY(1);
onUpdate(index, newPos);
}
useEffect(() => {
var curNodes = transformerRef.current?.nodes();
if (selected) {
if (!curNodes.includes(boxRef.current)) {
curNodes = [...curNodes, boxRef.current];
transformerRef.current?.nodes(curNodes);
}
} else {
if (curNodes.includes(boxRef.current)) {
var curNodes = curNodes.filter((node) => node!=boxRef.current);
transformerRef.current?.nodes(curNodes);
}
}
}, [selected, transformerRef])
console.log("Drawing", index, rest);
return <Portal selector={".top"} enabled={selected}>
<Rect ref={boxRef} {...rest} onClick={handleClick} draggable={true} onTransformEnd={handleTransformEnd} onDragStart={handleDragStart} onDragEnd={handleDragEnd}/>
</Portal>
}
export default App;
from react-konva.
Related Issues (20)
- Issue with RTL Text
- Facing the issue now with Next.js 14: HOT 5
- OnTouchStart does not contain touches HOT 1
- getRelativePointerPosition has an offset on x when going fullscreen
- Asynchronous drawing will exceed the cropping area HOT 4
- Free drawing performance HOT 6
- Coordinates not working correctly after zoom in/out or drag of stage HOT 1
- If I want to erase only the lines drawn with a pen, how can I do that? HOT 2
- Rendering <Context.Consumer.Provider> is not supported and will be removed in a future major release. HOT 1
- Arrow Drag and Transform using anchors not working HOT 4
- load data to stage HOT 1
- Positioning issue with transformer using keepRatio prop set to false HOT 1
- Breaking changes in React experimental branch
- TypeScript : `'cornerRadius'` does not exist on type `Shape<RectConfig>` HOT 4
- Trouble with Drag-and-Drop on Mobile Devices in React (with react-konva) HOT 1
- g
- Cannot drag the group if I do not drag the elements inside it HOT 1
- How to draw holes in polygons? HOT 1
- How to update the clipFunc state HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-konva.