求助:JavaScript中Neon Cursor功能异常排查
Neon Cursor功能异常排查与修复
问题背景
实现的Neon Cursor功能出现异常,尝试导入Three.js(明知该库并非必需)后仍无法解决,怀疑是JavaScript依赖导入方式错误导致。
核心问题分析
当前代码中直接使用import { neonCursor } from 'threejs-toys'属于裸导入,浏览器环境下无法识别这种无完整路径的导入语句,必须指定模块的完整CDN地址,这是功能异常的根本原因。
修复方案
- 替换JavaScript中的导入语句,使用
threejs-toys官方提供的CDN模块地址 - 保持HTML中的脚本标签
type="module"属性,确保ES模块机制正常工作
修正后的完整代码
HTML
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>VD Capital</title> <link rel="stylesheet" href="test.css"> <script src="https://kit.fontawesome.com/365cb50038.js" crossorigin="anonymous"></script> <link rel="preconnect" href="https://fonts.googleapis.com"> <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin> <link href="https://fonts.googleapis.com/css2?family=Kumbh+Sans:wght@400;700&display=swap" rel="stylesheet"> </head> <body> <div id="app"> <div id="hero"> <h1>NEON<br/>CURSOR</h1> <a target="_blank" href="https://github.com/klevron/threejs-toys">github/threejs-toys</a> </div> </div> <script type="module" src="test.js"></script> </body> </html>
JavaScript
// 使用CDN地址导入threejs-toys模块 import { neonCursor } from 'https://unpkg.com/threejs-toys@0.0.8/dist/threejs-toys.module.cdn.js' neonCursor({ el: document.getElementById('app'), shaderPoints: 16, curvePoints: 80, curveLerp: 0.5, radius1: 5, radius2: 30, velocityTreshold: 10, sleepRadiusX: 100, sleepRadiusY: 100, sleepTimeCoefX: 0.0025, sleepTimeCoefY: 0.0025 })
CSS
body, html, #app { margin: 0; width: 100%; height: 100%; } #app { overflow: hidden; touch-action: pan-up; color: #ffffff; font-family: 'Montserrat', sans-serif; text-align: center; text-shadow: 0 0 5px #ffffff, 0 0 20px #000, 0 0 30px #000; } #app h1 { --fontSize: 60px; --lineHeight: 80px; width: auto; height: calc(2 * var(--lineHeight)); line-height: var(--lineHeight); margin: calc(50vh - var(--lineHeight)) auto 0; font-size: var(--fontSize); text-transform: uppercase; } #app a { margin-top: 10px; display: inline-block; text-decoration: none; color: #fff; } #app canvas { display: block; position: fixed; z-index: -1; top: 0; }
补充说明
- 若后续
threejs-toys版本更新,可替换CDN地址中的版本号(@0.0.8)以获取最新功能 - 无需额外导入Three.js,
threejs-toys内部已封装所需依赖
内容的提问来源于stack exchange,提问作者Andrethebeginner
相关产品推荐
相关产品推荐

