66. three.js Canvas画布布局

Canvas元素

Canvas是HTML的元素之一,Canvas元素的CSS布局规律和div、img等其它HTML元素相似,webgl就是依赖于canvas实现,threejs是基于webgl封装的,自然也要依赖canvas,具体说就是把三场场景渲染到canvas画布上。

<!-- canvas元素默认是行内块元素 -->
<canvas style="background-color: #ff0000;" width="300" height="180"></canvas>

threejs渲染结果CSS布局

浏览器控制台通过元素选项查看threejs输出的cnavas画布,你可以看到threejs默认把canvas设置为块元素display: block;

既然three.js渲染输出的Canvas画布本质上就是一个HTML元素,那么你布局的时候,直接使用你的前端CSS知识即可。

案例源码“CSS布局案例.html”

案例源码“CSS布局案例.html”提供了一个CSS的布局案例,你可以把threejs画布插入右下角的div元素中,并完全填充。

案例源码“CSS布局案例.html”的布局效果,你也可以用CSS其它方式实现,不一定和课程中一样,CSS布局是前端知识,这里不展开讲解,咱们把重点放在threejs上面。

<head>
    <style>
        body {
            overflow: hidden;
            margin: 0px;
        }
    </style>
</head>
<body style="background: #555555;">
    <div style="height: 55px;background-color: #444444;">上面布局</div>
    <!-- 下 -->
    <div>
        <div id="left" style="position: absolute;top: 60px;background-color: #444444;width: 195px;">下面左侧布局</div>
        <div id="webgl" style="position: absolute;top: 60px;left: 200px;background-color: #444444;">下面右侧布局</div>
    </div>
    <script>
        const width = window.innerWidth - 200;
        const height = window.innerHeight - 60;
        document.getElementById('left').style.height = height + 'px';
        document.getElementById('webgl').style.width = width + 'px';
        document.getElementById('webgl').style.height = height + 'px';
    </script>
</body>

threejs Canvas画布尺寸设置

设置canvas画布的尺寸,注意在浏览器窗口文档区域整体宽高基础上,减掉其他顶部和左侧div元素和布局间隙的尺寸。

  • canvas画布的宽度: window.innerWidth减掉左侧div元素宽度195px和布局间距5px
  • canvas画布的高度: window.innerHeight减掉顶部div元素高度55px和布局间距5px
// 200表示左侧div元素宽度195px+间距5px
const width = window.innerWidth - 200; //canvas画布高度
//60表示顶部div元素高度55px+间距5px
const height = window.innerHeight - 60; //canvas画布宽度
...
const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
renderer.setSize(width, height);
// 200表示左侧div元素宽度195+间距5px
const width = window.innerWidth - 200; //canvas画布高度
//60表示顶部div元素高度55px+间距5px
const height = window.innerHeight - 60; //canvas画布宽度
...
const camera = new THREE.PerspectiveCamera(30, width / height, 1, 3000);
renderer.setSize(width, height);

Canvas随着窗口变化

// 画布跟随窗口变化
window.onresize = function () {
    const width = window.innerWidth - 200; //canvas画布高度
    const height = window.innerHeight - 60; //canvas画布宽度
    renderer.setSize(width, height);
    camera.aspect = width / height;
    camera.updateProjectionMatrix();
};
在使用Three.js创建的canvas画布布局方面,你可以根据你的项目需求将渲染结果renderer.domElement插入到web页面上的任何一个元素中。你可以通过设置元素的样式来控制canvas画布的位置和大小。例如,你可以使用div元素来容纳canvas画布,并设置div元素的样式来控制canvas画布的位置和边距。下面是一个示例代码: ```html <div id="webgl" style="margin-top: 100px; margin-left: 200px;"></div> ``` 在这个示例中,我们使用一个id为"webgl"的div元素来容纳canvas画布,并设置了margin-top和margin-left属性来控制canvas画布的位置。你可以根据需要调整这些属性的值来实现你想要的布局效果。然后,你可以使用JavaScript代码将渲染结果插入到这个div元素中,例如: ```javascript document.getElementById('webgl').appendChild(renderer.domElement); ``` 这样,canvas画布就会被插入到id为"webgl"的div元素中,实现了你所需的布局效果。你也可以根据需要将canvas画布插入到其他元素中,只要符合你项目的布局规则即可。 #### 引用[.reference_title] - *1* *3* [Canvas画布布局和全屏展示](https://blog.csdn.net/BradenHan/article/details/130055592)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Three.js教程:Canvas画布布局和全屏](https://blog.csdn.net/ygtu2018/article/details/131300425)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值