我已经将 Fabric 用于概念验证项目,总体思路与 D3 相同。请记住,Fabric 对 DOM 元素进行操作,而 React 将数据渲染到 DOM 中,通常后者是延迟的。有两件事可以帮助您确保代码正常工作:
等待组件安装完毕
为此,请将您的 Fabric 实例化放入componentDidMount
:
import React, { Component } from 'react';
import { fabric } from 'react-fabricjs';
import styles from './MyComponent.css';
class MyComponent extends Component {
componentWillMount() {
// dispatch some actions if you use Redux
}
componentDidMount() {
const canvas = new fabric.Canvas('c');
// do some stuff with it
}
render() {
return (
<div className={styles.myComponent}>
<canvas id="c" />
</div>
)
}
}
将 Fabric 构造函数放入其中componentDidMount
可确保它不会失败,因为在执行此方法时,DOM 已准备就绪。(但props有时不是,以防万一你使用 Redux)
使用 refs 计算实际宽度和高度
Refs是对实际 DOM 元素的引用。您可以使用 refs 执行使用 DOM API 对 DOM 元素执行的操作:选择子项、查找父项、分配样式属性、计算innerHeight
和innerWidth
. 后者正是您所需要的:
componentDidMount() {
const canvas = new fabric.Canvas('c', {
width: this.refs.canvas.clientWidth,
height: this.refs.canvas.clientHeight
});
// do some stuff with it
}
不要忘记定义refs
的属性this
。为此,您需要一个构造函数。整个事情看起来像
import React, { Component } from 'react';
import { fabric } from 'react-fabricjs';
import styles from './MyComponent.css';
class MyComponent extends Component {
constructor() {
super()
this.refs = {
canvas: {}
};
}
componentWillMount() {
// dispatch some actions if you use Redux
}
componentDidMount() {
const canvas = new fabric.Canvas('c', {
width: this.refs.canvas.clientWidth,
height: this.refs.canvas.clientHeight
});
// do some stuff with it
}
render() {
return (
<div className={styles.myComponent}>
<canvas
id="c"
ref={node => {
this.refs.canvas = node;
} />
</div>
)
}
}
将 Fabric 与组件状态或props混合
您可以让 Fabric 实例对任何组件props或状态更新做出react。要使其工作,只需在componentDidUpdate
. 简单地依赖render
函数调用不会真正有用,因为渲染的元素都不会在新props或新状态上发生变化。像这样的东西:
import React, { Component } from 'react';
import { fabric } from 'react-fabricjs';
import styles from './MyComponent.css';
class MyComponent extends Component {
constructor() {
this.refs = {
canvas: {}
};
}
componentWillMount() {
// dispatch some actions if you use Redux
}
componentDidMount() {
const canvas = new fabric.Canvas('c', {
width: this.refs.canvas.clientWidth,
height: this.refs.canvas.clientHeight
});
this.fabric = canvas;
// do some initial stuff with it
}
componentDidUpdate() {
const {
images = []
} = this.props;
const {
fabric
} = this;
// do some stuff as new props or state have been received aka component did update
images.map((image, index) => {
fabric.Image.fromURL(image.url, {
top: 0,
left: index * 100 // place a new image left to right, every 100px
});
});
}
render() {
return (
<div className={styles.myComponent}>
<canvas
id="c"
ref={node => {
this.refs.canvas = node;
} />
</div>
)
}
}
只需用您需要的代码替换图像渲染,这取决于新的组件状态或props。不要忘记在画布上渲染新对象之前清理画布!