bits-lab 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/dist/Accordion.d.ts +52 -0
- package/dist/Accordion.js +192 -0
- package/dist/AchievementToast.d.ts +36 -0
- package/dist/AchievementToast.js +162 -0
- package/dist/AgentCard.d.ts +41 -0
- package/dist/AgentCard.js +168 -0
- package/dist/ArcadeButtons.d.ts +23 -0
- package/dist/ArcadeButtons.js +169 -0
- package/dist/AuroraBands.d.ts +19 -0
- package/dist/AuroraBands.js +126 -0
- package/dist/AvatarTooltips.d.ts +32 -0
- package/dist/AvatarTooltips.js +121 -0
- package/dist/BarRace.d.ts +39 -0
- package/dist/BarRace.js +227 -0
- package/dist/BentoGrid.d.ts +50 -0
- package/dist/BentoGrid.js +145 -0
- package/dist/BootSequence.d.ts +48 -0
- package/dist/BootSequence.js +234 -0
- package/dist/BreadcrumbTrail.d.ts +31 -0
- package/dist/BreadcrumbTrail.js +181 -0
- package/dist/CRTPower.d.ts +30 -0
- package/dist/CRTPower.js +142 -0
- package/dist/CardDeck.d.ts +29 -0
- package/dist/CardDeck.js +154 -0
- package/dist/ChangelogTimeline.d.ts +35 -0
- package/dist/ChangelogTimeline.js +137 -0
- package/dist/ChapterProgress.d.ts +23 -0
- package/dist/ChapterProgress.js +211 -0
- package/dist/ChatBubbles.d.ts +42 -0
- package/dist/ChatBubbles.js +198 -0
- package/dist/ChiptuneKit.d.ts +158 -0
- package/dist/ChiptuneKit.js +207 -0
- package/dist/CipherCard.d.ts +22 -0
- package/dist/CipherCard.js +91 -0
- package/dist/CloudSky.d.ts +28 -0
- package/dist/CloudSky.js +222 -0
- package/dist/CodeRain.d.ts +24 -0
- package/dist/CodeRain.js +158 -0
- package/dist/CodeShowcase.d.ts +50 -0
- package/dist/CodeShowcase.js +307 -0
- package/dist/ColorDial.d.ts +42 -0
- package/dist/ColorDial.js +288 -0
- package/dist/CommandPalette.d.ts +59 -0
- package/dist/CommandPalette.js +349 -0
- package/dist/CompareSlider.d.ts +33 -0
- package/dist/CompareSlider.js +152 -0
- package/dist/Confetti.d.ts +61 -0
- package/dist/Confetti.js +176 -0
- package/dist/ConnectBeams.d.ts +39 -0
- package/dist/ConnectBeams.js +132 -0
- package/dist/ContextOrb.d.ts +34 -0
- package/dist/ContextOrb.js +269 -0
- package/dist/ContourLines.d.ts +20 -0
- package/dist/ContourLines.js +159 -0
- package/dist/Coverflow.d.ts +27 -0
- package/dist/Coverflow.js +167 -0
- package/dist/CursorAttractor.d.ts +21 -0
- package/dist/CursorAttractor.js +136 -0
- package/dist/CutReveal.d.ts +21 -0
- package/dist/CutReveal.js +116 -0
- package/dist/DateRange.d.ts +45 -0
- package/dist/DateRange.js +327 -0
- package/dist/DeviceFrame.d.ts +30 -0
- package/dist/DeviceFrame.js +145 -0
- package/dist/DiffReveal.d.ts +36 -0
- package/dist/DiffReveal.js +247 -0
- package/dist/DirectionHover.d.ts +15 -0
- package/dist/DirectionHover.js +90 -0
- package/dist/Discard.d.ts +18 -0
- package/dist/Discard.js +61 -0
- package/dist/DitherField.d.ts +15 -0
- package/dist/DitherField.js +157 -0
- package/dist/DotGlobe.d.ts +48 -0
- package/dist/DotGlobe.js +312 -0
- package/dist/DotMap.d.ts +47 -0
- package/dist/DotMap.js +172 -0
- package/dist/DropZone.d.ts +57 -0
- package/dist/DropZone.js +304 -0
- package/dist/DynamicIsland.d.ts +32 -0
- package/dist/DynamicIsland.js +92 -0
- package/dist/EdgeFX.d.ts +27 -0
- package/dist/EdgeFX.js +98 -0
- package/dist/ElasticString.d.ts +22 -0
- package/dist/ElasticString.js +127 -0
- package/dist/ExpandCard.d.ts +44 -0
- package/dist/ExpandCard.js +182 -0
- package/dist/FeatureTabs.d.ts +34 -0
- package/dist/FeatureTabs.js +169 -0
- package/dist/FeedList.d.ts +37 -0
- package/dist/FeedList.js +163 -0
- package/dist/FlickerGrid.d.ts +29 -0
- package/dist/FlickerGrid.js +156 -0
- package/dist/FocusGrid.d.ts +20 -0
- package/dist/FocusGrid.js +48 -0
- package/dist/FocusPull.d.ts +26 -0
- package/dist/FocusPull.js +106 -0
- package/dist/Footprints.d.ts +24 -0
- package/dist/Footprints.js +126 -0
- package/dist/FunnelFlow.d.ts +34 -0
- package/dist/FunnelFlow.js +267 -0
- package/dist/GameOver.d.ts +46 -0
- package/dist/GameOver.js +191 -0
- package/dist/GaugeMeter.d.ts +38 -0
- package/dist/GaugeMeter.js +195 -0
- package/dist/GlassPanel.d.ts +27 -0
- package/dist/GlassPanel.js +80 -0
- package/dist/GrainFX.d.ts +34 -0
- package/dist/GrainFX.js +122 -0
- package/dist/GravityBox.d.ts +30 -0
- package/dist/GravityBox.js +220 -0
- package/dist/HealthBar.d.ts +33 -0
- package/dist/HealthBar.js +140 -0
- package/dist/Heatmap.d.ts +42 -0
- package/dist/Heatmap.js +232 -0
- package/dist/HexField.d.ts +29 -0
- package/dist/HexField.js +188 -0
- package/dist/HoldToConfirm.d.ts +29 -0
- package/dist/HoldToConfirm.js +163 -0
- package/dist/HorizontalTrack.d.ts +28 -0
- package/dist/HorizontalTrack.js +195 -0
- package/dist/HotbarDock.d.ts +40 -0
- package/dist/HotbarDock.js +148 -0
- package/dist/InkText.d.ts +27 -0
- package/dist/InkText.js +65 -0
- package/dist/InlineEdit.d.ts +30 -0
- package/dist/InlineEdit.js +198 -0
- package/dist/InteractiveGrid.d.ts +25 -0
- package/dist/InteractiveGrid.js +194 -0
- package/dist/InventoryGrid.d.ts +45 -0
- package/dist/InventoryGrid.js +272 -0
- package/dist/JellyBlob.d.ts +30 -0
- package/dist/JellyBlob.js +209 -0
- package/dist/KineticHeadline.d.ts +30 -0
- package/dist/KineticHeadline.js +133 -0
- package/dist/LavaLamp.d.ts +21 -0
- package/dist/LavaLamp.js +133 -0
- package/dist/LetterSwap.d.ts +20 -0
- package/dist/LetterSwap.js +78 -0
- package/dist/LevelMap.d.ts +35 -0
- package/dist/LevelMap.js +189 -0
- package/dist/LightShafts.d.ts +22 -0
- package/dist/LightShafts.js +170 -0
- package/dist/LightboxGallery.d.ts +26 -0
- package/dist/LightboxGallery.js +327 -0
- package/dist/LinkPeek.d.ts +36 -0
- package/dist/LinkPeek.js +194 -0
- package/dist/LogoOrbit.d.ts +37 -0
- package/dist/LogoOrbit.js +117 -0
- package/dist/Marquee.d.ts +33 -0
- package/dist/Marquee.js +148 -0
- package/dist/MarqueeLine.d.ts +33 -0
- package/dist/MarqueeLine.js +167 -0
- package/dist/MasonryWall.d.ts +13 -0
- package/dist/MasonryWall.js +90 -0
- package/dist/MegaMenu.d.ts +32 -0
- package/dist/MegaMenu.js +280 -0
- package/dist/MeteorShower.d.ts +24 -0
- package/dist/MeteorShower.js +155 -0
- package/dist/Minimap.d.ts +21 -0
- package/dist/Minimap.js +236 -0
- package/dist/MobileTabBar.d.ts +38 -0
- package/dist/MobileTabBar.js +148 -0
- package/dist/MorphDialog.d.ts +34 -0
- package/dist/MorphDialog.js +199 -0
- package/dist/MorphPopover.d.ts +33 -0
- package/dist/MorphPopover.js +168 -0
- package/dist/MorphWords.d.ts +18 -0
- package/dist/MorphWords.js +136 -0
- package/dist/OTPInput.d.ts +43 -0
- package/dist/OTPInput.js +174 -0
- package/dist/OverlayKit.d.ts +97 -0
- package/dist/OverlayKit.js +466 -0
- package/dist/OverlayMenu.d.ts +32 -0
- package/dist/OverlayMenu.js +154 -0
- package/dist/ParallaxCollage.d.ts +41 -0
- package/dist/ParallaxCollage.js +143 -0
- package/dist/PasswordMeter.d.ts +44 -0
- package/dist/PasswordMeter.js +165 -0
- package/dist/PegBoard.d.ts +26 -0
- package/dist/PegBoard.js +184 -0
- package/dist/PinCard.d.ts +21 -0
- package/dist/PinCard.js +84 -0
- package/dist/PixelAvatar.d.ts +33 -0
- package/dist/PixelAvatar.js +144 -0
- package/dist/PixelButton.d.ts +20 -0
- package/dist/PixelButton.js +128 -0
- package/dist/PixelExplode.d.ts +31 -0
- package/dist/PixelExplode.js +164 -0
- package/dist/PixelLoader.d.ts +32 -0
- package/dist/PixelLoader.js +142 -0
- package/dist/PixelParallax.d.ts +41 -0
- package/dist/PixelParallax.js +151 -0
- package/dist/PixelSpinners.d.ts +21 -0
- package/dist/PixelSpinners.js +110 -0
- package/dist/PixelType.d.ts +26 -0
- package/dist/PixelType.js +220 -0
- package/dist/PointerKit.d.ts +23 -0
- package/dist/PointerKit.js +222 -0
- package/dist/Polaroids.d.ts +41 -0
- package/dist/Polaroids.js +293 -0
- package/dist/PressStart.d.ts +28 -0
- package/dist/PressStart.js +99 -0
- package/dist/PricingFlip.d.ts +52 -0
- package/dist/PricingFlip.js +190 -0
- package/dist/ProximityType.d.ts +26 -0
- package/dist/ProximityType.js +135 -0
- package/dist/QuestDialog.d.ts +50 -0
- package/dist/QuestDialog.js +204 -0
- package/dist/QuestLog.d.ts +39 -0
- package/dist/QuestLog.js +214 -0
- package/dist/RangeDual.d.ts +34 -0
- package/dist/RangeDual.js +238 -0
- package/dist/RatingPips.d.ts +25 -0
- package/dist/RatingPips.js +129 -0
- package/dist/RetroHorizon.d.ts +27 -0
- package/dist/RetroHorizon.js +169 -0
- package/dist/Reveal.d.ts +30 -0
- package/dist/Reveal.js +92 -0
- package/dist/RingProgress.d.ts +45 -0
- package/dist/RingProgress.js +180 -0
- package/dist/RollingNumber.d.ts +28 -0
- package/dist/RollingNumber.js +108 -0
- package/dist/ScanScope.d.ts +24 -0
- package/dist/ScanScope.js +164 -0
- package/dist/ScoreTicker.d.ts +32 -0
- package/dist/ScoreTicker.js +176 -0
- package/dist/ScreenScroller.d.ts +24 -0
- package/dist/ScreenScroller.js +138 -0
- package/dist/Screensaver.d.ts +31 -0
- package/dist/Screensaver.js +221 -0
- package/dist/ScrollTilt.d.ts +27 -0
- package/dist/ScrollTilt.js +100 -0
- package/dist/ScrollTypography.d.ts +26 -0
- package/dist/ScrollTypography.js +124 -0
- package/dist/ScrollZoom.d.ts +31 -0
- package/dist/ScrollZoom.js +123 -0
- package/dist/ScrubNumber.d.ts +29 -0
- package/dist/ScrubNumber.js +152 -0
- package/dist/ShadowStack.d.ts +28 -0
- package/dist/ShadowStack.js +116 -0
- package/dist/SideRail.d.ts +43 -0
- package/dist/SideRail.js +253 -0
- package/dist/SignalText.d.ts +25 -0
- package/dist/SignalText.js +178 -0
- package/dist/SkeletonShimmer.d.ts +18 -0
- package/dist/SkeletonShimmer.js +111 -0
- package/dist/SketchHighlight.d.ts +27 -0
- package/dist/SketchHighlight.js +180 -0
- package/dist/SlideTabs.d.ts +40 -0
- package/dist/SlideTabs.js +190 -0
- package/dist/SmartNavbar.d.ts +48 -0
- package/dist/SmartNavbar.js +188 -0
- package/dist/SparkChart.d.ts +41 -0
- package/dist/SparkChart.js +188 -0
- package/dist/Spoiler.d.ts +22 -0
- package/dist/Spoiler.js +97 -0
- package/dist/SpotlightTour.d.ts +38 -0
- package/dist/SpotlightTour.js +231 -0
- package/dist/SpringDrawer.d.ts +39 -0
- package/dist/SpringDrawer.js +360 -0
- package/dist/SpriteAnimator.d.ts +41 -0
- package/dist/SpriteAnimator.js +124 -0
- package/dist/StatCard.d.ts +43 -0
- package/dist/StatCard.js +168 -0
- package/dist/StepLoader.d.ts +41 -0
- package/dist/StepLoader.js +191 -0
- package/dist/StepperForm.d.ts +41 -0
- package/dist/StepperForm.js +206 -0
- package/dist/StickerBoard.d.ts +39 -0
- package/dist/StickerBoard.js +209 -0
- package/dist/StickyReveal.d.ts +42 -0
- package/dist/StickyReveal.js +181 -0
- package/dist/StoryViewer.d.ts +36 -0
- package/dist/StoryViewer.js +175 -0
- package/dist/StreamingText.d.ts +29 -0
- package/dist/StreamingText.js +271 -0
- package/dist/SuggestionChips.d.ts +32 -0
- package/dist/SuggestionChips.js +96 -0
- package/dist/SwipeActions.d.ts +30 -0
- package/dist/SwipeActions.js +141 -0
- package/dist/TagInput.d.ts +30 -0
- package/dist/TagInput.js +284 -0
- package/dist/TallyUp.d.ts +38 -0
- package/dist/TallyUp.js +157 -0
- package/dist/TerminalDemo.d.ts +63 -0
- package/dist/TerminalDemo.js +214 -0
- package/dist/TestimonialWall.d.ts +35 -0
- package/dist/TestimonialWall.js +131 -0
- package/dist/TextOnPath.d.ts +36 -0
- package/dist/TextOnPath.js +130 -0
- package/dist/ThinkingShimmer.d.ts +36 -0
- package/dist/ThinkingShimmer.js +132 -0
- package/dist/ToastStack.d.ts +62 -0
- package/dist/ToastStack.js +289 -0
- package/dist/ToggleGroup.d.ts +39 -0
- package/dist/ToggleGroup.js +190 -0
- package/dist/ToggleKit.d.ts +70 -0
- package/dist/ToggleKit.js +229 -0
- package/dist/TracingLine.d.ts +24 -0
- package/dist/TracingLine.js +139 -0
- package/dist/TradingCard.d.ts +30 -0
- package/dist/TradingCard.js +147 -0
- package/dist/VanishInput.d.ts +34 -0
- package/dist/VanishInput.js +190 -0
- package/dist/VideoHero.d.ts +32 -0
- package/dist/VideoHero.js +207 -0
- package/dist/VoiceOrb.d.ts +23 -0
- package/dist/VoiceOrb.js +182 -0
- package/dist/VoxelTiles.d.ts +38 -0
- package/dist/VoxelTiles.js +201 -0
- package/dist/WarpField.d.ts +18 -0
- package/dist/WarpField.js +160 -0
- package/dist/WebcamPixels.d.ts +38 -0
- package/dist/WebcamPixels.js +292 -0
- package/dist/WheelPicker.d.ts +29 -0
- package/dist/WheelPicker.js +199 -0
- package/dist/WobbleCard.d.ts +23 -0
- package/dist/WobbleCard.js +112 -0
- package/dist/index.d.ts +158 -0
- package/dist/index.js +159 -0
- package/package.json +34 -4
package/dist/VoiceOrb.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useInsertionEffect, useRef } from "react";
|
|
4
|
+
const STYLE_ID = "bl-voice-orb";
|
|
5
|
+
const CSS = `
|
|
6
|
+
.bl-orb { position: relative; display: inline-grid; place-items: center; width: var(--bl-size); max-width: 100%; aspect-ratio: 1; }
|
|
7
|
+
.bl-orb canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
|
|
8
|
+
.bl-orb__sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
|
9
|
+
`;
|
|
10
|
+
function useStyles() {
|
|
11
|
+
useInsertionEffect(() => {
|
|
12
|
+
if (typeof document === "undefined") return;
|
|
13
|
+
const style = document.getElementById(STYLE_ID) ?? Object.assign(document.createElement("style"), { id: STYLE_ID });
|
|
14
|
+
style.textContent = CSS;
|
|
15
|
+
if (!style.isConnected) document.head.appendChild(style);
|
|
16
|
+
}, []);
|
|
17
|
+
}
|
|
18
|
+
const STATE_TEXT = { idle: "Idle", listening: "Listening", thinking: "Thinking", speaking: "Speaking" };
|
|
19
|
+
function simulatedLevel(state, t) {
|
|
20
|
+
if (state === "idle") return 0.05 + Math.sin(t * 1.4) * 0.03;
|
|
21
|
+
if (state === "thinking") return 0.08;
|
|
22
|
+
const gate = Math.sin(t * 0.9) + Math.sin(t * 2.3 + 1) > -0.2 ? 1 : 0.15;
|
|
23
|
+
const syllables = Math.abs(Math.sin(t * (state === "speaking" ? 9 : 6))) * 0.6 + Math.abs(Math.sin(t * 3.7 + 2)) * 0.4;
|
|
24
|
+
return gate * syllables * (state === "speaking" ? 0.95 : 0.7);
|
|
25
|
+
}
|
|
26
|
+
function VoiceOrb({
|
|
27
|
+
state = "idle",
|
|
28
|
+
source = "simulated",
|
|
29
|
+
level,
|
|
30
|
+
size = 220,
|
|
31
|
+
colors = ["#41a6f6", "#73eff7", "#b13e53"],
|
|
32
|
+
glowColor = "rgba(115,239,247,0.45)",
|
|
33
|
+
sensitivity = 1,
|
|
34
|
+
label = "Voice assistant",
|
|
35
|
+
onMicError,
|
|
36
|
+
className = "",
|
|
37
|
+
style
|
|
38
|
+
}) {
|
|
39
|
+
useStyles();
|
|
40
|
+
const wrapRef = useRef(null);
|
|
41
|
+
const canvasRef = useRef(null);
|
|
42
|
+
const live = useRef({ state, level, colors, glowColor, sensitivity });
|
|
43
|
+
live.current = { state, level, colors, glowColor, sensitivity };
|
|
44
|
+
const micLevel = useRef(null);
|
|
45
|
+
const onMicErrorRef = useRef(onMicError);
|
|
46
|
+
onMicErrorRef.current = onMicError;
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
if (source !== "mic" || state !== "listening" || !navigator.mediaDevices?.getUserMedia) return void 0;
|
|
49
|
+
let cancelled = false;
|
|
50
|
+
let stream;
|
|
51
|
+
let ctx;
|
|
52
|
+
let frame = 0;
|
|
53
|
+
navigator.mediaDevices.getUserMedia({ audio: true }).then((media) => {
|
|
54
|
+
if (cancelled) {
|
|
55
|
+
media.getTracks().forEach((track) => track.stop());
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
stream = media;
|
|
59
|
+
ctx = new AudioContext();
|
|
60
|
+
const analyser = ctx.createAnalyser();
|
|
61
|
+
analyser.fftSize = 512;
|
|
62
|
+
ctx.createMediaStreamSource(media).connect(analyser);
|
|
63
|
+
const data = new Uint8Array(analyser.fftSize);
|
|
64
|
+
const read = () => {
|
|
65
|
+
analyser.getByteTimeDomainData(data);
|
|
66
|
+
let sum = 0;
|
|
67
|
+
for (const v of data) sum += ((v - 128) / 128) ** 2;
|
|
68
|
+
micLevel.current = Math.min(1, Math.sqrt(sum / data.length) * 4);
|
|
69
|
+
frame = requestAnimationFrame(read);
|
|
70
|
+
};
|
|
71
|
+
read();
|
|
72
|
+
}).catch((error) => {
|
|
73
|
+
micLevel.current = null;
|
|
74
|
+
onMicErrorRef.current?.(error);
|
|
75
|
+
});
|
|
76
|
+
return () => {
|
|
77
|
+
cancelled = true;
|
|
78
|
+
cancelAnimationFrame(frame);
|
|
79
|
+
micLevel.current = null;
|
|
80
|
+
stream?.getTracks().forEach((track) => track.stop());
|
|
81
|
+
ctx?.close();
|
|
82
|
+
};
|
|
83
|
+
}, [source, state]);
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const wrap = wrapRef.current;
|
|
86
|
+
const canvas = canvasRef.current;
|
|
87
|
+
const ctx = canvas.getContext("2d");
|
|
88
|
+
const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
|
|
89
|
+
let width = 0;
|
|
90
|
+
let frame = 0;
|
|
91
|
+
let visible = true;
|
|
92
|
+
let smooth = 0;
|
|
93
|
+
let spinAngle = 0;
|
|
94
|
+
let last = performance.now();
|
|
95
|
+
const start = last;
|
|
96
|
+
const resize = () => {
|
|
97
|
+
const dpr = Math.min(2, window.devicePixelRatio || 1);
|
|
98
|
+
width = wrap.clientWidth;
|
|
99
|
+
canvas.width = Math.round(width * dpr);
|
|
100
|
+
canvas.height = Math.round(width * dpr);
|
|
101
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
102
|
+
};
|
|
103
|
+
const draw = (now) => {
|
|
104
|
+
frame = 0;
|
|
105
|
+
const dt = Math.min(0.05, (now - last) / 1e3);
|
|
106
|
+
last = now;
|
|
107
|
+
const t = (now - start) / 1e3;
|
|
108
|
+
const { state: s, level: external, colors: palette, glowColor: glow, sensitivity: gain } = live.current;
|
|
109
|
+
const target = Math.min(1, (external ?? micLevel.current ?? simulatedLevel(s, t)) * gain);
|
|
110
|
+
smooth += (target - smooth) * (target > smooth ? 0.35 : 0.08);
|
|
111
|
+
spinAngle += dt * (s === "thinking" ? 2.6 : 0.5) * (reduced ? 0 : 1);
|
|
112
|
+
const c = width / 2;
|
|
113
|
+
const base = width * 0.3 * (1 + (reduced ? 0 : smooth * 0.14));
|
|
114
|
+
ctx.clearRect(0, 0, width, width);
|
|
115
|
+
const halo = ctx.createRadialGradient(c, c, base * 0.6, c, c, base * 1.65);
|
|
116
|
+
halo.addColorStop(0, glow);
|
|
117
|
+
halo.addColorStop(1, "rgba(0,0,0,0)");
|
|
118
|
+
ctx.fillStyle = halo;
|
|
119
|
+
ctx.globalAlpha = 0.5 + smooth * 0.5;
|
|
120
|
+
ctx.fillRect(0, 0, width, width);
|
|
121
|
+
ctx.globalAlpha = 1;
|
|
122
|
+
const wobble = reduced ? 0 : s === "thinking" ? 0.02 : 0.05 + smooth * 0.2;
|
|
123
|
+
const points = 72;
|
|
124
|
+
ctx.beginPath();
|
|
125
|
+
for (let i = 0; i <= points; i++) {
|
|
126
|
+
const a = i / points * Math.PI * 2;
|
|
127
|
+
const n = Math.sin(a * 3 + t * 1.7) * 0.5 + Math.sin(a * 5 - t * 2.3 + 1) * 0.3 + Math.sin(a * 2 + t * 0.9 + 2) * 0.2;
|
|
128
|
+
const r = base * (1 + n * wobble);
|
|
129
|
+
const x = c + Math.cos(a) * r;
|
|
130
|
+
const y = c + Math.sin(a) * r;
|
|
131
|
+
if (i === 0) ctx.moveTo(x, y);
|
|
132
|
+
else ctx.lineTo(x, y);
|
|
133
|
+
}
|
|
134
|
+
ctx.closePath();
|
|
135
|
+
const fill = ctx.createConicGradient ? ctx.createConicGradient(spinAngle, c, c) : ctx.createLinearGradient(0, 0, width, width);
|
|
136
|
+
palette.forEach((color, i) => fill.addColorStop(i / palette.length, color));
|
|
137
|
+
fill.addColorStop(1, palette[0]);
|
|
138
|
+
ctx.fillStyle = fill;
|
|
139
|
+
ctx.fill();
|
|
140
|
+
const shine = ctx.createRadialGradient(c - base * 0.35, c - base * 0.4, 0, c, c, base * 1.1);
|
|
141
|
+
shine.addColorStop(0, "rgba(255,255,255,0.45)");
|
|
142
|
+
shine.addColorStop(0.5, "rgba(255,255,255,0.05)");
|
|
143
|
+
shine.addColorStop(1, "rgba(0,0,0,0.25)");
|
|
144
|
+
ctx.fillStyle = shine;
|
|
145
|
+
ctx.fill();
|
|
146
|
+
if (s === "thinking" && !reduced) {
|
|
147
|
+
for (let k = 0; k < 3; k++) {
|
|
148
|
+
const a = spinAngle * 1.4 + k * Math.PI * 2 / 3;
|
|
149
|
+
ctx.beginPath();
|
|
150
|
+
ctx.arc(c + Math.cos(a) * base * 1.28, c + Math.sin(a) * base * 1.28, width * 0.014, 0, Math.PI * 2);
|
|
151
|
+
ctx.fillStyle = palette[k % palette.length];
|
|
152
|
+
ctx.fill();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (visible) frame = requestAnimationFrame(draw);
|
|
156
|
+
};
|
|
157
|
+
const observer = new ResizeObserver(resize);
|
|
158
|
+
observer.observe(wrap);
|
|
159
|
+
const io = new IntersectionObserver(([entry]) => {
|
|
160
|
+
visible = entry.isIntersecting;
|
|
161
|
+
if (visible && !frame) {
|
|
162
|
+
last = performance.now();
|
|
163
|
+
frame = requestAnimationFrame(draw);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
io.observe(wrap);
|
|
167
|
+
resize();
|
|
168
|
+
frame = requestAnimationFrame(draw);
|
|
169
|
+
return () => {
|
|
170
|
+
cancelAnimationFrame(frame);
|
|
171
|
+
observer.disconnect();
|
|
172
|
+
io.disconnect();
|
|
173
|
+
};
|
|
174
|
+
}, []);
|
|
175
|
+
return /* @__PURE__ */ jsxs("div", { ref: wrapRef, className: `bl-orb ${className}`, role: "img", "aria-label": `${label}: ${STATE_TEXT[state] ?? state}`, style: { "--bl-size": `${size}px`, ...style }, children: [
|
|
176
|
+
/* @__PURE__ */ jsx("canvas", { ref: canvasRef, "aria-hidden": "true" }),
|
|
177
|
+
/* @__PURE__ */ jsx("span", { className: "bl-orb__sr", "aria-live": "polite", children: STATE_TEXT[state] ?? state })
|
|
178
|
+
] });
|
|
179
|
+
}
|
|
180
|
+
export {
|
|
181
|
+
VoiceOrb as default
|
|
182
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type CSSProperties } from 'react';
|
|
2
|
+
export interface Biome {
|
|
3
|
+
/** Highest height (inclusive) that belongs to this biome. */
|
|
4
|
+
max: number;
|
|
5
|
+
name: string;
|
|
6
|
+
color: string;
|
|
7
|
+
}
|
|
8
|
+
export interface Tile {
|
|
9
|
+
x: number;
|
|
10
|
+
y: number;
|
|
11
|
+
h: number;
|
|
12
|
+
}
|
|
13
|
+
export interface VoxelTilesProps {
|
|
14
|
+
/** Heights per cell (rows of columns); 0 is water. */
|
|
15
|
+
map?: number[][];
|
|
16
|
+
/** Generated map size when no map is given. */
|
|
17
|
+
size?: number;
|
|
18
|
+
seed?: number;
|
|
19
|
+
tileSize?: number;
|
|
20
|
+
/** Height of one block in SVG units. */
|
|
21
|
+
heightUnit?: number;
|
|
22
|
+
/** Hover lift. */
|
|
23
|
+
lift?: number;
|
|
24
|
+
/** Rise-in delay per diagonal, in ms. */
|
|
25
|
+
stagger?: number;
|
|
26
|
+
biomes?: Biome[];
|
|
27
|
+
selectable?: boolean;
|
|
28
|
+
onTileSelect?: (tile: Tile & {
|
|
29
|
+
biome: string;
|
|
30
|
+
}) => void;
|
|
31
|
+
highlightColor?: string;
|
|
32
|
+
label?: string;
|
|
33
|
+
className?: string;
|
|
34
|
+
style?: CSSProperties;
|
|
35
|
+
}
|
|
36
|
+
export declare const DEFAULT_BIOMES: Biome[];
|
|
37
|
+
export declare function generateHeightmap(size?: number, seed?: number): number[][];
|
|
38
|
+
export default function VoxelTiles({ map, size, seed, tileSize, heightUnit, lift, stagger, biomes, selectable, onTileSelect, highlightColor, label, className, style }: VoxelTilesProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useId, useInsertionEffect, useMemo, useRef, useState } from "react";
|
|
4
|
+
const STYLE_ID = "bl-voxel-tiles";
|
|
5
|
+
const CSS = `
|
|
6
|
+
.bl-vox { display: block; width: 100%; max-width: var(--bl-w); height: auto; margin: 0 auto; overflow: visible; outline: none; }
|
|
7
|
+
.bl-vox:focus-visible { outline: 2px dashed var(--bl-hi); outline-offset: 6px; }
|
|
8
|
+
.bl-vox__tile { transition: transform 160ms steps(3); animation: bl-vox-rise 560ms cubic-bezier(.25,1.4,.5,1) backwards; animation-delay: calc(var(--d) * 1ms); cursor: pointer; }
|
|
9
|
+
.bl-vox__tile:hover, .bl-vox__tile[data-cursor], .bl-vox__tile[data-selected] { transform: translateY(calc(var(--bl-lift) * -1px)); }
|
|
10
|
+
.bl-vox__tile[data-cursor] .bl-vox__top, .bl-vox__tile[data-selected] .bl-vox__top { stroke: var(--bl-hi); stroke-width: 1.5; }
|
|
11
|
+
.bl-vox__tile[data-water] .bl-vox__top { transform-box: fill-box; animation: bl-vox-bob 2.6s ease-in-out infinite; animation-delay: calc(var(--d) * -4ms); }
|
|
12
|
+
.bl-vox[data-paused] .bl-vox__top { animation-play-state: paused; }
|
|
13
|
+
.bl-vox__sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
|
|
14
|
+
@keyframes bl-vox-rise { from { transform: translateY(40px); opacity: 0; } }
|
|
15
|
+
@keyframes bl-vox-bob { 50% { transform: translateY(1.5px); } }
|
|
16
|
+
@media (prefers-reduced-motion: reduce) { .bl-vox__tile { animation: none; transition: none; } .bl-vox__tile[data-water] .bl-vox__top { animation: none; } }
|
|
17
|
+
`;
|
|
18
|
+
function useStyles() {
|
|
19
|
+
useInsertionEffect(() => {
|
|
20
|
+
if (typeof document === "undefined") return;
|
|
21
|
+
const style = document.getElementById(STYLE_ID) ?? Object.assign(document.createElement("style"), { id: STYLE_ID });
|
|
22
|
+
style.textContent = CSS;
|
|
23
|
+
if (!style.isConnected) document.head.appendChild(style);
|
|
24
|
+
}, []);
|
|
25
|
+
}
|
|
26
|
+
const DEFAULT_BIOMES = [
|
|
27
|
+
{ max: 0, name: "Water", color: "#41a6f6" },
|
|
28
|
+
{ max: 1, name: "Sand", color: "#ffcd75" },
|
|
29
|
+
{ max: 3, name: "Grass", color: "#38b764" },
|
|
30
|
+
{ max: 4, name: "Forest", color: "#257179" },
|
|
31
|
+
{ max: 5, name: "Stone", color: "#94b0c2" },
|
|
32
|
+
{ max: Infinity, name: "Snow", color: "#f4f4f4" }
|
|
33
|
+
];
|
|
34
|
+
function random(seed) {
|
|
35
|
+
let a = seed | 0;
|
|
36
|
+
return () => {
|
|
37
|
+
a = a + 1831565813 | 0;
|
|
38
|
+
let t = Math.imul(a ^ a >>> 15, 1 | a);
|
|
39
|
+
t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
|
|
40
|
+
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function generateHeightmap(size = 12, seed = 3) {
|
|
44
|
+
const rand = random(seed);
|
|
45
|
+
let grid = Array.from({ length: size }, () => Array.from({ length: size }, () => rand()));
|
|
46
|
+
for (let pass = 0; pass < 2; pass++) {
|
|
47
|
+
grid = grid.map(
|
|
48
|
+
(row, y) => row.map((_, x) => {
|
|
49
|
+
let sum = 0;
|
|
50
|
+
let n = 0;
|
|
51
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
52
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
53
|
+
const v = grid[y + dy]?.[x + dx];
|
|
54
|
+
if (v !== void 0) {
|
|
55
|
+
sum += v;
|
|
56
|
+
n++;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return sum / n;
|
|
61
|
+
})
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
const flat = grid.flat();
|
|
65
|
+
const lo = Math.min(...flat);
|
|
66
|
+
const span = Math.max(...flat) - lo || 1;
|
|
67
|
+
const c = (size - 1) / 2;
|
|
68
|
+
return grid.map(
|
|
69
|
+
(row, y) => row.map((v, x) => {
|
|
70
|
+
const falloff = Math.max(0, 1 - Math.hypot(x - c, y - c) / (size * 0.58));
|
|
71
|
+
return Math.max(0, Math.round(falloff ** 0.8 * (0.25 + (v - lo) / span * 1.1) * 8 - 1));
|
|
72
|
+
})
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
function shade(hex, amount) {
|
|
76
|
+
const n = parseInt(hex.replace("#", "").slice(0, 6), 16);
|
|
77
|
+
const f = (ch) => Math.round((n >> ch & 255) * amount);
|
|
78
|
+
return `rgb(${f(16)},${f(8)},${f(0)})`;
|
|
79
|
+
}
|
|
80
|
+
function VoxelTiles({
|
|
81
|
+
map,
|
|
82
|
+
size = 12,
|
|
83
|
+
seed = 3,
|
|
84
|
+
tileSize = 32,
|
|
85
|
+
heightUnit = 8,
|
|
86
|
+
lift = 6,
|
|
87
|
+
stagger = 30,
|
|
88
|
+
biomes = DEFAULT_BIOMES,
|
|
89
|
+
selectable = true,
|
|
90
|
+
onTileSelect,
|
|
91
|
+
highlightColor = "#f4f4f4",
|
|
92
|
+
label = "Voxel island",
|
|
93
|
+
className = "",
|
|
94
|
+
style
|
|
95
|
+
}) {
|
|
96
|
+
useStyles();
|
|
97
|
+
const uid = useId().replace(/[^a-zA-Z0-9_-]/g, "");
|
|
98
|
+
const heights = useMemo(() => map ?? generateHeightmap(size, seed), [map, size, seed]);
|
|
99
|
+
const rows = heights.length;
|
|
100
|
+
const cols = heights[0]?.length ?? 0;
|
|
101
|
+
const [cursor, setCursor] = useState(null);
|
|
102
|
+
const [selected, setSelected] = useState(null);
|
|
103
|
+
const [paused, setPaused] = useState(false);
|
|
104
|
+
const svgRef = useRef(null);
|
|
105
|
+
useEffect(() => {
|
|
106
|
+
const io = new IntersectionObserver(([entry]) => setPaused(!entry.isIntersecting));
|
|
107
|
+
io.observe(svgRef.current);
|
|
108
|
+
return () => io.disconnect();
|
|
109
|
+
}, []);
|
|
110
|
+
const a = tileSize / 2;
|
|
111
|
+
const biomeOf = (h) => biomes.find((b) => h <= b.max) ?? biomes[biomes.length - 1];
|
|
112
|
+
const maxH = Math.max(0, ...heights.flat());
|
|
113
|
+
const minX = -rows * a;
|
|
114
|
+
const top = -(maxH + 1) * heightUnit - a / 2 - lift - 2;
|
|
115
|
+
const viewW = (rows + cols) * a;
|
|
116
|
+
const viewH = (rows + cols) * (a / 2) + a / 2 - top + 4;
|
|
117
|
+
const tiles = [];
|
|
118
|
+
for (let s = 0; s < rows + cols - 1; s++) {
|
|
119
|
+
for (let y = 0; y < rows; y++) {
|
|
120
|
+
const x = s - y;
|
|
121
|
+
if (x < 0 || x >= cols) continue;
|
|
122
|
+
tiles.push({ x, y, h: heights[y][x] });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const describe = (tile) => tile ? `${biomeOf(tile.h).name}, height ${tile.h}, column ${tile.x + 1}, row ${tile.y + 1}` : "";
|
|
126
|
+
const select = (tile) => {
|
|
127
|
+
if (!selectable) return;
|
|
128
|
+
setSelected(tile);
|
|
129
|
+
onTileSelect?.({ ...tile, biome: biomeOf(tile.h).name });
|
|
130
|
+
};
|
|
131
|
+
const onKeyDown = (event) => {
|
|
132
|
+
const moves = { ArrowRight: [1, 0], ArrowLeft: [-1, 0], ArrowDown: [0, 1], ArrowUp: [0, -1] };
|
|
133
|
+
if (event.key in moves) {
|
|
134
|
+
event.preventDefault();
|
|
135
|
+
const [dx, dy] = moves[event.key];
|
|
136
|
+
const from = cursor ?? { x: Math.floor(cols / 2), y: Math.floor(rows / 2) };
|
|
137
|
+
const x = Math.max(0, Math.min(cols - 1, from.x + (cursor ? dx : 0)));
|
|
138
|
+
const y = Math.max(0, Math.min(rows - 1, from.y + (cursor ? dy : 0)));
|
|
139
|
+
setCursor({ x, y, h: heights[y][x] });
|
|
140
|
+
} else if ((event.key === "Enter" || event.key === " ") && cursor) {
|
|
141
|
+
event.preventDefault();
|
|
142
|
+
select(cursor);
|
|
143
|
+
} else if (event.key === "Escape") {
|
|
144
|
+
setCursor(null);
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
148
|
+
/* @__PURE__ */ jsx(
|
|
149
|
+
"svg",
|
|
150
|
+
{
|
|
151
|
+
ref: svgRef,
|
|
152
|
+
className: `bl-vox ${className}`,
|
|
153
|
+
viewBox: `${minX} ${top} ${viewW} ${viewH}`,
|
|
154
|
+
role: "application",
|
|
155
|
+
"aria-roledescription": "tile map",
|
|
156
|
+
"aria-label": `${label}. ${cols} by ${rows} tiles. Use arrow keys to move${selectable ? " and Enter to select" : ""}.`,
|
|
157
|
+
"aria-describedby": `${uid}-status`,
|
|
158
|
+
tabIndex: 0,
|
|
159
|
+
"data-paused": paused || void 0,
|
|
160
|
+
onKeyDown,
|
|
161
|
+
onFocus: () => setCursor((c) => c ?? { x: Math.floor(cols / 2), y: Math.floor(rows / 2), h: heights[Math.floor(rows / 2)][Math.floor(cols / 2)] }),
|
|
162
|
+
onBlur: () => setCursor(null),
|
|
163
|
+
style: { "--bl-w": `${viewW * 1.5}px`, "--bl-hi": highlightColor, "--bl-lift": lift, ...style },
|
|
164
|
+
children: tiles.map((tile) => {
|
|
165
|
+
const { x, y, h } = tile;
|
|
166
|
+
const biome = biomeOf(h);
|
|
167
|
+
const water = h <= 0;
|
|
168
|
+
const sx = (x - y) * a;
|
|
169
|
+
const sy = (x + y) * (a / 2);
|
|
170
|
+
const H = water ? heightUnit * 0.6 : (h + 1) * heightUnit;
|
|
171
|
+
const t = sy - H;
|
|
172
|
+
return /* @__PURE__ */ jsxs(
|
|
173
|
+
"g",
|
|
174
|
+
{
|
|
175
|
+
className: "bl-vox__tile",
|
|
176
|
+
"data-water": water || void 0,
|
|
177
|
+
"data-cursor": cursor && cursor.x === x && cursor.y === y || void 0,
|
|
178
|
+
"data-selected": selected && selected.x === x && selected.y === y || void 0,
|
|
179
|
+
style: { "--d": (x + y) * stagger },
|
|
180
|
+
onClick: () => select(tile),
|
|
181
|
+
shapeRendering: "crispEdges",
|
|
182
|
+
children: [
|
|
183
|
+
/* @__PURE__ */ jsx("polygon", { points: `${sx - a},${t} ${sx},${t + a / 2} ${sx},${sy + a / 2} ${sx - a},${sy}`, fill: shade(biome.color, water ? 0.7 : 0.72) }),
|
|
184
|
+
/* @__PURE__ */ jsx("polygon", { points: `${sx},${t + a / 2} ${sx + a},${t} ${sx + a},${sy} ${sx},${sy + a / 2}`, fill: shade(biome.color, water ? 0.55 : 0.52) }),
|
|
185
|
+
/* @__PURE__ */ jsx("polygon", { className: "bl-vox__top", points: `${sx},${t - a / 2} ${sx + a},${t} ${sx},${t + a / 2} ${sx - a},${t}`, fill: biome.color, fillOpacity: water ? 0.9 : 1 }),
|
|
186
|
+
!water && /* @__PURE__ */ jsx("polyline", { points: `${sx - a},${t} ${sx},${t - a / 2} ${sx + a},${t}`, fill: "none", stroke: "rgba(255,255,255,0.35)", strokeWidth: "1" })
|
|
187
|
+
]
|
|
188
|
+
},
|
|
189
|
+
`${x}-${y}`
|
|
190
|
+
);
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
),
|
|
194
|
+
/* @__PURE__ */ jsx("span", { id: `${uid}-status`, className: "bl-vox__sr", "aria-live": "polite", children: cursor ? describe(cursor) : selected ? `Selected: ${describe(selected)}` : "" })
|
|
195
|
+
] });
|
|
196
|
+
}
|
|
197
|
+
export {
|
|
198
|
+
DEFAULT_BIOMES,
|
|
199
|
+
VoxelTiles as default,
|
|
200
|
+
generateHeightmap
|
|
201
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type CSSProperties } from 'react';
|
|
2
|
+
export interface WarpFieldProps {
|
|
3
|
+
stars?: number;
|
|
4
|
+
/** Cruising speed. */
|
|
5
|
+
speed?: number;
|
|
6
|
+
/** Hyperspace multiplier while held. */
|
|
7
|
+
boost?: number;
|
|
8
|
+
/** Hold the pointer down (or Space while focused) for hyperspace. */
|
|
9
|
+
holdToBoost?: boolean;
|
|
10
|
+
/** How far the vanishing point leans toward the pointer. */
|
|
11
|
+
lean?: number;
|
|
12
|
+
pixelSize?: number;
|
|
13
|
+
colors?: string[];
|
|
14
|
+
backgroundColor?: string;
|
|
15
|
+
className?: string;
|
|
16
|
+
style?: CSSProperties;
|
|
17
|
+
}
|
|
18
|
+
export default function WarpField({ stars, speed, boost, holdToBoost, lean, pixelSize, colors, backgroundColor, className, style }: WarpFieldProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
const hexToRgb = (hex) => {
|
|
5
|
+
const n = parseInt(hex.replace("#", "").replace(/^(.)(.)(.)$/, "$1$1$2$2$3$3"), 16);
|
|
6
|
+
return [n >> 16 & 255, n >> 8 & 255, n & 255];
|
|
7
|
+
};
|
|
8
|
+
function WarpField({
|
|
9
|
+
stars = 380,
|
|
10
|
+
speed = 0.35,
|
|
11
|
+
boost = 6,
|
|
12
|
+
holdToBoost = true,
|
|
13
|
+
lean = 0.12,
|
|
14
|
+
pixelSize = 2,
|
|
15
|
+
colors = ["#f4f4f4", "#73eff7", "#ffcd75"],
|
|
16
|
+
backgroundColor = "#0b0b14",
|
|
17
|
+
className = "",
|
|
18
|
+
style
|
|
19
|
+
}) {
|
|
20
|
+
const canvasRef = useRef(null);
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
const canvas = canvasRef.current;
|
|
23
|
+
const ctx = canvas.getContext("2d");
|
|
24
|
+
const reduced = window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
|
|
25
|
+
const tints = colors.map(hexToRgb);
|
|
26
|
+
let W = 0;
|
|
27
|
+
let H = 0;
|
|
28
|
+
let dpr = 1;
|
|
29
|
+
let raf = 0;
|
|
30
|
+
let last = 0;
|
|
31
|
+
let visible = true;
|
|
32
|
+
let warp = 1;
|
|
33
|
+
let held = false;
|
|
34
|
+
const aim = { x: 0, y: 0, tx: 0, ty: 0 };
|
|
35
|
+
const spawn = (s, far) => {
|
|
36
|
+
s.x = (Math.random() - 0.5) * 2;
|
|
37
|
+
s.y = (Math.random() - 0.5) * 2;
|
|
38
|
+
s.z = far ? 1 : 0.15 + Math.random() * 0.85;
|
|
39
|
+
s.pz = s.z;
|
|
40
|
+
s.tint = tints[Math.floor(Math.random() * tints.length)];
|
|
41
|
+
return s;
|
|
42
|
+
};
|
|
43
|
+
const field = Array.from({ length: stars }, () => spawn({}, false));
|
|
44
|
+
const fit = () => {
|
|
45
|
+
dpr = Math.min(2, window.devicePixelRatio || 1);
|
|
46
|
+
W = canvas.clientWidth;
|
|
47
|
+
H = canvas.clientHeight;
|
|
48
|
+
canvas.width = Math.round(W * dpr);
|
|
49
|
+
canvas.height = Math.round(H * dpr);
|
|
50
|
+
};
|
|
51
|
+
const project = (x, y, z) => {
|
|
52
|
+
const scale = Math.max(W, H) * 0.5;
|
|
53
|
+
return [W / 2 + aim.x * W + x / z * scale, H / 2 + aim.y * H + y / z * scale];
|
|
54
|
+
};
|
|
55
|
+
const draw = () => {
|
|
56
|
+
ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
57
|
+
ctx.fillStyle = backgroundColor;
|
|
58
|
+
ctx.fillRect(0, 0, W, H);
|
|
59
|
+
const p = pixelSize;
|
|
60
|
+
field.forEach((s) => {
|
|
61
|
+
const [x1, y1] = project(s.x, s.y, s.z);
|
|
62
|
+
const [x0, y0] = project(s.x, s.y, s.pz);
|
|
63
|
+
const near = 1 - s.z;
|
|
64
|
+
const [r, g, b] = s.tint;
|
|
65
|
+
ctx.fillStyle = `rgba(${r},${g},${b},${Math.min(1, 0.25 + near * 1.1)})`;
|
|
66
|
+
const len = Math.hypot(x1 - x0, y1 - y0);
|
|
67
|
+
const steps = Math.max(1, Math.min(40, Math.ceil(len / p)));
|
|
68
|
+
for (let k = 0; k <= steps; k++) {
|
|
69
|
+
const x = x0 + (x1 - x0) * k / steps;
|
|
70
|
+
const y = y0 + (y1 - y0) * k / steps;
|
|
71
|
+
ctx.fillRect(Math.round(x / p) * p, Math.round(y / p) * p, p, p);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
const tick = (now) => {
|
|
76
|
+
const dt = Math.max(0, Math.min(0.05, (now - last) / 1e3));
|
|
77
|
+
last = now;
|
|
78
|
+
warp += ((held ? boost : 1) - warp) * Math.min(1, dt * 3);
|
|
79
|
+
aim.x += (aim.tx - aim.x) * Math.min(1, dt * 2);
|
|
80
|
+
aim.y += (aim.ty - aim.y) * Math.min(1, dt * 2);
|
|
81
|
+
field.forEach((s) => {
|
|
82
|
+
s.pz = s.z + 0.02 * warp;
|
|
83
|
+
s.z -= speed * warp * dt;
|
|
84
|
+
if (s.z < 0.02) spawn(s, true);
|
|
85
|
+
else {
|
|
86
|
+
const [x, y] = project(s.x, s.y, s.z);
|
|
87
|
+
if (x < -50 || y < -50 || x > W + 50 || y > H + 50) spawn(s, true);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
draw();
|
|
91
|
+
raf = visible && !document.hidden ? requestAnimationFrame(tick) : 0;
|
|
92
|
+
};
|
|
93
|
+
const start = () => {
|
|
94
|
+
if (raf || reduced || !visible || document.hidden) return;
|
|
95
|
+
last = performance.now();
|
|
96
|
+
raf = requestAnimationFrame(tick);
|
|
97
|
+
};
|
|
98
|
+
const onMove = (event) => {
|
|
99
|
+
const r = canvas.getBoundingClientRect();
|
|
100
|
+
aim.tx = ((event.clientX - r.left) / r.width - 0.5) * lean;
|
|
101
|
+
aim.ty = ((event.clientY - r.top) / r.height - 0.5) * lean;
|
|
102
|
+
};
|
|
103
|
+
const press = (on) => () => {
|
|
104
|
+
if (holdToBoost) held = on;
|
|
105
|
+
};
|
|
106
|
+
const down = press(true);
|
|
107
|
+
const up = press(false);
|
|
108
|
+
const onKey = (event) => {
|
|
109
|
+
if (event.code === "Space" && holdToBoost) {
|
|
110
|
+
event.preventDefault();
|
|
111
|
+
held = event.type === "keydown";
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
const resize = new ResizeObserver(() => {
|
|
115
|
+
fit();
|
|
116
|
+
draw();
|
|
117
|
+
});
|
|
118
|
+
resize.observe(canvas);
|
|
119
|
+
const observer = new IntersectionObserver(([entry]) => {
|
|
120
|
+
visible = entry.isIntersecting;
|
|
121
|
+
start();
|
|
122
|
+
});
|
|
123
|
+
observer.observe(canvas);
|
|
124
|
+
document.addEventListener("visibilitychange", start);
|
|
125
|
+
canvas.addEventListener("pointermove", onMove);
|
|
126
|
+
canvas.addEventListener("pointerdown", down);
|
|
127
|
+
window.addEventListener("pointerup", up);
|
|
128
|
+
canvas.addEventListener("pointerleave", up);
|
|
129
|
+
canvas.addEventListener("keydown", onKey);
|
|
130
|
+
canvas.addEventListener("keyup", onKey);
|
|
131
|
+
fit();
|
|
132
|
+
draw();
|
|
133
|
+
start();
|
|
134
|
+
return () => {
|
|
135
|
+
cancelAnimationFrame(raf);
|
|
136
|
+
resize.disconnect();
|
|
137
|
+
observer.disconnect();
|
|
138
|
+
document.removeEventListener("visibilitychange", start);
|
|
139
|
+
canvas.removeEventListener("pointermove", onMove);
|
|
140
|
+
canvas.removeEventListener("pointerdown", down);
|
|
141
|
+
window.removeEventListener("pointerup", up);
|
|
142
|
+
canvas.removeEventListener("pointerleave", up);
|
|
143
|
+
canvas.removeEventListener("keydown", onKey);
|
|
144
|
+
canvas.removeEventListener("keyup", onKey);
|
|
145
|
+
};
|
|
146
|
+
}, [stars, speed, boost, holdToBoost, lean, pixelSize, colors.join(), backgroundColor]);
|
|
147
|
+
return /* @__PURE__ */ jsx(
|
|
148
|
+
"canvas",
|
|
149
|
+
{
|
|
150
|
+
ref: canvasRef,
|
|
151
|
+
"aria-hidden": "true",
|
|
152
|
+
tabIndex: holdToBoost ? -1 : void 0,
|
|
153
|
+
className,
|
|
154
|
+
style: { display: "block", width: "100%", height: "100%", background: backgroundColor, touchAction: "pan-y", outline: "none", ...style }
|
|
155
|
+
}
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
export {
|
|
159
|
+
WarpField as default
|
|
160
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { type CSSProperties } from 'react';
|
|
2
|
+
export type WebcamStatus = 'idle' | 'requesting' | 'live' | 'denied' | 'missing' | 'busy' | 'unsupported' | 'error';
|
|
3
|
+
export interface WebcamPixelsProps {
|
|
4
|
+
/** Render this image or video URL instead of the camera. */
|
|
5
|
+
src?: string;
|
|
6
|
+
mode?: 'pixels' | 'ascii' | 'dots';
|
|
7
|
+
/** Grid pitch in px. */
|
|
8
|
+
cellSize?: number;
|
|
9
|
+
/** Snap colours to these. */
|
|
10
|
+
palette?: string[];
|
|
11
|
+
/** Flip horizontally (default: on for the camera). */
|
|
12
|
+
mirror?: boolean;
|
|
13
|
+
contrast?: number;
|
|
14
|
+
/** Redraw rate. */
|
|
15
|
+
fps?: number;
|
|
16
|
+
/** Ask for the camera on mount instead of after a click. */
|
|
17
|
+
autoStart?: boolean;
|
|
18
|
+
/** Draw everything in inkColor. */
|
|
19
|
+
monochrome?: boolean;
|
|
20
|
+
inkColor?: string;
|
|
21
|
+
backgroundColor?: string;
|
|
22
|
+
textColor?: string;
|
|
23
|
+
mutedColor?: string;
|
|
24
|
+
accentColor?: string;
|
|
25
|
+
/** The LIVE dot. */
|
|
26
|
+
liveColor?: string;
|
|
27
|
+
radius?: number;
|
|
28
|
+
fontFamily?: string;
|
|
29
|
+
/** Start-screen heading. */
|
|
30
|
+
title?: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
/** The canvas's accessible name. */
|
|
33
|
+
label?: string;
|
|
34
|
+
onStatusChange?: (status: WebcamStatus) => void;
|
|
35
|
+
className?: string;
|
|
36
|
+
style?: CSSProperties;
|
|
37
|
+
}
|
|
38
|
+
export default function WebcamPixels({ src, mode, cellSize, palette, mirror, contrast, fps, autoStart, monochrome, inkColor, backgroundColor, textColor, mutedColor, accentColor, liveColor, radius, fontFamily, title, description, label, onStatusChange, className, style }: WebcamPixelsProps): import("react").JSX.Element;
|