Skip to content

Commit 845aa4d

Browse files
Merge pull request #43 from ChristianEdwardPadilla/development
Canvas MVP
2 parents 50a4d24 + 16b9688 commit 845aa4d

File tree

7 files changed

+252
-267
lines changed

7 files changed

+252
-267
lines changed

src/components/GrandchildRectangle.jsx

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ class GrandchildRectangle extends Component {
77
return color;
88
}
99

10+
getPseudoChild() {
11+
return this.props.components.find(comp => comp.id === this.props.childComponentId);
12+
}
13+
1014
render() {
1115
const {
1216
x,
@@ -64,12 +68,20 @@ class GrandchildRectangle extends Component {
6468
childComponentId={grandchild.childComponentId}
6569
focusChild={focusChild}
6670
childId={childId}
67-
x={grandchild.position.x * (width / (window.innerWidth / 2))}
68-
y={grandchild.position.y * (height / window.innerHeight)}
69-
scaleX={1}
70-
scaleY={1}
71-
width={grandchild.position.width * (width / (window.innerWidth / 2))}
72-
height={grandchild.position.height * (height / window.innerHeight)}
71+
// x={grandchild.position.x * (width / window.innerWidth)}
72+
// y={grandchild.position.y * (height / window.innerHeight)}
73+
// width={grandchild.position.width * (width / window.innerWidth)}
74+
// height={grandchild.position.height * (height / window.innerHeight)}
75+
width={grandchild.position.width * (width / this.getPseudoChild().position.width)}
76+
height={grandchild.position.height * (height / this.getPseudoChild().position.height)}
77+
x={
78+
(grandchild.position.x - this.getPseudoChild().position.x)
79+
* (width / this.getPseudoChild().position.width)
80+
}
81+
y={
82+
(grandchild.position.y - this.getPseudoChild().position.y)
83+
* (height / this.getPseudoChild().position.height)
84+
}
7385
/>
7486
))}
7587
</Group>

src/components/KonvaStage.jsx

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
import React, { Component, createRef, Fragment } from "react";
1+
import React, { Component, createRef, Fragment } from 'react';
22
// import PropTypes from 'prop-types';
33
import {
4-
Stage,
5-
Layer,
6-
Line,
7-
Group,
8-
Label,
9-
Text,
10-
Rect,
11-
Transformer
12-
} from "react-konva";
13-
import Rectangle from "./Rectangle.jsx";
4+
Stage, Layer, Line, Group, Label, Text, Rect, Transformer,
5+
} from 'react-konva';
6+
import Rectangle from './Rectangle.jsx';
147

158
class KonvaStage extends Component {
169
constructor(props) {
@@ -20,7 +13,7 @@ class KonvaStage extends Component {
2013
stageHeight: 1000,
2114
blockSnapSize: 5,
2215
grid: [],
23-
gridStroke: 1
16+
gridStroke: 1,
2417
};
2518
}
2619

@@ -29,120 +22,112 @@ class KonvaStage extends Component {
2922
// here we should add listener for "container" resize
3023
// take a look here https://developers.google.com/web/updates/2016/10/resizeobserver
3124
// for simplicity I will just listen window resize
32-
window.addEventListener("resize", this.checkSize);
25+
window.addEventListener('resize', this.checkSize);
3326
this.createGrid();
3427
}
3528

3629
componentWillUnmount() {
37-
window.removeEventListener("resize", this.checkSize);
30+
window.removeEventListener('resize', this.checkSize);
3831
}
3932

4033
checkSize = () => {
4134
const width = this.container.offsetWidth;
4235
const height = this.container.offsetHeight;
4336
this.setState({
4437
stageWidth: width,
45-
stageHeight: height
38+
stageHeight: height,
4639
});
4740
};
4841

49-
handleStageMouseDown = e => {
42+
handleStageMouseDown = (e) => {
5043
// // clicked on stage - clear selection
5144
if (e.target === e.target.getStage()) {
5245
// add functionality for allowing no focusChild
53-
console.log("user clicked on canvas:");
46+
console.log('user clicked on canvas:');
5447
return;
5548
}
5649
// // clicked on transformer - do nothing
57-
const clickedOnTransformer =
58-
e.target.getParent().className === "Transformer";
50+
const clickedOnTransformer = e.target.getParent().className === 'Transformer';
5951
if (clickedOnTransformer) {
60-
console.log("user clicked on transformer");
61-
console.log("HELLOOOO", e.target.getParent().className);
52+
console.log('user clicked on transformer');
53+
console.log('HELLOOOO', e.target.getParent().className);
6254
return;
6355
}
6456

6557
// find clicked rect by its name
6658
const rectChildId = e.target.attrs.childId;
67-
console.log("user clicked on child rectangle with Id: ", rectChildId);
59+
console.log('user clicked on child rectangle with childId: ', rectChildId);
6860
this.props.changeFocusChild({ childId: rectChildId });
6961
this.props.changeComponentFocusChild({
7062
componentId: this.props.focusComponent.id,
71-
childId: rectChildId
63+
childId: rectChildId,
7264
});
7365
};
7466

7567
createGrid = () => {
76-
let output = [];
68+
const output = [];
7769
for (let i = 0; i < this.state.stageWidth / this.state.blockSnapSize; i++) {
7870
output.push(
7971
<Line
8072
points={[
8173
Math.round(i * this.state.blockSnapSize) + 0.5,
8274
0,
8375
Math.round(i * this.state.blockSnapSize) + 0.5,
84-
this.state.stageHeight
76+
this.state.stageHeight,
8577
]}
86-
stroke={"#ddd"}
78+
stroke={'#ddd'}
8779
strokeWidth={this.state.gridStroke}
88-
key={i + "vertical"}
89-
/>
80+
key={`${i}vertical`}
81+
/>,
9082
);
9183
}
92-
for (
93-
let j = 0;
94-
j < this.state.stageHeight / this.state.blockSnapSize;
95-
j++
96-
) {
84+
for (let j = 0; j < this.state.stageHeight / this.state.blockSnapSize; j++) {
9785
output.push(
9886
<Line
9987
points={[
10088
0,
10189
Math.round(j * this.state.blockSnapSize),
10290
this.state.stageWidth,
103-
Math.round(j * this.state.blockSnapSize)
91+
Math.round(j * this.state.blockSnapSize),
10492
]}
105-
stroke={"#ddd"}
93+
stroke={'#ddd'}
10694
strokeWidth={this.state.gridStroke}
107-
key={j + "horizontal"}
108-
/>
95+
key={`${j}horizontal`}
96+
/>,
10997
);
11098
}
111-
console.log("calling function to render grid");
99+
console.log('calling function to render grid');
112100
this.setState({
113-
grid: output
101+
grid: output,
114102
});
115103
};
116104

117105
render() {
118106
const {
119-
components,
120-
handleTransform,
121-
focusComponent,
122-
focusChild
107+
components, handleTransform, focusComponent, focusChild,
123108
} = this.props;
124109

125110
return (
126111
<div
127112
style={{
128-
width: "100%",
129-
height: "100%"
113+
width: '100%',
114+
height: '100%',
130115
}}
131-
ref={node => {
116+
ref={(node) => {
132117
this.container = node;
133118
}}
134119
>
135120
<Stage
136-
className={"canvasStage"}
137-
ref={node => {
121+
className={'canvasStage'}
122+
ref={(node) => {
138123
this.stage = node;
139124
}}
140125
onMouseDown={this.handleStageMouseDown}
141126
width={this.state.stageWidth}
142127
height={this.state.stageHeight}
143128
>
144129
<Layer
145-
ref={node => {
130+
ref={(node) => {
146131
this.layer = node;
147132
}}
148133
>
@@ -171,10 +156,8 @@ class KonvaStage extends Component {
171156
/>
172157
))
173158
.sort(
174-
(rectA, rectB) =>
175-
rectA.props.width * rectA.props.height <
176-
rectB.props.width * rectB.props.height
177-
)
159+
(rectA, rectB) => rectA.props.width * rectA.props.height < rectB.props.width * rectB.props.height,
160+
) // shouldnt this be subtraction instead of < ? see MDN
178161
// reasoning for the sort:
179162
// Konva determines zIndex (which rect is clicked on if rects overlap) based on rendering order
180163
// as long as the smallest components are rendered last they will always be accessible over the big boys

0 commit comments

Comments
 (0)