python - 使用 React-konva 时 React 中出现错误“e.GetBoundingClientRect() 不是函数”
问题描述
我是 Reactjs 的新手,我正在尝试创建一个函数,在鼠标单击的位置创建一个圆圈。问题是每次我点击时,都会显示错误“e.getClickCoords 不是函数”。
此外,尽管我传递了参数,但函数 addCircle 中存在错误“形状未定义”。我不知道如何解决它:(
这是代码:
import React, { useState } from 'react';
import styled from "styled-components";
import './mysass.scss';
import { Stage, Layer, Circle, Line, Text } from "react-konva";
import { shapes } from 'konva/lib/Shape';
import ReactCursorPosition from 'react-cursor-position';
function initiate_shape() {
const initial_shape = [];
alert("Hello there");
for (var i = 0; i < 2; i++) {
initial_shape.push({
id: i,
x: Math.random() * window.innerWidth,
y: Math.random() * window.innerHeight
});
}
return initial_shape;
}
const getClickCoords = (event) => {
var e = event.target;
var dim = e.getBoundingClientRect();
var cx = event.clientX - dim.left;
var cy = event.clientY - dim.top;
alert(cx + cy);
return [cx, cy];
/*
var cX = event.clientX;
var sX = event.screenX;
var cY = event.clientY;
var sY = event.screenY;
var coords1 = "client - X: " + cX + ", Y coords: " + cY;
var coords2 = "screen - X: " + sX + ", Y coords: " + sY;
alert(coords1);
alert(coords2);
*/
//return [cX, cY];
}
function addCircle(event,shapes, shape_id) {
let [sX, sY] = getClickCoords(event);
alert("x: " + sX + " y: " + sY);
shapes.push({id: shape_id, x: sX, y: sY});
shape_id += 1
}
const FunctionApp = (event) => {
const shape_id = 2;
const initial_shape = initiate_shape();
const [shapes, setShapes] = React.useState(initial_shape);
alert(shapes);
const [connectors, setConnectors] = React.useState([]);
const [fromShapeId, setFromShapeId] = React.useState(null);
//let cx = coordinates[0];
//let cy = coordinates[1];
return (
<Stage width={window.innerWidth} height={window.innerHeight} onclick={addCircle}>
<Layer>
{connectors.map(con => {
const from = shapes.find(s => s.id === con.from);
const to = shapes.find(s => s.id === con.to);
return (
<Line
key={con.id}
points={[from.x, from.y, to.x, to.y]}
stroke="black"
/>
);
})}
{shapes.map(shape => (
<Circle
x={shape.x}
y={shape.y}
key={shape.id}
fill={fromShapeId === shape.id ? "red" : "green"}
radius={20}
shadowBlur={10}
onClick={() => {
if (fromShapeId) {
const newConnector = {
from: fromShapeId,
to: shape.id,
id: connectors.length
};
setConnectors(connectors.concat([newConnector]));
setFromShapeId(null);
//connectors.concat([newConnector]);
//fromShapeId = null;
} else {
setFromShapeId(shape.id);
//fromShapeId = shape.id;
}
}}
/>
))}
<Text text="Click on on circle then on another to connect them" />
</Layer>
</Stage>
)
}
export default FunctionApp;
提前谢谢你
解决方案
可能你应该这样onclick
做onClick
。你也应该通过shapes, shape_id
。
<Stage width={window.innerWidth} height={window.innerHeight} onClick={(e)=>addCircle(e, 'shapes details here', 'shape id here')}>
推荐阅读
- javascript - 如何通过正则表达式匹配或捕获给定字符串中的特定子字符串模式?
- python - 将实例方法结果传递给类属性
- angular - 为什么我的 Angular 路由总是重新路由到默认路由?
- ios - 从弹出框/弹出框中定义的数据填充自定义 tableView 单元格中的字段
- javascript - ExpressJS 中间件出现故障
- javascript - 什么是 unsafewindow 以及如何使用它
- php - 如何在没有 google 或 wordpress 的情况下替代 google recapture?
- python - 如何将验证功能链接到收据
- delphi - 从 INI 文件中读取所有部分并添加到列表 delphi
- docker - 无法从 Docker 桌面删除 Docker 容器