Skip to content

Commit 27378bd

Browse files
canvas ratios first working draft
1 parent afa4ced commit 27378bd

File tree

2 files changed

+47
-55
lines changed

2 files changed

+47
-55
lines changed

src/components/KonvaStage.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class KonvaStage extends Component {
109109
))
110110
.sort(
111111
(rectA, rectB) => rectA.props.width * rectA.props.height < rectB.props.width * rectB.props.height,
112-
)
112+
) // shouldnt this be subtraction instead of < ? see MDN
113113
// reasoning for the sort:
114114
// Konva determines zIndex (which rect is clicked on if rects overlap) based on rendering order
115115
// as long as the smallest components are rendered last they will always be accessible over the big boys

src/components/Rectangle.jsx

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,24 @@ class Rectangle extends Component {
1111
return color;
1212
}
1313

14-
getGrandchildRatio(grandchild) {
15-
// const childInstance = pseudoChildComp.childrenArray.find()
16-
const pseudoParent = this.props.components.find(
17-
comp => comp.title === this.props.childComponentName,
18-
);
19-
const childInstance = pseudoParent.childrenArray.find(
20-
child => child.childId === grandchild.childId,
21-
);
22-
// console.log(
23-
// this.props.childId,
24-
// this.props.componentId,
25-
// this.props.childComponentName,
26-
// this.props.childComponentId,
27-
// );
28-
console.log(grandchild, pseudoParent, childInstance);
29-
// console.log(childInstance.position.width / pseudoParent.position.width);
30-
// console.log(childInstance.position.height / pseudoParent.position.height);
14+
getPseudoChild(grandchild) {
15+
// example: Board with direct child Row (where Row has a child Box)
16+
// grandchild is a reference to the Box child of Row
17+
// pseudoParent is a reference to Row itself (whose size is determined by its pseudoChild)
18+
// directParent refers to the instance of Row
19+
return this.props.components.find(comp => comp.id === this.props.childComponentId);
3120

32-
console.log(childInstance.position.x, pseudoParent.position.x);
33-
console.log(childInstance.position.y, pseudoParent.position.y);
34-
const ratioObj = {
35-
// x:
36-
// grandchild.position.x
37-
// - (pseudoParent.position.x * childInstance.position.width) / pseudoParent.position.width,
38-
// y:
39-
// grandchild.position.y
40-
// - (pseudoParent.position.y * childInstance.position.height) / pseudoParent.position.height,
41-
x: childInstance.position.x - pseudoParent.position.x,
42-
y: childInstance.position.y - pseudoParent.position.y,
43-
width: childInstance.position.width / pseudoParent.position.width,
44-
height: childInstance.position.height / pseudoParent.position.height,
45-
};
46-
// console.log('ratioObj', ratioObj);
47-
return ratioObj;
21+
// const ratioObj = {
22+
// x:
23+
// ((grandchild.position.x - pseudoParent.position.x) * directParent.position.width)
24+
// / pseudoParent.position.width,
25+
// y:
26+
// ((grandchild.position.y - pseudoParent.position.y) * directParent.position.height)
27+
// / pseudoParent.position.height,
28+
// width: grandchild.position.width / pseudoParent.position.width,
29+
// height: grandchild.position.height / pseudoParent.position.height,
30+
// };
31+
// return ratioObj;
4832
}
4933

5034
handleResize(componentId, childId, target) {
@@ -104,6 +88,7 @@ class Rectangle extends Component {
10488
onDragEnd={event => this.handleDrag(componentId, childId, event.target)}
10589
>
10690
<Rect
91+
// a Konva Rect is generated for each child of the focusComponent (including the pseudochild, representing the focusComponent itself)
10792
name={`${childId}`}
10893
className={'childRect'}
10994
x={0}
@@ -137,44 +122,51 @@ class Rectangle extends Component {
137122
y={childId === '-1' ? -15 : 5}
138123
/>
139124
</Label>
140-
{focusChild
141-
&& focusChild.childId === childId
142-
&& draggable && (
143-
<TransformerComponent
144-
focusChild={focusChild}
145-
rectClass={'childRect'}
146-
anchorSize={8}
147-
color={'grey'}
148-
/>
149-
)}
150-
{childId !== '-1'
125+
{// for all children other than the pseudoChild, find their component's children array and recursively render the children found there
126+
childId !== '-1'
151127
&& components
152128
.find(comp => comp.title === childComponentName)
153129
.childrenArray.filter(child => child.childId !== '-1')
130+
// .sort((a, b) => parseInt(a.childId) - parseInt(b.childId)) // using i within map below, sorting by childId might be necessary
154131
.map((grandchild, i) => (
155132
<GrandchildRectangle
156133
key={i}
157134
components={components}
158135
componentId={componentId}
136+
directParentName={childComponentName}
159137
childComponentName={grandchild.componentName}
160138
childComponentId={grandchild.childComponentId}
161139
focusChild={focusChild}
162140
childId={childId} // scary addition, grandchildren rects default to childId of "direct" children
163-
// x={grandchild.position.x * (width / window.innerWidth)}
164-
// y={grandchild.position.y * (height / window.innerHeight)}
141+
// x={this.getPseudoChild().position.x}
142+
// y={}
143+
width={grandchild.position.width * (width / this.getPseudoChild().position.width)}
144+
height={
145+
grandchild.position.height * (height / this.getPseudoChild().position.height)
146+
}
147+
x={
148+
(grandchild.position.x - this.getPseudoChild().position.x)
149+
* (width / this.getPseudoChild().position.width)
150+
}
151+
y={
152+
(grandchild.position.y - this.getPseudoChild().position.y)
153+
* (height / this.getPseudoChild().position.height)
154+
}
165155
// width={grandchild.position.width * (width / window.innerWidth)}
166156
// height={grandchild.position.height * (height / window.innerHeight)}
167-
x={this.getGrandchildRatio(grandchild).x}
168-
y={this.getGrandchildRatio(grandchild).y}
169-
width={width * this.getGrandchildRatio(grandchild).width}
170-
height={height * this.getGrandchildRatio(grandchild).height}
171-
// x={grandchild.position.x}
172-
// y={grandchild.position.y}
173-
// width={grandchild.position.width}
174-
// height={grandchild.position.height}
175157
// title={child.componentName + child.childId}
176158
/>
177159
))}
160+
{focusChild
161+
&& focusChild.childId === childId
162+
&& draggable && (
163+
<TransformerComponent
164+
focusChild={focusChild}
165+
rectClass={'childRect'}
166+
anchorSize={8}
167+
color={'grey'}
168+
/>
169+
)}
178170
</Group>
179171
);
180172
}

0 commit comments

Comments
 (0)