前端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

前端防范 XSS(跨站脚本攻击)

目录 一、防范措施 1.layui util  核心转义的特殊字符 示例 2.js-xss.js库 安装 1. Node.js 环境(npm/yarn) 2. 浏览器环境 核心 API 基础使用 1. 基础过滤(默认规则) 2. 自定义过滤规则 (1)允许特定标签 (2)允许特定属性 (3)自定义标签处理 (4)自定义属性处理 (5)转义特定字符 常见场景示例 1. 过滤用户输入的评论内容 2. 允许特定富文本标签(如富文本编辑器内容) 注意事项 更多配置 XSS(跨站脚本攻击)是一种常见的网络攻击手段,它允许攻击者将恶意脚本注入到其他用户的浏览器中。

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

详细教程:如何从前端查看调用接口、传参及返回结果(附带图片案例)

目录 1. 打开浏览器开发者工具 2. 使用 Network 面板 3. 查看具体的API请求 a. Headers b. Payload c. Response d. Preview e. Timing 4. 实际操作步骤 5. 常见问题及解决方法 a. 无法看到API请求 b. 请求失败 c. 跨域问题(CORS) 作为一名后端工程师,理解前端如何调用接口、传递参数以及接收返回值是非常重要的。下面将详细介绍如何通过浏览器开发者工具(F12)查看和分析这些信息,并附带图片案例帮助你更好地理解。 1. 打开浏览器开发者工具 按下 F12 或右键点击页面选择“检查”可以打开浏览器的开发者工具。常用的浏览器如Chrome、Firefox等都内置了开发者工具。下面是我选择我的一篇文章,打开开发者工具进行演示。 2. 使用

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例)

Cursor+Codex隐藏技巧:用截图秒修前端Bug的保姆级教程(React/Chakra UI案例) 前端开发中最令人头疼的莫过于那些难以定位的UI问题——元素错位、样式冲突、响应式失效...传统调试方式往往需要反复修改代码、刷新页面、检查元素。现在,通过Cursor编辑器集成的Codex功能,你可以直接用截图交互快速定位和修复这些问题。本文将带你从零开始,掌握这套革命性的调试工作流。 1. 环境准备与基础配置 在开始之前,确保你已经具备以下环境: * Cursor编辑器最新版(v2.5+) * Node.js 18.x及以上版本 * React 18项目(本文以Chakra UI 2.x为例) 首先在Cursor中安装Codex插件: 1. 点击左侧扩展图标 2. 搜索"Codex"并安装 3. 登录你的OpenAI账户(需要ChatGPT Plus订阅) 关键配置项: // 在项目根目录创建.