Three.js生成一个可拖拽的3D窗口

如下图:

源码:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Draggable and Auto-moving Floating Windows</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }

        .floating-window {
            position: absolute;
            width: 300px;
            height: 190px;
            background-color: #f1f1f1;
            border: 1px solid #d3d3d3;
            z-index: 9999;
        }

        .floating-window-header {
            padding: 10px;
            cursor: move;
            background-color: #2196F3;
            color: #fff;
            font-weight: bold;
        }

        .floating-window-content {
            padding: 0px;

        }
    </style>
</head>

<body>
    <div id="floatingWindow" class="floating-window" style="top: 200px; left: 300px;">
        <div id="floatingWindowHeader" class="floating-window-header">
            悬浮窗1
        </div>
        <div class="floating-window-content">
            <canvas id="threeCanvas" style="width: 100%; height: 100%;"></canvas>
        </div>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script>
        // 可拖动悬浮窗
        const floatingWindow = document.getElementById("floatingWindow");
        const floatingWindowHeader = document.getElementById("floatingWindowHeader");

        let offsetX = 0;
        let offsetY = 0;
        let isMouseDown = false;

        floatingWindowHeader.addEventListener("mousedown", (e) => {
            offsetX = e.clientX - floatingWindow.getBoundingClientRect().left;
            offsetY = e.clientY - floatingWindow.getBoundingClientRect().top;
            isMouseDown = true;
        });

        document.addEventListener("mousemove", (e) => {
            if (!isMouseDown) return;
            floatingWindow.style.left = e.clientX - offsetX + "px";
            floatingWindow.style.top = e.clientY - offsetY + "px";
        });

        document.addEventListener("mouseup", () => {
            isMouseDown = false;
        });

        // Three.js渲染器
        const canvas = document.getElementById("threeCanvas");
        const renderer = new THREE.WebGLRenderer({ canvas });

        // Three.js相机
        const camera = new THREE.PerspectiveCamera(75, canvas.clientWidth / canvas.clientHeight, 0.1, 1000);
        camera.position.z = 5;

        // Three.js场景
        const scene = new THREE.Scene();

        // Three.js立方体
        const geometry = new THREE.BoxGeometry();
        const material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        // Three.js渲染循环
        function render() {
            requestAnimationFrame(render);

            cube.rotation.x += 0.01;
            cube.rotation.y += 0.01;

            renderer.render(scene, camera);
        }
        render();
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值