我有以下代碼,我試圖將看到的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
。