Skip to content

Commit 8a8f3f3

Browse files
committed
Got functionality of snapping working with pics
1 parent c79311e commit 8a8f3f3

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/components/KonvaStage.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React, { Component } from "react";
2-
import { Stage, Layer, Line } from "react-konva";
2+
import { Stage, Layer, Line, Image } from "react-konva";
33
import Rectangle from "./Rectangle.tsx";
44
import cloneDeep from "../utils/cloneDeep.ts";
55
import { ComponentInt, ComponentsInt, ChildInt } from "../utils/Interfaces.ts";
6+
import isEmpty from '../utils/isEmpty';
67

78
interface PropsInt {
9+
image: HTMLImageElement;
810
components: ComponentsInt;
911
focusComponent: ComponentInt;
1012
selectableChildren: Array<number>;
@@ -107,6 +109,8 @@ class KonvaStage extends Component<PropsInt, StateInt> {
107109
if (e.target === e.target.getStage()) {
108110
return;
109111
}
112+
console.log(this.props.image.width);
113+
110114
// // clicked on transformer - do nothing
111115
const clickedOnTransformer =
112116
e.target.getParent().className === "Transformer";
@@ -167,6 +171,7 @@ class KonvaStage extends Component<PropsInt, StateInt> {
167171

168172
render() {
169173
const {
174+
image,
170175
components,
171176
handleTransform,
172177
focusComponent,
@@ -202,7 +207,8 @@ class KonvaStage extends Component<PropsInt, StateInt> {
202207
}}
203208
>
204209
{this.state.grid}
205-
{this.getDirectChildrenCopy(focusComponent)
210+
<Image image={image} draggable width={this.state.stageWidth*0.8} height={this.state.stageHeight*0.9}/>
211+
{!isEmpty(focusComponent) && this.getDirectChildrenCopy(focusComponent)
206212
.map((child: ChildInt, i: number) => (
207213
<Rectangle
208214
childType={child.childType}

src/components/Rectangle.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class Rectangle extends Component<PropsInt, StateInt> {
6262
x: target.x() + focChild.position.x,
6363
y: target.y() + focChild.position.y
6464
};
65-
65+
console.log('width:', transformation.width, 'height:', transformation.height, 'x', transformation.x, 'y', transformation.y);
6666
this.props.handleTransform(componentId, childId, transformation);
6767
}
6868

@@ -146,9 +146,10 @@ class Rectangle extends Component<PropsInt, StateInt> {
146146
draggable={false}
147147
fill={childId === -1 ? 'white' : null}
148148
shadowBlur={childId === -1 ? 6 : null}
149-
fillPatternImage={this.state.image ? this.state.image : this.setImage(imageSource)}
149+
fillPatternImage={this.state.image ? this.state.image : null}
150150
fillPatternScaleX={this.state.image ? width / this.state.image.width : 1}
151151
fillPatternScaleY={this.state.image ? height / this.state.image.height : 1}
152+
_useStrictMode
152153
/>
153154
<Label>
154155
<Text

src/containers/MainContainer.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import {
1111
} from "../actions/components.ts";
1212
import KonvaStage from "../components/KonvaStage.tsx";
1313
import { ComponentInt, ComponentsInt } from "../utils/Interfaces.ts";
14+
import * as actions from '../actions/components';
1415

1516
interface PropsInt {
17+
image: HTMLImageElement | null;
1618
components: ComponentsInt;
1719
focusComponent: ComponentInt;
1820
selectableChildren: Array<number>;
@@ -21,6 +23,7 @@ interface PropsInt {
2123
addChild: any;
2224
changeFocusComponent: any;
2325
changeFocusChild: any;
26+
changeImagePath: any;
2427
deleteComponent: any;
2528
createApp: any;
2629
deleteAllData: any;
@@ -62,6 +65,8 @@ const mapDispatchToProps = (dispatch: any) => ({
6265
})
6366
),
6467
// openPanel: component => dispatch(openExpansionPanel(component)),
68+
changeImagePath: (imageSource: string) => dispatch(actions.changeImagePath(imageSource)),
69+
6570
changeFocusChild: ({ childId }: { childId: number }) =>
6671
dispatch(changeFocusChild({ childId })),
6772
changeComponentFocusChild: ({
@@ -101,8 +106,10 @@ class MainContainer extends Component<PropsInt, StateInt> {
101106
changeFocusChild,
102107
changeComponentFocusChild,
103108
deleteChild,
104-
classes
109+
classes,
110+
image
105111
} = this.props;
112+
106113
const { main }: { main: HTMLDivElement } = this;
107114

108115
return (
@@ -111,6 +118,7 @@ class MainContainer extends Component<PropsInt, StateInt> {
111118
{modal}
112119
<div className="main" ref={main}>
113120
<KonvaStage
121+
image={image}
114122
scaleX={1}
115123
scaleY={1}
116124
draggable={draggable}

0 commit comments

Comments
 (0)