diff --git a/counter.js b/counter.js deleted file mode 100644 index 881e2d7..0000000 --- a/counter.js +++ /dev/null @@ -1,9 +0,0 @@ -export function setupCounter(element) { - let counter = 0 - const setCounter = (count) => { - counter = count - element.innerHTML = `count is ${counter}` - } - element.addEventListener('click', () => setCounter(counter + 1)) - setCounter(0) -} diff --git a/javascript.svg b/javascript.svg deleted file mode 100644 index f9abb2b..0000000 --- a/javascript.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/main.js b/main.js index b400b4e..a06801c 100644 --- a/main.js +++ b/main.js @@ -1,24 +1,116 @@ -import './style.css' -import javascriptLogo from './javascript.svg' -import viteLogo from '/vite.svg' -import { setupCounter } from './counter.js' +// 清理默认样式 +document.body.style.margin = 0 +document.body.style.padding = 0 +document.body.style.overflow = 'hidden' -document.querySelector('#app').innerHTML = ` -
- - - - - - -

Hello Vite!

-
- -
-

- Click on the Vite logo to learn more -

-
-` +// 创建一个画布 +const canvas = document.createElement('canvas') +document.body.appendChild(canvas) -setupCounter(document.querySelector('#counter')) +// 画布填满屏幕 +const ctx = canvas.getContext('2d') +const width = canvas.width = window.innerWidth +const height = canvas.height = window.innerHeight + +// 画布中心点 +const centerX = width / 2 +const centerY = height / 2 + +// 画布中心点的偏移量 +const offsetX = 0 +const offsetY = 0 + +// 准备一千个小圆点作为粒子 +const particles = [] +const particleCount = 1000 + +// 生成粒子 +for (let i = 0; i < particleCount; i++) { + particles.push({ + x: Math.random() * width, + y: Math.random() * height, + vx: Math.random() * 10 - 5, + vy: Math.random() * 10 - 5, + radius: Math.random() * 1 + 1 + }) +} + +// 绘制粒子 +function drawParticles () { + ctx.fillStyle = '#ff1414' + for (let i = 0; i < particleCount; i++) { + const particle = particles[i] + ctx.beginPath() + ctx.arc(particle.x, particle.y, particle.radius, 0, Math.PI * 2) + ctx.fill() + } +} + +// 绘制粒子的连线 +function drawParticlesLine () { + ctx.strokeStyle = '#ff1414' + for (let i = 0; i < particleCount; i++) { + const particle = particles[i] + for (let j = 0; j < particleCount; j++) { + const otherParticle = particles[j] + const dx = particle.x - otherParticle.x + const dy = particle.y - otherParticle.y + const dist = Math.sqrt(dx * dx + dy * dy) + if (dist < 100) { + ctx.beginPath() + ctx.moveTo(particle.x, particle.y) + ctx.lineTo(otherParticle.x, otherParticle.y) + ctx.stroke() + } + } + } +} + +// 粒子颜色为红色, 大小为 2px +function drawParticles2 () { + ctx.fillStyle = '#ff1414' + for (let i = 0; i < particleCount; i++) { + const particle = particles[i] + ctx.beginPath() + ctx.arc(particle.x, particle.y, 2, 0, Math.PI * 2) + ctx.fill() + } +} + +// 将粒子的位置更新到下一帧 +function updateParticles () { + for (let i = 0; i < particleCount; i++) { + const particle = particles[i] + particle.x += particle.vx + particle.y += particle.vy + if (particle.x < 0) { + particle.x = width + } + if (particle.x > width) { + particle.x = 0 + } + if (particle.y < 0) { + particle.y = height + } + if (particle.y > height) { + particle.y = 0 + } + } +} + +// 清空画布 +function clear () { + ctx.clearRect(0, 0, width, height) +} + +// 绘制 +function draw () { + clear() + drawParticles() + drawParticlesLine() + updateParticles() + requestAnimationFrame(draw) +} + +// 开始绘制 +draw() diff --git a/style.css b/style.css deleted file mode 100644 index abf9d15..0000000 --- a/style.css +++ /dev/null @@ -1,97 +0,0 @@ -:root { - font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif; - line-height: 1.5; - font-weight: 400; - - color-scheme: light dark; - color: rgba(255, 255, 255, 0.87); - background-color: #242424; - - font-synthesis: none; - text-rendering: optimizeLegibility; - -webkit-font-smoothing: antialiased; - -moz-osx-font-smoothing: grayscale; - -webkit-text-size-adjust: 100%; -} - -a { - font-weight: 500; - color: #646cff; - text-decoration: inherit; -} -a:hover { - color: #535bf2; -} - -body { - margin: 0; - display: flex; - place-items: center; - min-width: 320px; - min-height: 100vh; -} - -h1 { - font-size: 3.2em; - line-height: 1.1; -} - -#app { - max-width: 1280px; - margin: 0 auto; - padding: 2rem; - text-align: center; -} - -.logo { - height: 6em; - padding: 1.5em; - will-change: filter; - transition: filter 300ms; -} -.logo:hover { - filter: drop-shadow(0 0 2em #646cffaa); -} -.logo.vanilla:hover { - filter: drop-shadow(0 0 2em #f7df1eaa); -} - -.card { - padding: 2em; -} - -.read-the-docs { - color: #888; -} - -button { - border-radius: 8px; - border: 1px solid transparent; - padding: 0.6em 1.2em; - font-size: 1em; - font-weight: 500; - font-family: inherit; - background-color: #1a1a1a; - cursor: pointer; - transition: border-color 0.25s; -} -button:hover { - border-color: #646cff; -} -button:focus, -button:focus-visible { - outline: 4px auto -webkit-focus-ring-color; -} - -@media (prefers-color-scheme: light) { - :root { - color: #213547; - background-color: #ffffff; - } - a:hover { - color: #747bff; - } - button { - background-color: #f9f9f9; - } -}