gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
使用html5 canvas写出海葵生长特效
- User
使用html5 canvas写出海葵生长特效
- Assistant
以下是我为您编写的海葵生长特效的HTML5 Canvas示例代码。您可以将其放入HTML文件中并在浏览器中查看。请注意,代码中的绘画形状和颜色仅供参考,您可以根据需要进行更改。
<!DOCTYPE html> <html> <head> <title>海葵生长特效</title> <style> body { margin: 0; padding: 0; overflow: hidden; } </style> </head> <body> <canvas id="canvas"></canvas> <script> const canvas = document.querySelector("#canvas"); const ctx = canvas.getContext("2d"); canvas.width = window.innerWidth; canvas.height = window.innerHeight; const aneLength = 200; // 海葵的长度 let aneOffset = 0; // 海葵偏移量 let startAngle = 0; // 控制海葵摆动幅度和角度 function draw() { ctx.clearRect(0, 0, canvas.width, canvas.height); // 清空画布 startAngle += 0.02; for (let i = 0; i < aneLength; i++) { // 绘制海葵茎的路径 ctx.beginPath(); ctx.moveTo(i * 16 + aneOffset, canvas.height); ctx.quadraticCurveTo( i * 16 + aneOffset + Math.sin(startAngle * i) * 20, canvas.height - 150, i * 16 + aneOffset + Math.sin(startAngle * (i + 1)) * 15, canvas.height - 300 ); ctx.strokeStyle = "#32a852"; // 海葵茎的颜色 ctx.lineWidth = 20; // 海葵茎的宽度 ctx.lineCap = "round"; ctx.stroke(); } aneOffset += 0.3; // 海葵偏移速度 requestAnimationFrame(draw); // 不断重绘画布 } draw(); </script> </body> </html> - User
使用html5 canvas写出海葵生长背景特效
- Assistant
以下是我为您编写的海葵生长背景特效的HTML5 Canvas示例代码。您可以将其放入HTML文件中并在浏览器中查看。请注意,代码中的绘画形状和颜色仅供参考,您可以根据需要进行更改。
<!DOCTYPE html> <html> <head> <title>海葵生长背景特效</title> <style> body { margin: 0; padding: 0; overflow: hidden; } canvas { position: absolute; top: 0; left: 0; } </style> </head> <body> <canvas id="canvas"></canvas> <script> const canvas = document.querySelector("#canvas"); const ctx = canvas.getContext("2d"); canvas.width = window.innerWidth; canvas.height = window.innerHeight; const aneLength = 200; // 海葵的长度 let aneOffset = 0; // 海葵偏移量 let startAngle = 0; // 控制海葵摆动幅度和角度 const bgGradient = ctx.createLinearGradient(0, 0, 0, canvas.height); // 背景渐变色 bgGradient.addColorStop(0, "#16222a"); bgGradient.addColorStop(1, "#3a6073"); function draw() { // 绘制背景渐变色 ctx.fillStyle = bgGradient; ctx.fillRect(0, 0, canvas.width, canvas.height); startAngle += 0.02; for (let i = 0; i < aneLength; i++) { // 绘制海葵茎的路径 ctx.beginPath(); ctx.moveTo(i * 16 + aneOffset, canvas.height); ctx.quadraticCurveTo( i * 16 + aneOffset + Math.sin(startAngle * i) * 20, canvas.height - 150, i * 16 + aneOffset + Math.sin(startAngle * (i + 1)) * 15, canvas.height - 300 ); ctx.strokeStyle = "#32a852"; // 海葵茎的颜色 ctx.lineWidth = 20; // 海葵茎的宽度 ctx.lineCap = "round"; ctx.stroke(); // 绘制海葵茎上的果实 ctx.beginPath(); ctx.arc( i * 16 + aneOffset + Math.sin(startAngle * i) * 20, canvas.height - 150, 10, 0, Math.PI * 2 ); ctx.fillStyle = "#ff7f50"; // 果实颜色 ctx.fill(); } aneOffset += 0.3; // 海葵偏移速度 requestAnimationFrame(draw); // 不断重绘画布 } draw(); </script> </body> </html>