From 34df99df6f5dae1b6363bb1ea57962673e3e6f2f Mon Sep 17 00:00:00 2001 From: yehuozhili Date: Thu, 24 Sep 2020 13:19:30 +0800 Subject: [PATCH] add ball --- package.json | 3 ++- src/pages/editor/Container.tsx | 17 +++++++++++++++-- src/pages/editor/SourceBox.tsx | 20 ++++++++++++++++---- src/pages/editor/index.less | 9 +++++++-- yarn.lock | 7 +++++++ 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 12403d8..24bc638 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "react-dnd-html5-backend": "^11.1.3", "react-dom": "^16.12.0", "react-draggable": "^4.4.3", + "react-draggable-ball": "^0.1.0", "react-grid-layout": "^1.0.0", "react-hotkeys-hook": "^2.3.1", "react-text-loop": "^2.3.0", @@ -81,12 +82,12 @@ }, "license": "MIT", "devDependencies": { - "@types/qrcode.react": "^1.0.1", "@types/classnames": "^2.2.10", "@types/codemirror": "^0.0.98", "@types/events": "^3.0.0", "@types/file-saver": "^2.0.1", "@types/node": "^14.6.2", + "@types/qrcode.react": "^1.0.1", "@types/react-color": "^3.0.4", "@types/react-grid-layout": "^1.1.0", "@types/redux-logger": "^3.0.8", diff --git a/src/pages/editor/Container.tsx b/src/pages/editor/Container.tsx index 303821b..f08b90f 100644 --- a/src/pages/editor/Container.tsx +++ b/src/pages/editor/Container.tsx @@ -21,6 +21,7 @@ import schema from 'components/BasicShop/schema'; import { ActionCreators } from 'redux-undo'; import { StateWithHistory } from 'redux-undo'; import styles from './index.less'; +import { useGetBall } from 'react-draggable-ball'; const { TabPane } = Tabs; @@ -28,7 +29,6 @@ const Container = (props: { history?: any; location?: any; pstate?: any; dispatc const [scaleNum, setScale] = useState(1); const { pstate, dispatch } = props; - console.log(props); const pointData = pstate ? pstate.pointData : {}; const curPoint = pstate ? pstate.curPoint : {}; // 指定画布的id @@ -107,6 +107,12 @@ const Container = (props: { history?: any; location?: any; pstate?: any; dispatc return arr; }, []); + const [dragstate, setDragState] = useState({ x: 0, y: 0 }); + const [render] = useGetBall(setDragState, { + innerStyle: { top: '10px', left: '10px', cursor: 'pointer' }, + ratioSpeed: { x: 1.2, y: 1.2 }, + }); + return (
- + +
{render}
+ diff --git a/src/pages/editor/SourceBox.tsx b/src/pages/editor/SourceBox.tsx index c08c68c..2506ff8 100644 --- a/src/pages/editor/SourceBox.tsx +++ b/src/pages/editor/SourceBox.tsx @@ -1,6 +1,6 @@ import React, { memo, useEffect, useState } from 'react'; import { useDrop } from 'react-dnd'; -import Draggable from 'react-draggable'; +import Draggable, { DraggableData, DraggableEvent } from 'react-draggable'; import GridLayout, { ItemCallback } from 'react-grid-layout'; import { Tooltip } from 'antd'; import { connect } from 'dva'; @@ -9,17 +9,23 @@ import styles from './index.less'; import { uuid } from '@/utils/tool'; import { Dispatch } from 'umi'; import { StateWithHistory } from 'redux-undo'; - interface SourceBoxProps { pstate: { pointData: { id: string; item: any; point: any }[] }; scaleNum: number; canvasId: string; allType: string[]; dispatch: Dispatch; + dragState: { x: number; y: number }; + setDragState: React.Dispatch< + React.SetStateAction<{ + x: number; + y: number; + }> + >; } const SourceBox = memo((props: SourceBoxProps) => { - const { pstate, scaleNum, canvasId, allType, dispatch } = props; + const { pstate, scaleNum, canvasId, allType, dispatch, dragState, setDragState } = props; const pointData = pstate ? pstate.pointData : []; const [canvasRect, setCanvasRect] = useState([]); @@ -92,8 +98,14 @@ const SourceBox = memo((props: SourceBoxProps) => { }, []); const opacity = isOver ? 0.7 : 1; // const backgroundColor = isOver ? 1 : 0.7; + return ( - + { + setDragState({ x: data.x, y: data.y }); + }} + >