【问题标题】:How can I fill a konva rect with fillPatternImage in React or create an infinite grid with the same svg image in React如何在 React 中使用 fillPatternImage 填充 konva rect 或在 React 中创建具有相同 svg 图像的无限网格
【发布时间】:2021-01-17 06:25:25
【问题描述】:

我已经用 konva 和 React 创建了砖石网格,这里是我已经走了多远的例子: https://codesandbox.io/s/masonry-grid-image-konva-react-cp73d

关于 React 中 Konva Image 的文档https://konvajs.org/docs/react/Images.html

我需要用同一张卡片Like this image 创建一个无限网格,但我无法用背景图像或填充图案填充矩形。我不确定如何创建 img 对象来填充矩形或如何在无限网格中重复相同的图像with this image... svg 它在代码和框中,这是我当前的代码:

对此的任何帮助将不胜感激

import React, { Component } from "react";
import { render } from "react-dom";
import { Stage, Layer, Image, Rect } from "react-konva";
import useImage from "use-image";

const CardImage = () => {
  const [image] = useImage("https://i.stack.imgur.com/7nF5K.png");
  return <Image image={image} />;
};

const WIDTH = 318;
const HEIGHT = 452;

const App = () => {
  const [stagePos, setStagePos] = React.useState({ x: 200, y: 200 });
  const startX = Math.floor((-stagePos.x - window.innerWidth) / WIDTH) * WIDTH;
  const endX = Math.floor((-stagePos.x + window.innerWidth * 2) / WIDTH) * WIDTH;
  const startY = Math.floor((-stagePos.y - window.innerHeight) / HEIGHT) * HEIGHT;
  const endY = Math.floor((-stagePos.y + window.innerHeight * 2) / HEIGHT) * HEIGHT;

  const gridComponents = [];

  for (var x = startX; x < endX; x += WIDTH) {
    for (var y = startY; y < endY; y += HEIGHT) {

      gridComponents.push(
        <>
          <Rect
            x={3.8 * x}
            y={1.2 * y}
            width={WIDTH}
            height={HEIGHT}
            stroke
            shadowBlur={20}
            cornerRadius={10}
          />
          <Rect
            x={3.8 * x + 405}
            y={1.2 * y + 200}
            width={WIDTH}
            height={HEIGHT}
            stroke
            shadowBlur={20}
            cornerRadius={10}
          />
          <Rect
            x={3.8 * x + 810}
            y={1.2 * y + 400}
            width={WIDTH}
            height={HEIGHT}
            stroke
            shadowBlur={20}
            cornerRadius={10}
          />
          <CardImage />
        </>
      );
    }
  }

  return (
    <Stage
      x={stagePos.x}
      y={stagePos.y}
      width={window.innerWidth}
      height={window.innerHeight}
      draggable
      onDragEnd={(e) => {
        setStagePos(e.currentTarget.position());
      }}
    >
      <Layer>{gridComponents}</Layer>
    </Stage>
  );
};

render(<App />, document.getElementById("root"));

【问题讨论】:

    标签: reactjs image konvajs react-konva konva


    【解决方案1】:

    如果你想用图像填充矩形,你可以使用fillPatternImage 属性。

    <Rect
      width={WIDTH}
      height={HEIGHT}
      shadowBlur={20}
      cornerRadius={10}
      fillPatternImage={image}
    />
    

    要下载图像,您可以使用与Konva.Imageuse-image 挂钩https://konvajs.org/docs/react/Images.html 类似的方式

    const [image] = useImage("https://i.stack.imgur.com/7nF5K.png");
    

    您更新了演示:https://codesandbox.io/s/masonry-grid-image-konva-react-forked-s2wku?file=/src/index.js

    【讨论】:

    • 嗯,这比我想象的要容易。非常感谢!问题是与代码沙盒相比,它的工作速度非常慢。有没有办法用 svg 填充它?
    • 您也可以使用 svg 作为图像源。演示很慢,因为它有太多的形状,你只需要有形状,在视口内(或非常靠近它)。
    • 是的,谢谢!我已经使用 svg 文件作为源。正如你所注意到的,我在康瓦方面不是很有经验。您是否知道如何单独管理每张卡片/矩形以更改点击事件?有没有关于如何在视口附近显示卡片的文档?感谢您的帮助,我真的很感激!
    猜你喜欢
    • 2020-02-15
    • 2020-06-27
    • 2021-04-04
    • 2019-11-08
    • 2020-12-17
    • 2022-07-21
    • 1970-01-01
    • 2018-11-14
    • 1970-01-01
    相关资源
    最近更新 更多