Skip to content

Instantly share code, notes, and snippets.

@arifd
arifd / edgeDetect.js
Created March 16, 2020 14:57
Simple, fast Sobel Edge Detection in Javascript
// I wrote a sobel edge detector in Javascript!
// todo: try Laplacian of Gaussian (LoG) instead of Sobel
// Example usage (taking the pxiels 1D):
// const edge = createEdgeMapFromImageData(imageData);
// for (const i in edge) {
// let x = i % canvas.width;
// let y = (i - x) / canvas.width;
// ctx.fillStyle = `rgba(${edge[i]},${edge[i]},${edge[i]},255)`;
// ctx.fillRect(x, y, 1, 1 );
@arifd
arifd / primes-and-factors.js
Created March 11, 2020 17:13
Prime numbers & factors
export function listFactors(target) {
let primes = listPrimes(target);
let factors = [];
for (let i = 0; i < primes.length; i++) {
if ((target % primes[i]) === 0) factors.push(primes[i]);
}
return factors;
}
// list all prime numbers up to n
@arifd
arifd / createScaledImageFromSource.js
Last active January 30, 2020 10:32
Let the browser scale/resize an image for you
// If you want to load an image but have it be scaled.
// instead of 'new Image();' call this function, with onload being its 3rd argument.
function createScaledImageFromSource(src, scaleFactor, callback = null)
{
let img = new Image();
img.src = src;
let outImage = new Image();
img.onload = () =>
@arifd
arifd / webcam2webGL.js
Created January 13, 2020 19:16
Get webcam into WebGL
// In INITIALISATION:
// create webcam stream
let video = document.createElement('video');
video.width = 320;
video.height = 240;
video.autoplay = true;
let constraints = {video: true};
navigator.mediaDevices.getUserMedia(constraints).then(stream => video.srcObject = stream);