简体   繁体   English

渲染 svgs 并在 React 组件中设置内部 css.this

[英]render svgs and set inner css within a React component .this

I have a react component, in which im setting a sequence of notes.我有一个反应组件,我在其中设置了一系列音符。 I would like to dynamically render an svg for each pitch;我想为每个音高动态渲染一个 svg; in this case, where there are 3 notes, so the would just be 3 svgs.在这种情况下,有 3 个音符,所以只有 3 个 svg。

So, if this in my component:所以,如果这在我的组件中:

this.sequence = {
      BEATB: {
      ticksPerQuarter: 360,
      totalTime: 2,
      timeSignatures: [{ time: 0, numerator: 4, denominator: 4 }],
      tempos: [{ time: 0, qpm: 300 }],
      notes: [
        { pitch: 60.3, startTime: 0, endTime: 0.9 },
        { pitch: 65.4, startTime: 0.9, endTime: 1.9 },
        { pitch: 67.2, startTime: 1.9, endTime: 2.7 },
      ]
    }
  }

the code should return something like this:代码应返回如下内容:

render(){
  return(
    <div>
      <img src={svg1}></img>
      <img src={svg2}></img>
      <img src={svg3}></img>
    </div
  )
}

Moreso, i would like to set inner styling with the keys.此外,我想用按键设置内部样式。 So that for:所以对于:

 { pitch: 65.4, startTime: 0.9, endTime: 1.9 },

you get:你得到:

<img src={svg2} style={{transform:'translateY({pitch})', 
 animationDelay:'0.9s', animationDuration:'1s'}}></img>

where a transform is set by the pitch, and animation delay and duration is set by start time and end time.其中变换由音高设置,animation 延迟和持续时间由开始时间和结束时间设置。

All and any help is appreciated!感谢所有和任何帮助!

There are some parts.有一些部分。 First, if your app compatibles , you can import the SVG directly.首先,如果您的应用兼容,您可以直接import SVG。 Then, you can show the notes based on sequence.BEATB.notes using map .然后,您可以使用map显示基于sequence.BEATB.notes的注释。

Like this像这样

import { ReactComponent as NoteSymbol } from "./note.svg";

// ...
{this.sequence.BEATB.notes.map((note, idx) => (
  <NoteSymbol
    key={idx}
    className="note"
    style={{
      marginTop: note.pitch,
      animationDelay: "0.9s",
      animationDuration: "1s"
    }}
  />
))}

https://codesandbox.io/s/sharp-paper-tc9rs?file=/src/App.js https://codesandbox.io/s/sharp-paper-tc9rs?file=/src/App.js

BTW, in the example, I used marginTop instead of transform because, this way, the note movement will be calculated in document flow (so the parent height will "consider" the notes margin.顺便说一句,在示例中,我使用marginTop而不是transform因为这样,便笺移动将在文档流中计算(因此父高度将“考虑”便笺边距。

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

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