简体   繁体   English

图像拖放到矩形中

[英]image drag and drop into the rectangle

I wanted to show all the 5 images in the area around the rectangle and make them draggable using React-Draggable but the images aren't being shown even if I map through the state and try to pass the url down the Images component.我想显示矩形周围区域中的所有 5 个图像,并使用 React-Draggable 使它们可拖动,但即使我通过 map 通过 state 并尝试将 Z572D4E421E5E6B71111 传递到图像组件,图像也不会显示。 where am I going wrong?我哪里错了? how do I show the images around the canvas?如何显示 canvas 周围的图像?

codesandbox 密码箱

import React, { Component } from "react";
import Draggable from "react-draggable";
import axios from "axios";
import "./App.css";
import Images from "./Images";
export default class App extends Component {
  constructor(props) {
    super(props);
    this.toggleAspect = this.toggleAspect.bind(this);
    this.state = {
      activeDrags: 0,
      deltaPosition: {
        x: 0,
        y: 0
      },
      controlledPosition: {
        x: -400,
        y: 200
      },
      urlImages: null,
      toggleDefault: true
    };
  }

  async componentDidMount() {
    const res = await axios.get("https://picsum.photos/v2/list?limit=5");
    this.setState({ urlImages: res.data });
    this.state.urlImages.map(data =>
      console.log("urlImage\t" + data.download_url)
    );
  }

  toggleAspect() {
    const currentState = this.state.toggleDefault;
    this.setState({ toggleDefault: !currentState });
  }
  handleDrag = (e, ui) => {
    const { x, y } = this.state.deltaPosition;
    this.setState({
      deltaPosition: {
        x: x + ui.deltaX,
        y: y + ui.deltaY
      }
    });
  };

  onStart = () => {
    this.setState({ activeDrags: ++this.state.activeDrags });
  };

  onStop = () => {
    this.setState({ activeDrags: --this.state.activeDrags });
  };

  // For controlled component
  adjustXPos = e => {
    e.preventDefault();
    e.stopPropagation();
    const { x, y } = this.state.controlledPosition;
    this.setState({ controlledPosition: { x: x - 10, y } });
  };

  adjustYPos = e => {
    e.preventDefault();
    e.stopPropagation();
    const { controlledPosition } = this.state;
    const { x, y } = controlledPosition;
    this.setState({ controlledPosition: { x, y: y - 10 } });
  };

  onControlledDrag = (e, position) => {
    const { x, y } = position;
    this.setState({ controlledPosition: { x, y } });
  };

  onControlledDragStop = (e, position) => {
    this.onControlledDrag(e, position);
    this.onStop();
  };

  render() {
    const dragHandlers = { onStart: this.onStart, onStop: this.onStop };
    const { deltaPosition, controlledPosition } = this.state;
    return (
      <div>
        <h1>React Draggable</h1>
        <p>Active DragHandlers: {this.state.activeDrags}</p>
        <p>
          <a href="https://github.com/STRML/react-draggable/blob/master/example/example.js">
            Demo Source
          </a>
        </p>
        <button onClick={this.toggleAspect}>Toggle Aspect Ratio</button>
        <canvas
          className={` ${
            this.state.toggleDefault ? "canvasFrame" : "canvasFrame-2"
          }`}
        />
        <Draggable>
          {/* urlImages.map((data)=>  problem
            <Images
            url={data.download_url}
            />
          ) */}
          <div
            className="box"
            style={{ position: "absolute", bottom: "100px", right: "100px" }}
          >
            I already have an absolute position.
          </div>
        </Draggable>
      </div>
    );
  }
}

There were a few changes required tldr; tldr 需要进行一些更改; here is the working link,这是工作链接,

https://codesandbox.io/s/unruffled-bogdan-x98ff https://codesandbox.io/s/unruffled-bogdan-x98ff

Images was changed a bit, one thing react-draggable mentions in it's docs is that the components must accept a few passed down properties.图像发生了一些变化, react-draggable在其文档中提到的一件事是组件必须接受一些传递下来的属性。 link to docs链接到文档

Example of the Images component now to illustrate.现在以Images组件的例子来说明。

import React from "react";

const Images = ({
  download_url,
  className,
  style,
  onMouseDown,
  onMouseUp,
  onTouchStart,
  onTouchEnd
}) => {
  return (
    <img
      onMouseDown={onMouseDown}
      onMouseUp={onMouseUp}
      onTouchStart={onTouchStart}
      onTouchEnd={onTouchEnd}
      className={className}
      style={style}
      src={download_url}
      alt="photos"
      width="50px"
      height="50px"
    />
  );
};
export default Images;

I made it overly verbose instead of spreading so you could see the properties required.我让它过于冗长而不是传播,所以你可以看到所需的属性。

In App.js I made these changes, first in the setting of the state here在 App.js 中我做了这些更改,首先是在 state 的设置中

  async componentDidMount() {
    const res = await axios.get("https://picsum.photos/v2/list?limit=5");
    const urlImages = [...res.data];
    this.setState({ urlImages });
  }

And the actual rendering of the images以及图像的实际渲染

    {urlImages.map(data => (
      <Draggable>
        <Images key={data.id} download_url={data.download_url} />
      </Draggable>
    ))}

That gets the images on the page and they are draggable, I've never used react-draggable personally so I'm sure there are some improvements that could be made to make them smoother, etc. However this hopefully points you in the right direction.这样就可以在页面上获取图像并且它们是可拖动的,我个人从未使用过react-draggable所以我确信可以进行一些改进以使它们更平滑等。但是这希望为您指明正确的方向.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM