【发布时间】:2019-07-11 22:25:41
【问题描述】:
我有一个 ReactJS 应用程序。我正在动态生成 SVG 图像作为 JSX 组件。到目前为止,一切都很好——但现在我需要使用 SVG 作为 Canvas 元素中的图像源。这适用于静态 SVG 文件,但如何将动态 SVG 放入画布?
sn-p 中出现了一个简化版本。在 componentDidMount 方法中显示了 drawImage 调用的两种方法:创建一个未挂载的 SvgSource,以及使用一个挂载在页面上的 ref,但它们都失败了。
class App extends React.Component {
// On mount, paint the SVG in the canvas
componentDidMount(){
let ctx = this.canvasRef.getContext("2d")
// THIS DOES NOT WORK:
//ctx.drawImage(new SvgSource({fill: "green"}), 50, 50);
// NOR DOES THIS:
//ctx.drawImage(this.svgRef, 50, 50);
/* TypeError: Argument 1 of CanvasRenderingContext2D.drawImage could not be converted to any of: HTMLImageElement, SVGImageElement, HTMLCanvasElement, HTMLVideoElement, ImageBitmap. */
}
render() {
return (
<div>
{/* This works, inserting the SvgSource element directly - but that's not what I want. */}
<SvgSource ref={s => this.svgRef = s} fill="blue" />
{/* I want to use the SVG as an image source in this canvas. */}
<canvas
ref={
c => this.canvasRef = c
/* NB: using older ref syntax because jsFiddle uses React .14 - use CreateRef with version 16 */
}
width={200}
height={200} />
</div>
);
}
}
// Our JSX SVG component that provides the custom image to use in the canvas
class SvgSource extends React.Component {
render() {
return (
<svg width={100} height={100} viewBox="0 0 100 100">
<circle cx={50} cy={50} r={25} fill={this.props.fill || "red"}/>
</svg>
);
}
}
ReactDOM.render(<App />, document.querySelector("#app"))
body {
background: #20262E;
padding: 20px;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
min-height: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>
澄清一下:我知道如何将普通的 SVG 图像插入到画布元素中。问题是将 JSX SVG 转换为画布上下文中 drawImage 的有效源,因此这一切都可以在 React 组件中完成。
【问题讨论】:
-
谢谢,我没有看到这些,但它们并没有真正帮助我。我知道如何将 SVG 绘制到画布上。具体来说,就是将表示 SVG 的 JSX 对象转换为上下文将接受的东西作为我无法理解的图像的步骤。
-
你只需要把这个jsx对象转换成标记。链接的答案为您提供了 DOM 以将 uri 标记为图像部分。虽然我不是反应忍者,但您似乎已经掌握了该标记。