简体   繁体   English

在 react-konva 上动态渲染数据库中的图像

[英]Render Image from database dynamically on react-konva

Im trying to do a RPG table using react-konva and I coudnt render images from database dynamically user the hook useImage, Its now work inside the map.我正在尝试使用 react-konva 制作一个 RPG 表,并且我可以使用钩子 useImage 动态地从数据库中渲染图像,它现在可以在 map 内工作。

const imagens =
    tokens && tokens.tokens && tokens.tokens.map(item => item.image)

    const [token] = useImage(imagens && imagens[0])

  return (
    <Stage
      x={stagePos.x}
      y={stagePos.y}
      width={1200}
      height={600}
      draggable
      onDragEnd={e => {
        setStagePos(e.currentTarget.position())
      }}
    >
      <Layer>
        {gridComponents}

        {tokens &&
          tokens.tokens &&
          tokens.tokens.map(item => (
            <Image
              draggable
              x={item.x}
              y={item.y}
              image={token} // item.image
              width={item.width}
              height={item.height}
              offsetX={item.width / 2}
              offsetY={item.height / 2}
              scaleX={1}
              rotation={item.angle}
              onDragEnd={handleDragEnd}
              onClick={() => {
                setAngle(angle + 45)
                item.angle += 45
              }}
            />
          ))}
      </Layer>
    </Stage>

That way us working but with static image for all itens.这样我们就可以工作,但所有项目都使用 static 图像。 How could I put the useImage inside de map?我怎么能把 useImage 放在 de map 里面?

I return this error message when I tried我尝试时返回此错误消息在此处输入图像描述

I also need to move each one and rotate.我还需要移动每一个并旋转。 I did the rotate like this, looks like it worked!我做了这样的旋转,看起来它有效!

 onClick={() => {
                setAngle(angle + 45)
                item.angle += 45
              }}

This is because hooks must be deterministic in their ordering.这是因为钩子的顺序必须是确定的。 By putting a hook outside of the component, react is able to guarantee the order of the useImage hook in each render.通过在组件外部放置一个钩子,react 能够保证 useImage 钩子在每个渲染中的顺序。

If you put it inside of the components props then react cannot guarantee the order;如果你把它放在 components 的 props 里面,那么 react 不能保证顺序; If the array changes order, or items are removed from the array then the hooks will no longer be in the same order as they were in the initial render.如果数组更改顺序,或者从数组中删除项目,则挂钩将不再与初始渲染中的顺序相同。

Read more about why they have to be in order here: https://reactjs.org/docs/hooks-rules.html#explanation在这里阅读更多关于为什么它们必须有序的信息: https://reactjs.org/docs/hooks-rules.html#explanation

To solve your problem, you can instead push down the useImage call into a component:要解决您的问题,您可以改为将 useImage 调用下推到组件中:

const Token = props => {
    const [tokenImg] = useImage(props.image);
    return (
       <Image
          image={tokenImg}
          / * snip */
       />
    );
}

Then in the.map:然后在.map中:

tokens.tokens.map(item => <Token image={item.image}/>)

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

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