canvas实现支持中文的自定义文字颗粒化粒子散落一地动画效果代码
代码语言:html
所属分类:粒子
代码描述:canvas实现支持中文的自定义文字颗粒化粒子散落一地动画效果代码
代码标签: canvas 支持 中文 自定义 文字 颗粒化 粒子 散落 一地 动画
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="en" > <head> <meta charset="UTF-8"> <link rel='stylesheet' href='https://fonts.googleapis.com/css2?family=Paytone+One&display=swap'> <style> :root { --foreground: #f5ff6e; --foreground-hover: #f8ff99; --background: #103b82; --background-lgt: #2561c4; } html * { box-sizing: border-box; } body { padding: 0; margin: 0; } body { background-color: var(--background); color: var(--foreground); position: relative; overflow: hidden; } button#restart { position: absolute; top: 1rem; right: 1rem; z-index: 3; padding: 0.75rem 1.5rem; border: none; background-color: var(--foreground); color: #000; border-radius: 0.25rem; cursor: pointer; } #input-container { position: absolute; left: 50%; top: 1rem; transform: translate3d(-50%, 0, 0); display: flex; align-items: center; gap: 0.5rem; } #input-container input { background-color: rgb(255 255 255 / 0.25); border: 3px solid var(--background-lgt); border-radius: 0.25rem; color: #fff; font-size: 1.25rem; } #select-word { /* font-size: 1.25rem; */ background-color: var(--foreground); border: none; border-radius: 0.25rem; padding: 0.5rem 1.5rem; } button#restart:hover, #select-word:hover { background-color: var(--foreground-hover); } canvas#oof-canvas { position: absolute; left: 0; right: 0; width: 100vw; height: 100vh; /* outline: 1px solid lime; */ z-index: 2; pointer-events: none; } </style> </head> <body translate="no"> <div id="input-container"> <input id="word" type="text" value="BFW" /> <button id="select-word">Clumsy me!</button> </div> <!-- <p>Oof</a> --> <!-- inspired by Edward Ruscha's OOF https://www.moma.org/collection/works/79298 --> <canvas id="oof-canvas"></canvas> <!-- <button id="restart">Restart</button> --> <script> console.clear(); const canvas = document.querySelector("#oof-canvas"); const ctx = canvas.getContext("2d"); const restartBtn = document.querySelector("#restart"); const wordInput = document.querySelector("#word"); const wordSubmit = document.querySelector("#select-word"); resizeCanvas(); const text = { value: "BFW", color: "245 255 110", fontFamily: `"Paytone One", sans-serif`, size: 20, opacity: 1 }; const textSize = { width: null, height: null }; let particleArr = []; let textCoordinates; let gravity = 0; let gravityFlag = false; let isAnimating = false; class Particle { constructor({ position }) { this.position = position; this.velocity = { x: 0, y: 0 }; this.size = 2; } update() { this.velocity.y += gravity; this.position.x += this.velocity.x; this.position.y += this.velocity.y; if (this.position.y >= canvas.height) { this.velocity.y = this.velocity.y * -(Math.random() * 0.4 + 0.2); } this.draw(); } draw() { ctx.beginPath(); ctx.arc(this.position.x, this.position.y, this.size, 0, Math.PI * 2); ctx.fillStyle = `rg.........完整代码请登录后点击上方下载按钮下载查看
网友评论0