美文网首页
Three.js生成一个可拖拽的3D窗口

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

作者: ShawnWeasley | 来源:发表于2023-03-28 15:56 被阅读0次

如下图:



源码:

<!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>

相关文章

  • 可拖拽浮动窗口

    转载一个web可浮动窗口示例转载于http://www.jq22.com/webqd5574

  • Three.js

    Three.js 1. 概述 1.1 什么是Three.js Three.js是一个3D javascript库。...

  • 文件拖拽

    文件拖拽ondragover 拖拽文件进入窗口 (不断执行)ondragenter 拖拽文件进入窗口 ...

  • 可拖拽的小窗口,最好用?

    一、前言 1、写了10多天的小程序代码,有兴趣的可以看我这篇小程序官方文档-小程序版【持续更新】,被坑得有点晕,突...

  • ThingJS 使用

    ThingJS,注意不是 Three.js,是比Three.js 封装度更高得3D框架,但不是开源的,使用得付费。...

  • Three.js源码解读二:Geometry

    (一)基础知识 网格(Mesh) Geometry是Three.js对3D物体的一个整合,记录了渲染一个3D物体所...

  • 添加插件

    生成Podfile 1.打开终端2.输入命令:cd (此处有一个空格)3.将项目文件夹拖拽到终端窗口,项目目录自动...

  • Three.js笔记(二)啥是Three.js,它都有啥用

    啥是Three.js 它是一个关于3D的JavaScript库,用来帮助开发人员创建3D网页。其基于WebGL,但...

  • Three.js入门(二)——画星空

    three.js是JavaScript编写的WebGL第三方库。提供了非常多的3D显示功能。Three.js 是一...

  • three.js 全景重力感应

    实现three.js 全景图 demo 使用three.js 写了球体和圆柱体版本的3D重力感应全景图,支持手指触...

网友评论

      本文标题:Three.js生成一个可拖拽的3D窗口

      本文链接:https://www.haomeiwen.com/subject/iriykdtx.html