首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Konva文本边界

Konva文本边界
EN

Stack Overflow用户
提问于 2018-08-08 12:40:35
回答 1查看 1.8K关注 0票数 2

我有一个简单的Konva设置,有一个层,直角和文本。

画布与窗口的大小成比例。我的目标是在拖动时将文本包含在画布中。目前,它工作在左和上界,但不是下界和右界。

我在这里做错什么了?

说明gif

https://media.giphy.com/media/fBG0OGhFPwiHUOeGSJ/giphy.gif

代码

代码语言:javascript
复制
import React from "react";
import ReactDOM from "react-dom";
import styled from "styled-components";
import Konva from "konva";
import { Stage, Layer, Text, Rect } from "react-konva";

const CANVAS_INNER_PADDING = 5;

const Container = styled.div`
  width: 50vw;
  height: 50vw;
  background-color: lightgray;
`;

class Canvas extends React.Component {
  constructor(props) {
    super(props);
    this.container = React.createRef();
    this.rect = React.createRef();
    window.addEventListener("resize", this.updateCanvasDimensions);

    this.state = {
      text: {
        value: "hello",
        x: CANVAS_INNER_PADDING,
        y: CANVAS_INNER_PADDING
      },
      canvas: {
        width: 0,
        height: 0,
        color: "red"
      }
    };
  }

  componentDidMount() {
    this.updateCanvasDimensions();
  }

  updateCanvasDimensions = () => {
    const height = this.container.current.clientHeight;
    const width = this.container.current.clientWidth;

    this.setState({
      canvas: { ...this.state.canvas, height, width }
    });
  };

  handleDragMove = e => {
    const { canvas } = this.state;
    const newX = e.target.x();
    const newY = e.target.y();
    let newerX = newX;
    let newerY = newY;

    if (newX < CANVAS_INNER_PADDING) {
      newerX = CANVAS_INNER_PADDING;
    }

    if (newX > canvas.width - CANVAS_INNER_PADDING) {
      newerX = canvas.width - CANVAS_INNER_PADDING;
    }

    if (newY < CANVAS_INNER_PADDING) {
      newerY = CANVAS_INNER_PADDING;
    }

    if (newY > canvas.height - CANVAS_INNER_PADDING) {
      newerY = canvas.height - CANVAS_INNER_PADDING;
    }

    this.setState({
      text: { ...this.state.text, x: newerX, y: newerY }
    });
  };

  handleDragEnd = e => {
    const newX = e.target.x();
    const newY = e.target.y();

    this.setState({
      text: { ...this.state.text, x: newX, y: newY }
    });
  };

  render() {
    const { text, canvas } = this.state;

    return (
      // Have to use `innerRef` with Styled Components.
      <Container innerRef={this.container}>
        <Stage height={canvas.height} width={canvas.width}>
          <Layer>
            <Rect
              ref={this.rect}
              x={0}
              y={0}
              height={canvas.height}
              width={canvas.width}
              fill={canvas.color}
            />
            <Text
              ref={this.text}
              draggable
              onDragMove={this.handleDragMove}
              onDragEnd={this.handleDragEnd}
              text={`${canvas.height}, ${canvas.width}`}
              fill="black"
              x={text.x}
              y={text.y}
            />
          </Layer>
        </Stage>
      </Container>
    );
  }
}

const mapStateToProps = state => ({
  text: state.printState.text,
  canvas: state.printState.canvas
});

const rootElement = document.getElementById("root");
ReactDOM.render(<Canvas />, rootElement);

沙盒

https://codesandbox.io/s/6jnmvzyqmk

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-08 12:49:19

似乎已经解决了这个问题,我没有考虑到课文的高度和宽度:

代码语言:javascript
复制
const { canvas } = this.props;
const { textHeight, textWidth } = this.text.current;

const newX = e.target.x();
const newY = e.target.y();
let newerX = newX;
let newerY = newY;

if (newX < CANVAS_INNER_PADDING) {
  newerX = CANVAS_INNER_PADDING;
}

if (newX + textWidth > canvas.width - CANVAS_INNER_PADDING) {
  newerX = canvas.width - CANVAS_INNER_PADDING - textWidth;
}

if (newY < CANVAS_INNER_PADDING) {
  newerY = CANVAS_INNER_PADDING;
}

if (newY + textHeight > canvas.height - CANVAS_INNER_PADDING) {
  newerY = canvas.height - CANVAS_INNER_PADDING - textHeight;
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51747021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档