-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
32 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,40 @@ | ||
import { configure, fs } from '@zenfs/core'; | ||
import { framebuffer, dsp } from '@zenfs/devices'; | ||
import { framebuffer } from '@zenfs/devices'; | ||
|
||
// this is optional, but I set them, so I have control | ||
const canvas = document.querySelector('#fb'); | ||
const { width, height } = canvas; | ||
|
||
const audioContext = new AudioContext(); | ||
|
||
// add initial devices like /dev/null, etc | ||
await configure({ addDevices: true }); | ||
|
||
const devfs = fs.mounts.get('/dev'); | ||
|
||
// mount framebuffer & dsp | ||
devfs.createDevice('/fb0', framebuffer({ canvas })); | ||
devfs.createDevice('/dsp', await dsp({ audioContext })); | ||
fs.mounts.get('/dev').createDevice('/fb0', framebuffer({ canvas })); | ||
|
||
// example: write static to framebuffer | ||
// example: write gradient with changing hue to framebuffer | ||
const screen = new Uint8Array(width * height * 4); | ||
|
||
let hue = 0; | ||
|
||
function hslToRgb(hue, saturation) { | ||
const a = saturation / 2; | ||
const f = n => { | ||
const k = (n + hue / 30) % 12; | ||
return 0.5 - a * Math.max(Math.min(k - 3, 9 - k, 1), -1); | ||
}; | ||
return [f(0) * 255, f(8) * 255, f(4) * 255]; | ||
} | ||
|
||
function makeGradientFb() { | ||
hue = (hue + 1) % 360; // Increment hue and keep it in the 0-359 range | ||
|
||
for (let y = 0; y < height; y++) { | ||
for (let x = 0; x < width; x++) { | ||
const index = (y * width + x) * 4; | ||
const gradientValue = (x / width) * 255; | ||
screen.set([gradientValue, gradientValue, 255 - gradientValue, 255], index); | ||
const gradientValue = (x / width) * 100; // Adjust the saturation and lightness effect | ||
const [r, g, b] = hslToRgb(hue, gradientValue / 100, 0.5); // S=gradientValue, L=0.5 for vivid color | ||
|
||
screen.set([r, g, b, 255], index); | ||
} | ||
} | ||
fs.promises.writeFile('/dev/fb0', screen); | ||
fs.writeFileSync('/dev/fb0', screen, { flag: 'r+' }); | ||
requestAnimationFrame(makeGradientFb); | ||
} | ||
makeGradientFb(); | ||
|
||
// example: write static to audio | ||
const audioBuffer = new Float32Array(audioContext.sampleRate * 4); | ||
setInterval(() => { | ||
for (let i in audioBuffer) { | ||
audioBuffer[i] = Math.random() * 2 - 1; | ||
} | ||
fs.promises.writeFile('/dev/dsp', audioBuffer); | ||
}, 1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
<html> | ||
<head> | ||
<title>ZenFS Devices</title> | ||
<link rel="stylesheet" href="./demo.css" /> | ||
<link rel="stylesheet" href="./framebuffer.css" /> | ||
</head> | ||
<body> | ||
<p>This will demonstrate drawing to a framebuffer & creating audio:</p> | ||
<p>Framebuffer demo</p> | ||
<canvas id="fb"></canvas> | ||
<script type="module" src="./demo.js"></script> | ||
<script type="module" src="./framebuffer.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters