|
1 |
| -let start = new Date().getTime(); |
| 1 | +const canvas = document.createElement("canvas"); |
| 2 | +document.body.appendChild(canvas); |
2 | 3 |
|
3 |
| -const originPosition = { x: 0, y: 0 }; |
| 4 | +const ctx = canvas.getContext("2d"); |
4 | 5 |
|
5 |
| -const last = { |
6 |
| - starTimestamp: start, |
7 |
| - starPosition: originPosition, |
8 |
| - mousePosition: originPosition |
9 |
| -}; |
| 6 | +const width = window.innerWidth; |
| 7 | +const height = window.innerHeight; |
10 | 8 |
|
11 |
| -const config = { |
12 |
| - // ... other configuration options (unchanged) |
13 |
| -}; |
| 9 | +canvas.width = width; |
| 10 | +canvas.height = height; |
14 | 11 |
|
15 |
| -let count = 0; |
| 12 | +let points = []; |
16 | 13 |
|
17 |
| -const rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min, |
18 |
| - selectRandom = items => items[rand(0, items.length - 1)]; |
| 14 | +window.addEventListener("mousemove", (event) => { |
| 15 | + const x = event.clientX; |
| 16 | + const y = event.clientY; |
19 | 17 |
|
20 |
| -const withUnit = (value, unit) => `${value}${unit}`, |
21 |
| - px = value => withUnit(value, "px"), |
22 |
| - ms = value => withUnit(value, "ms"); |
| 18 | + // Add a new point to the trail |
| 19 | + points.push({ x, y }); |
23 | 20 |
|
24 |
| -const calcDistance = (a, b) => { |
25 |
| - const diffX = b.x - a.x, |
26 |
| - diffY = b.y - a.y; |
| 21 | + // Limit the number of points for performance |
| 22 | + points = points.slice(-20); |
27 | 23 |
|
28 |
| - return Math.sqrt(Math.pow(diffX, 2) + Math.pow(diffY, 2)); |
29 |
| -}; |
| 24 | + ctx.clearRect(0, 0, width, height); |
30 | 25 |
|
31 |
| -const calcElapsedTime = (start, end) => end - start; |
| 26 | + // Draw the trail |
| 27 | + for (let i = 0; i < points.length - 1; i++) { |
| 28 | + const current = points[i]; |
| 29 | + const next = points[i + 1]; |
32 | 30 |
|
33 |
| -const appendElement = element => document.body.appendChild(element), |
34 |
| - removeElement = (element, delay) => setTimeout(() => document.body.removeChild(element), delay); |
35 |
| - |
36 |
| -const createStar = position => { |
37 |
| - const star = document.createElement("span"); |
38 |
| - |
39 |
| - star.className = "star"; // Remove "fa-solid fa-sparkle" class for emoji |
40 |
| - star.textContent = "⭐"; // Set the content to the star emoji |
41 |
| - |
42 |
| - star.style.left = px(position.x); |
43 |
| - star.style.top = px(position.y); |
44 |
| - star.style.fontSize = selectRandom(config.sizes); |
45 |
| - star.style.color = `rgb(${selectRandom(config.colors)})`; |
46 |
| - star.style.textShadow = `0px 0px 1.5rem rgb(${selectRandom(config.colors)} / 0.5)`; |
47 |
| - star.style.animationName = config.animations[count++ % 3]; |
48 |
| - star.style.animationDuration = ms(config.starAnimationDuration); |
49 |
| - |
50 |
| - appendElement(star); |
51 |
| - |
52 |
| - removeElement(star, config.starAnimationDuration); |
53 |
| -}; |
54 |
| - |
55 |
| -const handleOnMove = e => { |
56 |
| - const mousePosition = { x: e.clientX, y: e.clientY }; |
57 |
| - |
58 |
| - // ... existing code for trail creation (unchanged) |
59 |
| - |
60 |
| - if (calcElapsedTime(last.starTimestamp, new Date().getTime()) > config.minimumTimeBetweenStars) { |
61 |
| - createStar(mousePosition); |
62 |
| - last.starTimestamp = new Date().getTime(); |
| 31 | + ctx.beginPath(); |
| 32 | + ctx.moveTo(current.x, current.y); |
| 33 | + ctx.lineTo(next.x, next.y); |
| 34 | + ctx.strokeStyle = "white"; // Trail color |
| 35 | + ctx.lineWidth = 2; // Trail thickness |
| 36 | + ctx.stroke(); |
63 | 37 | }
|
64 | 38 |
|
65 |
| - last.mousePosition = mousePosition; |
66 |
| -}; |
67 |
| - |
68 |
| -window.onmousemove = e => handleOnMove(e); |
69 |
| - |
70 |
| -window.ontouchmove = e => handleOnMove(e.touches[0]); |
71 |
| - |
72 |
| -document.body.onmouseleave = () => last.mousePosition = originPosition; |
| 39 | + // Draw the star |
| 40 | + ctx.beginPath(); |
| 41 | + ctx.arc(x, y, 5, 0, Math.PI * 2); // Star size and shape |
| 42 | + ctx.fillStyle = "yellow"; // Star color |
| 43 | + ctx.fill(); |
| 44 | +}); |
0 commit comments