使用 HTML + JavaScript 实现图片裁剪上传功能

本文将详细介绍一个基于 HTML 和 JavaScript 实现的图片裁剪上传功能。该功能支持文件选择、拖放上传、图片预览、区域选择、裁剪操作以及图片下载等功能,适用于需要进行图片处理的 Web 应用场景。

效果演示

image-20250602112435691

image-20250602112543017

项目概述

本项目主要包含以下核心功能:

  • 文件选择与拖放上传
  • 裁剪框拖动与调整大小
  • 图片裁剪
  • 图片上传(模拟)与下载

页面结构

上传区域

实现友好的文件选择体验,并支持拖放上传。

<div class="upload-section"><input type="file" id="fileInput" accept="image/*"><label for="fileInput" class="file-label">选择图片</label><p>或拖放图片到此处</p>
</div>
预览区域

分为两个部分,左侧显示原始图片和裁剪框,右侧展示裁剪后的结果。

<div class="preview-section"><div class="image-container"><div><h3>原始图片</h3><img id="originalImage" style="max-width: 100%; display: none;"><div class="cropper-container" id="cropperContainer" style="display: none;"><canvas id="sourceCanvas"></canvas><div class="selection-box" id="selectionBox"><div class="resize-handle"></div></div></div><p class="instruction">拖动选择框可移动位置,拖动右下角可调整大小</p></div><div><h3>裁剪结果</h3><canvas id="croppedCanvas" style="display: none;"></canvas><p id="noCropMessage">请先选择图片并设置裁剪区域</p></div></div>
</div>
操作按钮

提供“裁剪”、“上传”、“下载”和“重置”按钮,方便用户进行各种操作。

<div class="controls"><button id="cropBtn" disabled>裁剪图片</button><button id="uploadBtn" disabled>上传图片</button><button id="downloadBtn" disabled>下载图片</button><button id="resetBtn">重置</button>
</div>

核心功能实现

定义基础变量

获取DOM元素

const fileInput = document.getElementById('fileInput');
const originalImage = document.getElementById('originalImage');
const sourceCanvas = document.getElementById('sourceCanvas');
const croppedCanvas = document.getElementById('croppedCanvas');
const cropperContainer = document.getElementById('cropperContainer');
const selectionBox = document.getElementById('selectionBox');
const cropBtn = document.getElementById('cropBtn');
const uploadBtn = document.getElementById('uploadBtn');
const resetBtn = document.getElementById('resetBtn');
const noCropMessage = document.getElementById('noCropMessage');
const downloadBtn = document.getElementById('downloadBtn');

定义全局变量

let isDragging = false;
let isResizing = false;
let startX, startY;
let selection = {x: 0,y: 0,width: 0,height: 0,startX: 0,startY: 0,startWidth: 0,startHeight: 0
};
let imageRatio = 1;
文件选择

使用 FileReader API 将选中的图片读取为 Data URL 并显示在页面上。

function handleFileSelect(event) {const file = event.target.files[0];if (!file || !file.type.match('image.*')) {alert('请选择有效的图片文件');return;}const reader = new FileReader();reader.onload = function(e) {originalImage.src = e.target.result;originalImage.onload = function() {initCropper();};};reader.readAsDataURL(file);
}
拖放上传

通过监听 dragover、dragleave 和 drop 事件实现拖放上传功能。

const uploadSection = document.querySelector('.upload-section');
uploadSection.addEventListener('dragover', (e) => {e.preventDefault();uploadSection.style.borderColor = '#4CAF50';
});uploadSection.addEventListener('dragleave', () => {uploadSection.style.borderColor = '#ccc';
});uploadSection.addEventListener('drop', (e) => {e.preventDefault();uploadSection.style.borderColor = '#ccc';if (e.dataTransfer.files.length) {fileInput.files = e.dataTransfer.files;handleFileSelect({ target: fileInput });}
});
图片预览与裁剪框初始化

在图片加载完成后,绘制到 canvas 上,并根据图片尺寸调整画布大小。初始化一个固定比例的裁剪框,居中显示在画布上。

function initCropper() {// 显示原始图片和裁剪区域originalImage.style.display = 'none';cropperContainer.style.display = 'inline-block';// 设置canvas尺寸const maxWidth = 500;imageRatio = originalImage.naturalWidth / originalImage.naturalHeight;let canvasWidth, canvasHeight;if (originalImage.naturalWidth > maxWidth) {canvasWidth = maxWidth;canvasHeight = maxWidth / imageRatio;} else {canvasWidth = originalImage.naturalWidth;canvasHeight = originalImage.naturalHeight;}sourceCanvas.width = canvasWidth;sourceCanvas.height = canvasHeight;// 绘制图片到canvasconst ctx = sourceCanvas.getContext('2d');ctx.drawImage(originalImage, 0, 0, canvasWidth, canvasHeight);// 初始化选择框 (1:1比例)const boxSize = Math.min(canvasWidth, canvasHeight) * 0.6;selection = {x: Math.max(0, Math.min((canvasWidth - boxSize) / 2, canvasWidth - boxSize)), // 确保初始位置在画布范围内y: Math.max(0, Math.min((canvasHeight - boxSize) / 2, canvasHeight - boxSize)), // 确保初始位置在画布范围内width: boxSize,height: boxSize,startX: 0,startY: 0,startWidth: 0,startHeight: 0};updateSelectionBox();cropBtn.disabled = false;
}
裁剪框拖动与调整大小

通过监听鼠标事件(mousedown、mousemove、mouseup)实现裁剪框的拖动和调整大小功能。确保裁剪框始终位于画布范围内,并保持指定的比例。

selectionBox.addEventListener('mousedown', startDrag);
document.addEventListener('mousemove', handleDrag);
document.addEventListener('mouseup', endDrag);
const resizeHandle = document.querySelector('.resize-handle');
resizeHandle.addEventListener('mousedown', (e) => {e.stopPropagation();startResize(e);
});
function startDrag(e) {if (e.target.classList.contains('resize-handle')) {return; // 忽略调整大小手柄的点击}isDragging = true;startX = e.clientX;startY = e.clientY;// 存储初始位置selection.startX = selection.x;selection.startY = selection.y;e.preventDefault();
}
function startResize(e) {isResizing = true;startX = e.clientX;startY = e.clientY;// 存储初始尺寸和位置selection.startX = selection.x;selection.startY = selection.y;selection.startWidth = selection.width;selection.startHeight = selection.height;e.preventDefault();
}
function handleDrag(e) {if (!isDragging && !isResizing) return;const dx = e.clientX - startX;const dy = e.clientY - startY;if (isDragging) {// 处理移动选择框let newX = selection.startX + dx;let newY = selection.startY + dy;// 限制在canvas范围内newX = Math.max(0, Math.min(newX, sourceCanvas.width - selection.width));newY = Math.max(0, Math.min(newY, sourceCanvas.height - selection.height));selection.x = newX;selection.y = newY;} else if (isResizing) {// 处理调整大小 (保持1:1比例)let newSize = Math.max(10, Math.min(selection.startWidth + (dx + dy) / 2, // 取dx和dy的平均值使调整更平滑Math.min(sourceCanvas.width - selection.startX,sourceCanvas.height - selection.startY)));// 应用新尺寸 (保持正方形)selection.width = newSize;selection.height = newSize;// 确保裁剪框不会超出画布范围if (selection.x + selection.width > sourceCanvas.width) {selection.x = sourceCanvas.width - selection.width;}if (selection.y + selection.height > sourceCanvas.height) {selection.y = sourceCanvas.height - selection.height;}}updateSelectionBox();
}
function endDrag() {isDragging = false;isResizing = false;
}
图片裁剪与结果展示

使用 drawImage 方法从源画布中裁剪出指定区域,并将其绘制到目标画布上。

function cropImage() {const ctx = croppedCanvas.getContext('2d');// 设置裁剪后canvas的尺寸 (1:1)croppedCanvas.width = selection.width;croppedCanvas.height = selection.height;// 执行裁剪ctx.drawImage(sourceCanvas,selection.x, selection.y, selection.width, selection.height, // 源图像裁剪区域0, 0, selection.width, selection.height                     // 目标canvas绘制区域);// 显示裁剪结果croppedCanvas.style.display = 'block';noCropMessage.style.display = 'none';uploadBtn.disabled = false;downloadBtn.disabled = false;
}
图片上传与下载

提供模拟的上传功能,使用 toBlob 方法获取裁剪后的图片数据。支持将裁剪后的图片下载为 JPEG 格式的文件。

function uploadImage() {// 在实际应用中,这里应该将图片数据发送到服务器croppedCanvas.toBlob((blob) => {// 创建FormData对象并添加图片const formData = new FormData();formData.append('croppedImage', blob, 'cropped-image.jpg');// 模拟上传延迟setTimeout(() => {alert('图片上传成功!(模拟)');console.log('上传的图片数据:', blob);// 在实际应用中,你可能需要处理服务器响应}, 1000);}, 'image/jpeg', 0.9);
}
function downloadImage() {if (!croppedCanvas.width || !croppedCanvas.height) {alert('请先裁剪图片');return;}// 创建一个临时的a标签用于触发下载const link = document.createElement('a');link.href = croppedCanvas.toDataURL('image/jpeg', 0.9);link.download = 'cropped-image.jpg'; // 设置下载文件名link.click();
}

扩展建议

  • 支持多种裁剪比例:可以扩展代码以支持不同的裁剪比例(如 4:3、16:9),并通过 UI 控件让用户选择。
  • 图像缩放功能:添加对图片缩放的支持,允许用户放大或缩小图片以便更精确地选择裁剪区域。
  • 服务器端集成:实际应用中,应将裁剪后的图片发送到服务器进行存储和处理,可以通过请求实现。

完整代码

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>图片裁剪上传</title><style>body {max-width: 1200px;margin: 0 auto;padding: 20px;}.container {display: flex;flex-direction: column;gap: 20px;}h1 {text-align: center;}.upload-section {padding: 20px;text-align: center;border-radius: 5px;background: #f8f9fa;border: 2px dashed #dee2e6;transition: all 0.3s ease;cursor: pointer;}.upload-section:hover {border-color: #4CAF50;background: rgba(76, 175, 80, 0.05);}.file-label {background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);color: white;border-radius: 25px;padding: 12px 24px;transition: transform 0.2s;}.file-label:hover {transform: translateY(-2px);box-shadow: 0 4px 15px rgba(76, 175, 80, 0.3);}.preview-section {display: flex;flex-direction: column;gap: 20px;background: #ffffff;border-radius: 12px;padding: 20px;box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);}canvas {max-width: 100%;border: 1px solid #eee;display: block;}.cropper-container {position: relative;display: inline-block;}.selection-box {position: absolute;border: 2px dashed #000;background: rgba(255, 255, 255, 0.3);cursor: move;box-sizing: border-box;}.resize-handle {position: absolute;width: 10px;height: 10px;background: #fff;border: 2px solid #000;border-radius: 50%;bottom: -5px;right: -5px;cursor: se-resize;}button {padding: 10px 15px;background-color: #4CAF50;color: white;border: none;border-radius: 4px;cursor: pointer;font-size: 16px;}button:hover {background-color: #45a049;}button {background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);border-radius: 25px;padding: 12px 24px;font-weight: 500;box-shadow: 0 4px 6px rgba(76, 175, 80, 0.1);transition: all 0.3s ease;}button:hover {transform: translateY(-2px);box-shadow: 0 6px 12px rgba(76, 175, 80, 0.2);}button:disabled {opacity: 0.6;cursor: not-allowed;}input[type="file"] {display: none;}.file-label {display: inline-block;padding: 10px 15px;background-color: #f0f0f0;border-radius: 4px;cursor: pointer;margin-bottom: 10px;}.controls {display: flex;gap: 10px;margin-top: 10px;justify-content: center;}.instruction {font-size: 14px;color: #666;margin-top: 10px;}.image-container {display: flex;gap: 20px;}.image-container > div {width: 500px;}</style>
</head>
<body>
<div class="container"><h1>图片裁剪上传</h1><div class="upload-section"><input type="file" id="fileInput" accept="image/*"><label for="fileInput" class="file-label">选择图片</label><p>或拖放图片到此处</p></div><div class="preview-section"><div class="image-container"><div><h3>原始图片</h3><img id="originalImage" style="max-width: 100%; display: none;"><div class="cropper-container" id="cropperContainer" style="display: none;"><canvas id="sourceCanvas"></canvas><div class="selection-box" id="selectionBox"><div class="resize-handle"></div></div></div><p class="instruction">拖动选择框可移动位置,拖动右下角可调整大小</p></div><div><h3>裁剪结果</h3><canvas id="croppedCanvas" style="display: none;"></canvas><p id="noCropMessage">请先选择图片并设置裁剪区域</p></div></div></div><div class="controls"><button id="cropBtn" disabled>裁剪图片</button><button id="uploadBtn" disabled>上传图片</button><button id="downloadBtn" disabled>下载图片</button><button id="resetBtn">重置</button></div>
</div><script>// 获取DOM元素const fileInput = document.getElementById('fileInput');const originalImage = document.getElementById('originalImage');const sourceCanvas = document.getElementById('sourceCanvas');const croppedCanvas = document.getElementById('croppedCanvas');const cropperContainer = document.getElementById('cropperContainer');const selectionBox = document.getElementById('selectionBox');const cropBtn = document.getElementById('cropBtn');const uploadBtn = document.getElementById('uploadBtn');const resetBtn = document.getElementById('resetBtn');const noCropMessage = document.getElementById('noCropMessage');const downloadBtn = document.getElementById('downloadBtn');// 全局变量let isDragging = false;let isResizing = false;let startX, startY;let selection = {x: 0,y: 0,width: 0,height: 0,startX: 0,startY: 0,startWidth: 0,startHeight: 0};let imageRatio = 1;// 监听文件选择fileInput.addEventListener('change', handleFileSelect);// 拖放功能const uploadSection = document.querySelector('.upload-section');uploadSection.addEventListener('dragover', (e) => {e.preventDefault();uploadSection.style.borderColor = '#4CAF50';});uploadSection.addEventListener('dragleave', () => {uploadSection.style.borderColor = '#ccc';});uploadSection.addEventListener('drop', (e) => {e.preventDefault();uploadSection.style.borderColor = '#ccc';if (e.dataTransfer.files.length) {fileInput.files = e.dataTransfer.files;handleFileSelect({ target: fileInput });}});// 选择框鼠标事件selectionBox.addEventListener('mousedown', startDrag);document.addEventListener('mousemove', handleDrag);document.addEventListener('mouseup', endDrag);// 调整大小手柄事件const resizeHandle = document.querySelector('.resize-handle');resizeHandle.addEventListener('mousedown', (e) => {e.stopPropagation();startResize(e);});// 下载图片function downloadImage() {if (!croppedCanvas.width || !croppedCanvas.height) {alert('请先裁剪图片');return;}// 创建一个临时的a标签用于触发下载const link = document.createElement('a');link.href = croppedCanvas.toDataURL('image/jpeg', 0.9);link.download = 'cropped-image.jpg'; // 设置下载文件名link.click();}// 按钮事件cropBtn.addEventListener('click', cropImage);uploadBtn.addEventListener('click', uploadImage);resetBtn.addEventListener('click', resetAll);downloadBtn.addEventListener('click', downloadImage);// 处理文件选择function handleFileSelect(event) {const file = event.target.files[0];if (!file || !file.type.match('image.*')) {alert('请选择有效的图片文件');return;}const reader = new FileReader();reader.onload = function(e) {originalImage.src = e.target.result;originalImage.onload = function() {initCropper();};};reader.readAsDataURL(file);}// 初始化裁剪器function initCropper() {// 显示原始图片和裁剪区域originalImage.style.display = 'none';cropperContainer.style.display = 'inline-block';// 设置canvas尺寸const maxWidth = 500;imageRatio = originalImage.naturalWidth / originalImage.naturalHeight;let canvasWidth, canvasHeight;if (originalImage.naturalWidth > maxWidth) {canvasWidth = maxWidth;canvasHeight = maxWidth / imageRatio;} else {canvasWidth = originalImage.naturalWidth;canvasHeight = originalImage.naturalHeight;}sourceCanvas.width = canvasWidth;sourceCanvas.height = canvasHeight;// 绘制图片到canvasconst ctx = sourceCanvas.getContext('2d');ctx.drawImage(originalImage, 0, 0, canvasWidth, canvasHeight);// 初始化选择框 (1:1比例)const boxSize = Math.min(canvasWidth, canvasHeight) * 0.6;selection = {x: Math.max(0, Math.min((canvasWidth - boxSize) / 2, canvasWidth - boxSize)), // 确保初始位置在画布范围内y: Math.max(0, Math.min((canvasHeight - boxSize) / 2, canvasHeight - boxSize)), // 确保初始位置在画布范围内width: boxSize,height: boxSize,startX: 0,startY: 0,startWidth: 0,startHeight: 0};updateSelectionBox();cropBtn.disabled = false;}// 更新选择框位置和尺寸function updateSelectionBox() {selectionBox.style.left = `${selection.x}px`;selectionBox.style.top = `${selection.y}px`;selectionBox.style.width = `${selection.width}px`;selectionBox.style.height = `${selection.height}px`;}// 开始拖动function startDrag(e) {if (e.target.classList.contains('resize-handle')) {return; // 忽略调整大小手柄的点击}isDragging = true;startX = e.clientX;startY = e.clientY;// 存储初始位置selection.startX = selection.x;selection.startY = selection.y;e.preventDefault();}// 处理拖动function handleDrag(e) {if (!isDragging && !isResizing) return;const dx = e.clientX - startX;const dy = e.clientY - startY;if (isDragging) {// 处理移动选择框let newX = selection.startX + dx;let newY = selection.startY + dy;// 限制在canvas范围内newX = Math.max(0, Math.min(newX, sourceCanvas.width - selection.width));newY = Math.max(0, Math.min(newY, sourceCanvas.height - selection.height));selection.x = newX;selection.y = newY;} else if (isResizing) {// 处理调整大小 (保持1:1比例)let newSize = Math.max(10, Math.min(selection.startWidth + (dx + dy) / 2, // 取dx和dy的平均值使调整更平滑Math.min(sourceCanvas.width - selection.startX,sourceCanvas.height - selection.startY)));// 应用新尺寸 (保持正方形)selection.width = newSize;selection.height = newSize;// 确保裁剪框不会超出画布范围if (selection.x + selection.width > sourceCanvas.width) {selection.x = sourceCanvas.width - selection.width;}if (selection.y + selection.height > sourceCanvas.height) {selection.y = sourceCanvas.height - selection.height;}}updateSelectionBox();}// 结束拖动或调整大小function endDrag() {isDragging = false;isResizing = false;}// 开始调整大小function startResize(e) {isResizing = true;startX = e.clientX;startY = e.clientY;// 存储初始尺寸和位置selection.startX = selection.x;selection.startY = selection.y;selection.startWidth = selection.width;selection.startHeight = selection.height;e.preventDefault();}// 裁剪图片function cropImage() {const ctx = croppedCanvas.getContext('2d');// 设置裁剪后canvas的尺寸 (1:1)croppedCanvas.width = selection.width;croppedCanvas.height = selection.height;// 执行裁剪ctx.drawImage(sourceCanvas,selection.x, selection.y, selection.width, selection.height, // 源图像裁剪区域0, 0, selection.width, selection.height                     // 目标canvas绘制区域);// 显示裁剪结果croppedCanvas.style.display = 'block';noCropMessage.style.display = 'none';uploadBtn.disabled = false;downloadBtn.disabled = false;}// 上传图片function uploadImage() {// 在实际应用中,这里应该将图片数据发送到服务器croppedCanvas.toBlob((blob) => {// 创建FormData对象并添加图片const formData = new FormData();formData.append('croppedImage', blob, 'cropped-image.jpg');// 模拟上传延迟setTimeout(() => {alert('图片上传成功!(模拟)');console.log('上传的图片数据:', blob);// 在实际应用中,你可能需要处理服务器响应}, 1000);}, 'image/jpeg', 0.9);}// 重置所有function resetAll() {// 隐藏元素cropperContainer.style.display = 'none';croppedCanvas.style.display = 'none';noCropMessage.style.display = 'block';originalImage.style.display = 'none';// 重置按钮状态cropBtn.disabled = true;uploadBtn.disabled = true;downloadBtn.disabled = true;// 清除文件输入fileInput.value = '';// 清除画布const ctx = sourceCanvas.getContext('2d');ctx.clearRect(0, 0, sourceCanvas.width, sourceCanvas.height);const croppedCtx = croppedCanvas.getContext('2d');croppedCtx.clearRect(0, 0, croppedCanvas.width, croppedCanvas.height);}
</script>
</body>
</html>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.pswp.cn/web/82303.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

GO+RabbitMQ+Gin+Gorm+docker 部署 demo

更多个人笔记见&#xff1a; &#xff08;注意点击“继续”&#xff0c;而不是“发现新项目”&#xff09; github个人笔记仓库 https://github.com/ZHLOVEYY/IT_note gitee 个人笔记仓库 https://gitee.com/harryhack/it_note 个人学习&#xff0c;学习过程中还会不断补充&…

【安全】VulnHub靶场 - W1R3S

【安全】VulnHub靶场 - W1R3S 备注一、故事背景二、Web渗透1.主机发现端口扫描2.ftp服务3.web服务 三、权限提升 备注 2025/05/22 星期四 简单的打靶记录 一、故事背景 您受雇对 W1R3S.inc 个人服务器进行渗透测试并报告所有发现。 他们要求您获得 root 访问权限并找到flag&…

WEB安全--SQL注入--MSSQL注入

一、SQLsever知识点了解 1.1、系统变量 版本号&#xff1a;version 用户名&#xff1a;USER、SYSTEM_USER 库名&#xff1a;DB_NAME() SELECT name FROM master..sysdatabases 表名&#xff1a;SELECT name FROM sysobjects WHERE xtypeU 字段名&#xff1a;SELECT name …

工作流引擎-18-开源审批流项目之 plumdo-work 工作流,表单,报表结合的多模块系统

工作流引擎系列 工作流引擎-00-流程引擎概览 工作流引擎-01-Activiti 是领先的轻量级、以 Java 为中心的开源 BPMN 引擎&#xff0c;支持现实世界的流程自动化需求 工作流引擎-02-BPM OA ERP 区别和联系 工作流引擎-03-聊一聊流程引擎 工作流引擎-04-流程引擎 activiti 优…

Docker 笔记 -- 借助AI工具强势辅助

常用命令 镜像管理命令&#xff1a; docker images&#xff08;列出镜像&#xff09; docker pull&#xff08;拉取镜像&#xff09; docker build&#xff08;构建镜像&#xff09; docker save/load&#xff08;保存/加载镜像&#xff09; 容器操作命令 docker run&#…

5G-A时代与p2p

5G-A时代正在走来&#xff0c;那么对P2P的影响有多大。 5G-A作为5G向6G过渡的关键技术&#xff0c;将数据下载速率从千兆提升至万兆&#xff0c;上行速率从百兆提升至千兆&#xff0c;时延降至毫秒级。这种网络性能的跨越式提升&#xff0c;为P2P提供了更强大的底层支撑&#x…

Redis-6.2.9 主从复制配置和详解

1 主从架构图 192.168.254.120 u24-redis-120 #主库 192.168.254.121 u24-redis-121 #从库 2 redis软件版本 rootu24-redis-121:~# redis-server --version Redis server v6.2.9 sha00000000:0 malloclibc bits64 build56edd385f7ce4c9b 3 主库redis配置文件(192.168.254.1…

004 flutter基础 初始文件讲解(3)

之前&#xff0c;我们正向的学习了一些flutter的基础&#xff0c;如MaterialApp&#xff0c;Scaffold之类的东西&#xff0c;那么接下来&#xff0c;我们将正式接触原代码&#xff1a; import package:flutter/material.dart;void main() {runApp(const MyApp()); }class MyAp…

Linux 系统 Docker Compose 安装

个人博客地址&#xff1a;Linux 系统 Docker Compose 安装 | 一张假钞的真实世界 本文方法是直接下载 GitHub 项目的 release 版本。项目地址&#xff1a;GitHub - docker/compose: Define and run multi-container applications with Docker。 执行以下命令将发布程序加载至…

Tree 树形组件封装

整体思路 数据结构设计 使用递归的数据结构&#xff08;TreeNode&#xff09;表示树形数据每个节点包含id、name、可选的children数组和selected状态 状态管理 使用useState在组件内部维护树状态的副本通过deepCopyTreeData函数进行深拷贝&#xff0c;避免直接修改原始数据 核…

tortoisegit 使用rebase修改历史提交

在 TortoiseGit 中使用 rebase 修改历史提交&#xff08;如修改提交信息、合并提交或删除提交&#xff09;的步骤如下&#xff1a; --- ### **一、修改最近一次提交** 1. **操作**&#xff1a; - 右键项目 → **TortoiseGit** → **提交(C)** - 勾选 **"Amend…

中科院报道铁电液晶:从实验室突破到多场景应用展望

2020年的时候&#xff0c;相信很多关注科技前沿的朋友都注意到&#xff0c;中国科学院一篇报道聚焦一项有望改写显示产业格局的新技术 —— 铁电液晶&#xff08;FeLC&#xff09;。这项被业内称为 "下一代显示核心材料" 的研究&#xff0c;究竟取得了哪些实质性进展…

论文阅读(六)Open Set Video HOI detection from Action-centric Chain-of-Look Prompting

论文来源&#xff1a;ICCV&#xff08;2023&#xff09; 项目地址&#xff1a;https://github.com/southnx/ACoLP 1.研究背景与问题 开放集场景下的泛化性&#xff1a;传统 HOI 检测假设训练集包含所有测试类别&#xff0c;但现实中存在大量未见过的 HOI 类别&#xff08;如…

74道Node.js高频题整理(附答案背诵版)

简述 Node. js 基础概念 &#xff1f; Node.js是一个基于Chrome V8引擎的JavaScript运行环境。它使得JavaScript可以在服务器端运行&#xff0c;从而进行网络编程&#xff0c;如构建Web服务器、处理网络请求等。Node.js采用事件驱动、非阻塞I/O模型&#xff0c;使其轻量且高效…

年龄是多少

有5个人坐在一起&#xff0c;问第五个人多少岁&#xff1f;他说比第四个人大两岁。问第四个人岁数&#xff0c;他说比第三个人大两岁。问第三个人&#xff0c;又说比第二个人大两岁。问第二个人&#xff0c;说比第一个人大两岁。最后问第一个人&#xff0c;他说是10岁。请问他们…

华为OD机试真题——模拟消息队列(2025A卷:100分)Java/python/JavaScript/C++/C语言/GO六种最佳实现

2025 A卷 100分 题型 本文涵盖详细的问题分析、解题思路、代码实现、代码详解、测试用例以及综合分析; 并提供Java、python、JavaScript、C++、C语言、GO六种语言的最佳实现方式! 2025华为OD真题目录+全流程解析/备考攻略/经验分享 华为OD机试真题《模拟消息队列》: 目录 题…

LangChain-结合GLM+SQL+函数调用实现数据库查询(三)

针对 LangChain-结合GLM+SQL+函数调用实现数据库查询(二)-CSDN博客 进一步简化 通过 LangChain 和大语言模型(GLM-4)实现了一个 AI 代理,能够根据自然语言提问自动生成 SQL 查询语句,并连接 MySQL 数据库执行查询,最终返回结果。 整个流程如下: 用户提问 → AI 生成 SQ…

ZLG ZCANPro,ECU刷新,bug分享

文章目录 摘要 📋问题的起因bug分享 ✨思考&反思 🤔摘要 📋 ZCANPro想必大家都不陌生,买ZLG的CAN卡,必须要用的上位机软件。在汽车行业中,有ECU软件升级的需求,通常都通过UDS协议实现程序的更新,满足UDS升级的上位机要么自己开发,要么用CANoe或者VFlash,最近…

第2期:APM32微控制器键盘PCB设计实战教程

第2期&#xff1a;APM32微控制器键盘PCB设计实战教程 一、APM32小系统介绍 使用apm32键盘小系统开源工程操作 APM32是一款与STM32兼容的微控制器&#xff0c;可以直接替代STM32进行使用。本教程基于之前开源的APM32小系统&#xff0c;链接将放在录播评论区中供大家参考。 1…

单元测试-断言常见注解

目录 1.断言 2.常见注解 3.依赖范围 1.断言 断言练习 package com.gdcp;import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test;//测试类 public class UserServiceTest {Testpublic void testGetGender(){UserService userService new UserService…