我有以下代码,我试图将看到的3d(使用REGL
)渲染到React组件App
中。它似乎首先渲染得很好。但我注意到,如果我调整浏览器窗口的大小,组件呈现的div高度会增加。因此,任何窗口调整意味着直接转换成高度增长,直到div高于窗口。我试图了解REGL
和REACT
如何一起工作,所以我不确定将该行为归因于什么。这可能是我的误解。REGL在React组件中渲染
import React, {
Component
} from 'react';
import regl from 'regl';
class App extends Component {
constructor() {
super()
this.state = {
reglTest: "Test REGL",
};
}
componentDidMount() {
const rootDiv = document.getElementById('reglTest');
console.log(rootDiv);
var reglObj = regl({
container: rootDiv,
})
reglObj.frame(({
tick
}) => {
reglObj.clear({
color: [(tick % 100 * 0.01), 0, 0, 1],
depth: 1,
});
reglObj({
frag: `
void main() {
gl_FragColor = vec4(1, 0, 0, 1);
}`,
vert: `
attribute vec2 position;
void main() {
gl_Position = vec4(position, 0, 1);
}`,
attributes: {
position: [
[(tick % 100 * 0.01), -1],
[-1, 0],
[1, 1]
]
},
count: 3
})()
});
}
render() {
return (<div id = "reglTest" > {this.state.reglTest} < /div>);
}
}
export default App;
编辑:
我能够追踪bug被纠正了在REGL
文件resize
功能。
function resize() {
var w = window.innerWidth;
var h = window.innerHeight;
if (element !== document.body) {
var bounds = element.getBoundingClientRect();
w = bounds.right - bounds.left;
h = bounds.bottom - bounds.top;
}
canvas.width = pixelRatio * w;
canvas.height = pixelRatio * h;
extend(canvas.style, {
width: w + 'px',
height: h + 'px'
});
}
它计算h
卷起一些高值(比方说1000+调整一点点浏览器窗口之后),而window.innerHeight
保持在发言权320
。