
本文详细指导如何将codepen上的前端项目,特别是涉及外部库和模块的mediapipe人脸关键点检测项目,成功部署到本地运行。文章聚焦于解决依赖引入、javascript模块加载、资源路径配置及跨域等常见问题,提供了一份完整的html代码示例,帮助开发者顺利实现codepen项目的本地化调试与开发。
在前端开发中,CodePen等在线代码编辑器为快速原型设计和分享提供了极大便利。然而,当需要将这些在线项目迁移到本地环境进行更深入的开发或调试时,开发者常会遇到一些挑战,尤其是在处理外部库、模块化脚本和资源路径方面。本文将以一个MediaPipe人脸关键点检测项目为例,详细阐述如何解决这些问题,确保项目能在本地顺利运行。
本地化CodePen项目面临的挑战
将CodePen项目直接复制粘贴到本地往往无法正常工作,主要原因包括:
- 外部css/js库的引入方式差异: CodePen通常通过其ui界面或特定的预处理器语法(如scss的@use)来管理外部库。在本地,这些库需要通过标准的<link>标签引入CSS文件,或通过<script>标签引入javaScript文件,且通常需要指向CDN地址。
- javascript模块化: 现代JavaScript项目广泛使用ES Modules(import/export语法)。在浏览器环境中,使用import语句的脚本必须通过<script type=”module”>标签加载,并且导入路径需要是有效的URL(通常是CDN)。
- 资源路径问题: 项目中引用的图片、模型文件(如MediaPipe的.task文件)或WebAssembly(wasm)文件,在CodePen上可能通过相对路径或CodePen内部机制访问。本地化时,这些资源需要确保能够被正确访问,通常意味着需要使用完整的CDN路径。
- 跨域资源共享(CORS): 当项目从不同源加载图片并将其绘制到<canvas>元素上进行处理时(例如图像分析),浏览器会触发CORS策略。此时,<img>标签需要添加crossorigin=”anonymous”属性,以允许跨域加载并处理图片数据。
解决方案:构建一个完整的本地html文件
解决上述问题的最直接方法是创建一个独立的HTML文件,将所有必要的HTML结构、CSS样式和JavaScript代码整合进去,并确保所有外部依赖都通过正确的CDN链接引入。
1. HTML结构与元数据
一个标准的HTML文件需要包含<html>、<head>和<body>标签。<head>中应包含字符集声明、视口设置以及页面标题等基本元数据。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate"> <meta http-equiv="Pragma" content="no-cache"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <title>Face Landmarker</title> <!-- 样式和脚本将在此处添加 --> </head> <body> <!-- 页面内容将在此处添加 --> </body> </html>
2. 样式引入 (CSS Integration)
原CodePen项目可能使用了SCSS的@use “@material”;来引入Material Design样式。在本地,我们应移除此SCSS语法,转而通过CDN链接直接引入Material Components Web的CSS文件。项目特有的CSS可以直接内联到<style>标签中。
<head> <!-- ...其他meta标签... --> <title>Face Landmarker</title> <style> /* 移除 @use "@material"; */ body { font-family: helvetica, arial, sans-serif; margin: 2em; color: #3d3d3d; --mdc-theme-primary: #007f8b; --mdc-theme-on-primary: #f1f3f4; } h1 { font-style: italic; color: #ff6f00; color: #007f8b; } h2 { clear: both; } em { font-weight: bold; } video { clear: both; display: block; transform: rotateY(180deg); -webkit-transform: rotateY(180deg); -moz-transform: rotateY(180deg); } section { opacity: 1; transition: opacity 500ms ease-in-out; } header, footer { clear: both; } .removed { display: none; } .invisible { opacity: 0.2; } .note { font-style: italic; font-size: 130%; } .videoView, .detectOnClick, .blend-shapes { position: relative; float: left; width: 48%; margin: 2% 1%; cursor: pointer; } .videoView p, .detectOnClick p { position: absolute; padding: 5px; background-color: #007f8b; color: #fff; border: 1px dashed rgba(255, 255, 255, 0.7); z-index: 2; font-size: 12px; margin: 0; } .highlighter { background: rgba(0, 255, 0, 0.25); border: 1px dashed #fff; z-index: 1; position: absolute; } .canvas { z-index: 1; position: absolute; pointer-events: none; } .output_canvas { transform: rotateY(180deg); -webkit-transform: rotateY(180deg); -moz-transform: rotateY(180deg); } .detectOnClick { z-index: 0; } .detectOnClick img { width: 100%; } .blend-shapes-item { display: flex; align-items: center; height: 20px; } .blend-shapes-label { display: flex; width: 120px; justify-content: flex-end; align-items: center; margin-right: 4px; } .blend-shapes-value { display: flex; height: 16px; align-items: center; background-color: #007f8b; } </style> <!-- 通过CDN链接引入Material Components Web CSS --> <link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet"> <!-- 通过CDN链接引入Material Components Web JS --> <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script> </head>
3. 脚本引入 (JavaScript Integration)
JavaScript部分是本地化过程中最容易出错的地方。
- 首先,Material Components Web的JavaScript库也需要通过CDN链接引入。
- 其次,MediaPipe Vision库的导入必须使用<script type=”module”>标签,并确保import语句指向正确的CDN URL。
- WASM文件和模型文件的路径也必须是可访问的CDN地址。
- 对于需要进行图像处理的<img>标签,务必添加crossorigin=”anonymous”属性。
<body> <h1>Face landmark detection using the MediaPipe FaceLandmarker task</h1> <section id="demos" class="invisible"> <h2>Demo: Detecting Images</h2> <p><b>Click on an image below</b> to see the key landmarks of the face.</p> <div class="detectOnClick"> <!-- 注意 crossorigin="anonymous" 属性 --> <img src="https://storage.googleapis.com/mediapipe-assets/portrait.jpg" width="100%" crossorigin="anonymous" title="Click to get detection!" /> </div> <div class="blend-shapes"> <ul class="blend-shapes-list" id="image-blend-shapes"></ul> </div> <h2>Demo: Webcam continuous face landmarks detection</h2> <p>Hold your face in front of your webcam to get real-time face landmarker detection.</br>Click <b>enable webcam</b> below and grant access to the webcam if prompted.</p> <div id="liveView" class="videoView"> <button id="webcamButton" class="mdc-button mdc-button--raised"> <span class="mdc-button__ripple"></span> <span class="mdc-button__label">ENABLE WEBCAM</span> </button> <div style="position: relative;"> <video id="webcam" style="position: abso" autoplay playsinline></video> <canvas class="output_canvas" id="output_canvas" style="position: absolute; left: 0px; top: 0px;"></canvas> </div> </div> <div class="blend-shapes"> <ul class="blend-shapes-list" id="video-blend-shapes"></ul> </div> </section> <!-- 核心 JavaScript 代码,使用 type="module" --> <script type="module"> // 确保 MediaPipe Vision 库从 CDN 正确导入,并指定版本 import vision from "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0"; const { FaceLandmarker, FilesetResolver, DrawingUtils } = vision; const demosSection = document.getElementById("demos"); const imageBlendShapes = document.getElementById("image-blend-shapes"); const videoBlendShapes = document.getElementById("video-blend-shapes"); let faceLandmarker; let runningMode= "IMAGE" | "VIDEO"; let enableWebcamButton = HTMLButtonElement; let webcamRunning= Boolean = false; const videoWidth = 480; async function runDemo() { // FilesetResolver 需要指向正确的 WASM 文件路径,通常也是 CDN const filesetResolver = await FilesetResolver.forVisionTasks( "https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@0.10.0/wasm" ); faceLandmarker = await FaceLandmarker.createFromOptions(filesetResolver, { baseoptions: { // 模型资产路径也需要是 CDN 地址 modelAssetPath: `https://storage.googleapis.com/mediapipe-models/face_landmarker/face_landmarker/float16/1/face_landmarker.task`, delegate: "GPU" }, outputFaceBlendshapes: true, runningMode, numFaces: 1 }); demosSection.classlist.remove("