Skip to content

Instantly share code, notes, and snippets.

@remy
Created June 1, 2018 20:30
Show Gist options
  • Save remy/f6273783b2a32b71c86ab6556d7f1bae to your computer and use it in GitHub Desktop.
Save remy/f6273783b2a32b71c86ab6556d7f1bae to your computer and use it in GitHub Desktop.
// Example of the JavaScript module for the Arc project.
let bytes;
let slicePtr = 0;
function yellow(line, i) {
line[i + 1] = line[i] = 255;
line[i + 2] = 0;
}
function blue(line, i) {
line[i + 1] = line[i] = 0;
line[i + 2] = 255;
}
function setFrameByBytes(frame, rows, cols, bytes) {
let bytePtr = 0;
let bitPtr = 0;
for (let i = 0; i < rows; i++) {
const lineSize = cols * 3;
const line = frame.subarray(lineSize * i, lineSize * (i + 1));
for (let j = 0; j < cols; j++) {
if (bytes[bytePtr]) {
yellow(line, j * 3);
} else {
blue(line, j * 3);
}
}
bytePtr++;
}
}
export function transform(buffer, rows, cols, frameCount, fps, isFirst) {
const frameSize = 3 * rows * cols;
for (let i = 0; i < frameCount; i++) {
const second = i / fps;
const frame = new Uint8Array(buffer, frameSize * i, frameSize);
setFrameByBytes(
frame,
rows,
cols,
bytes.subarray(slicePtr, slicePtr + rows)
);
slicePtr++;
}
}
function toBits(acc, curr) {
for (let i = 7; i >= 0; i--) {
acc.push(curr << i & 1);
}
return acc;
}
export default function() {
return fetch('https://2018.jsconf.eu/')
.then(res => res.arrayBuffer())
.then(res => {
bytes = Uint8Array.from(new Uint8Array(res).reduce(toBits, []))
return Promise.resolve({
transform,
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment