前端Canvas:让你的网站更具视觉冲击力

前端Canvas:让你的网站更具视觉冲击力

毒舌时刻

前端Canvas?这不是游戏开发才用的吗?

"Canvas性能差,我不用"——结果错过了丰富的视觉效果,
"Canvas太复杂了,我学不会"——结果只能用静态图片,
"我用CSS就够了,要Canvas干嘛"——结果无法实现复杂的动画效果。

醒醒吧,Canvas不是游戏开发的专利,前端也可以用它来创建丰富的视觉效果!

为什么你需要这个?

  • 丰富的视觉效果:创建动态图形、动画和游戏
  • 高性能:直接操作像素,性能优异
  • 交互性:支持鼠标、触摸等交互
  • 数据可视化:绘制图表、仪表盘等
  • 跨平台:在所有现代浏览器中运行

反面教材

// 反面教材:简单的Canvas绘制 function drawCircle() { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 绘制一个简单的圆形 ctx.beginPath(); ctx.arc(100, 100, 50, 0, Math.PI * 2); ctx.fillStyle = 'red'; ctx.fill(); ctx.stroke(); } // 反面教材:没有优化的动画 function animate() { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); let x = 0; setInterval(() => { // 每次都清除整个画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制移动的矩形 ctx.fillStyle = 'blue'; ctx.fillRect(x, 50, 50, 50); x += 1; if (x > canvas.width) { x = 0; } }, 16); } 

正确的做法

// 正确的做法:Canvas基础绘制 function basicDrawing() { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 设置画布尺寸 canvas.width = 800; canvas.height = 600; // 绘制矩形 ctx.fillStyle = 'blue'; ctx.fillRect(50, 50, 100, 100); // 绘制圆形 ctx.beginPath(); ctx.arc(250, 100, 50, 0, Math.PI * 2); ctx.fillStyle = 'red'; ctx.fill(); // 绘制路径 ctx.beginPath(); ctx.moveTo(350, 50); ctx.lineTo(450, 150); ctx.lineTo(350, 150); ctx.closePath(); ctx.strokeStyle = 'green'; ctx.lineWidth = 2; ctx.stroke(); // 绘制文本 ctx.font = '24px Arial'; ctx.fillStyle = 'black'; ctx.textAlign = 'center'; ctx.fillText('Canvas绘制示例', canvas.width / 2, 30); } // 正确的做法:Canvas动画 function canvasAnimation() { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); canvas.width = 800; canvas.height = 600; let x = 0; let y = canvas.height / 2; let dx = 2; let dy = 2; const radius = 20; function animate() { // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制移动的圆形 ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI * 2); ctx.fillStyle = 'purple'; ctx.fill(); // 边界检测 if (x + radius > canvas.width || x - radius < 0) { dx = -dx; } if (y + radius > canvas.height || y - radius < 0) { dy = -dy; } // 更新位置 x += dx; y += dy; // 使用requestAnimationFrame requestAnimationFrame(animate); } animate(); } // 正确的做法:Canvas交互 function canvasInteraction() { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); canvas.width = 800; canvas.height = 600; let isDrawing = false; let lastX = 0; let lastY = 0; // 鼠标按下事件 canvas.addEventListener('mousedown', (e) => { isDrawing = true; [lastX, lastY] = [e.offsetX, e.offsetY]; }); // 鼠标移动事件 canvas.addEventListener('mousemove', (e) => { if (!isDrawing) return; ctx.beginPath(); ctx.moveTo(lastX, lastY); ctx.lineTo(e.offsetX, e.offsetY); ctx.strokeStyle = 'black'; ctx.lineWidth = 2; ctx.stroke(); [lastX, lastY] = [e.offsetX, e.offsetY]; }); // 鼠标释放事件 canvas.addEventListener('mouseup', () => { isDrawing = false; }); // 鼠标离开事件 canvas.addEventListener('mouseout', () => { isDrawing = false; }); } // 正确的做法:Canvas数据可视化 function canvasChart() { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); canvas.width = 800; canvas.height = 600; // 数据 const data = [12, 19, 3, 5, 2, 3, 7, 11, 15, 18, 10, 6]; const labels = ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']; // 绘制坐标系 const padding = 50; const chartWidth = canvas.width - padding * 2; const chartHeight = canvas.height - padding * 2; // X轴 ctx.beginPath(); ctx.moveTo(padding, canvas.height - padding); ctx.lineTo(canvas.width - padding, canvas.height - padding); ctx.stroke(); // Y轴 ctx.beginPath(); ctx.moveTo(padding, padding); ctx.lineTo(padding, canvas.height - padding); ctx.stroke(); // 绘制数据 const maxValue = Math.max(...data); const barWidth = chartWidth / data.length; data.forEach((value, index) => { const barHeight = (value / maxValue) * chartHeight; const x = padding + index * barWidth; const y = canvas.height - padding - barHeight; // 绘制柱子 ctx.fillStyle = 'rgba(54, 162, 235, 0.6)'; ctx.fillRect(x + 5, y, barWidth - 10, barHeight); // 绘制标签 ctx.font = '12px Arial'; ctx.fillStyle = 'black'; ctx.textAlign = 'center'; ctx.fillText(labels[index], x + barWidth / 2, canvas.height - padding + 15); }); // 绘制标题 ctx.font = '20px Arial'; ctx.fillStyle = 'black'; ctx.textAlign = 'center'; ctx.fillText('月度销售数据', canvas.width / 2, padding / 2); } // 正确的做法:Canvas图像处理 function canvasImageProcessing() { const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); canvas.width = 800; canvas.height = 600; // 加载图像 const img = new Image(); img.crossOrigin = 'anonymous'; img.onload = () => { // 绘制原始图像 ctx.drawImage(img, 0, 0, 400, 300); // 获取图像数据 const imageData = ctx.getImageData(0, 0, 400, 300); const data = imageData.data; // 处理图像(灰度) for (let i = 0; i < data.length; i += 4) { const r = data[i]; const g = data[i + 1]; const b = data[i + 2]; const gray = (r + g + b) / 3; data[i] = gray; data[i + 1] = gray; data[i + 2] = gray; } // 绘制处理后的图像 ctx.putImageData(imageData, 400, 0); }; img.src = 'https://picsum.photos/800/600'; } 

毒舌点评

看看,这才叫前端Canvas!不是简单地绘制几个图形,而是创建丰富的视觉效果、动画和交互。

记住,Canvas是一个强大的工具,它可以让你创建从简单的图表到复杂的游戏等各种视觉效果。合理使用Canvas,可以让你的网站更具视觉冲击力。

所以,别再觉得Canvas复杂了,它是前端视觉效果的重要工具!

总结

  • 基础绘制:矩形、圆形、路径、文本等
  • 动画效果:使用requestAnimationFrame实现流畅动画
  • 交互操作:鼠标、触摸等事件处理
  • 数据可视化:绘制图表、仪表盘等
  • 图像处理:像素级操作,实现滤镜效果
  • 性能优化:合理使用缓存、避免频繁重绘
  • 响应式:根据容器大小调整画布尺寸
  • 工具库:使用Chart.js、Fabric.js等库简化开发

前端Canvas,让你的网站更具视觉冲击力!

Read more

OpenClaw启动后,web控制面板无法登录,返回信息:Not Found

OpenClaw启动后,web控制面板无法登录,返回信息:Not Found

在1.19这台服务器安装了OpenClaw,不管用浏览器,还是直接使用curl,都是返回信息:Not Found 但是1.12这台服务器就没有问题... curl http://localhost:18789 Not Found 查看绑定情况 openclaw config get gateway.bind 🦞 OpenClaw 2026.3.2 (85377a2) — I'm not saying your workflow is chaotic... I'm just bringing a linter and a helmet. loopback 绑定到lan 打开网络连通 openclaw config

不用AList也能挂载115网盘?飞牛NAS原生WebDAV配置全攻略

飞牛NAS原生WebDAV直连115网盘全流程解析 在私有云存储领域,飞牛NAS凭借其简洁易用的特性赢得了不少用户的青睐。对于拥有115网盘资源的用户来说,如何在不依赖第三方工具的情况下实现高效挂载,成为提升使用体验的关键。本文将深入探讨飞牛NAS原生支持WebDAV协议挂载115网盘的全套方案,从原理分析到实操细节,帮助用户构建更稳定的私有云存储架构。 1. WebDAV协议与飞牛NAS的兼容性解析 WebDAV(Web Distributed Authoring and Versioning)作为一种基于HTTP/HTTPS的扩展协议,早已成为跨平台文件管理的通用标准。飞牛NAS在系统层面原生集成WebDAV服务,这为直接挂载各类云存储提供了技术基础。相比需要通过AList等第三方工具中转的方案,原生WebDAV连接具有明显的优势: * 性能提升:省去中间层处理,传输效率提高30%以上 * 稳定性增强:减少因第三方服务更新导致的兼容性问题 * 资源占用降低:无需额外安装维护应用,节省系统资源 在实际测试中,原生WebDAV挂载的响应速度比AList方案快1.5-2

从入门到精通:Ghostty-config配置面板完全指南

从入门到精通:Ghostty-config配置面板完全指南 【免费下载链接】ghostty-configA beautiful config generator for Ghostty terminal. 项目地址: https://gitcode.com/gh_mirrors/gh/ghostty-config Ghostty-config是一款美观直观的配置生成器,专为Ghostty终端设计,让自定义终端变得轻松简单。无需手动编辑文本文件,通过可视化界面即可调整设置、实时预览效果并导出配置文件。本文将带你全面了解如何使用这个强大工具打造个性化的终端体验。 快速开始:安装与基本设置 要开始使用Ghostty-config,首先需要克隆项目仓库: git clone https://gitcode.com/gh_mirrors/gh/ghostty-config 项目采用现代化的Svelte框架构建,主要配置逻辑集中在src/lib/data/settings.ts文件中。配置面板提供了丰富的设置选项,涵盖应用程序、剪贴板、窗口、颜色、字体、

前端权限管理实现:别让用户看到不该看的东西!

前端权限管理实现:别让用户看到不该看的东西! 毒舌时刻 权限管理?听起来就像是前端工程师为了显得自己很专业而特意搞的一套复杂流程。你以为随便加个if语句就能实现权限管理?别做梦了!到时候你会发现,权限逻辑分散在各个组件中,难以维护。 你以为前端权限管理就是最终的安全保障?别天真了!前端权限管理只是为了提高用户体验,真正的安全保障在后端。还有那些所谓的权限管理库,看起来高大上,用起来却各种问题。 为什么你需要这个 1. 用户体验:良好的权限管理可以为不同角色的用户提供不同的界面,提高用户体验。 2. 安全性:前端权限管理可以防止用户访问不该访问的功能,提高应用的安全性。 3. 代码组织:集中的权限管理可以使代码结构更清晰,便于维护。 4. 可扩展性:良好的权限管理设计可以方便地添加新的角色和权限。 5. 合规性:某些行业和地区要求应用必须实现严格的权限控制。 反面教材 // 1. 分散的权限逻辑 function AdminPanel() { const user = useUser(); if (user.role !== 'admin'