|
| 1 | +/** |
| 2 | + * JavaScript30 by Wes Bos, https://javascript30.com/ |
| 3 | + * TypeScript implementation by Will Wager |
| 4 | + * Project: Webcam fun |
| 5 | + * Concepts: Accessing user media; Painting video to canvas; pixel manipulation |
| 6 | + * Key takeaways: Pixel manipulation is straightforward with the ImageData object and a canvas; |
| 7 | + * Create a download link with the download attribute |
| 8 | + * Sidenotes: Small update was required to get the media stream to work (video.srcObject) |
| 9 | + * Compilation command: |
| 10 | + * tsc --removeComments --strictNullChecks --noImplicitAny --target es5 typescripts.ts |
| 11 | + */ |
| 12 | + |
| 13 | +interface GreenScreenConfig { |
| 14 | + [index: string]: number; |
| 15 | +} |
| 16 | + |
| 17 | +const video = document.querySelector('.player') as HTMLVideoElement; |
| 18 | +const canvas = document.querySelector('.photo') as HTMLCanvasElement; |
| 19 | +const ctx = canvas.getContext('2d'); |
| 20 | +const strip = document.querySelector('.strip')! as HTMLDivElement; |
| 21 | +const snap = document.querySelector('.snap') as HTMLAudioElement; |
| 22 | + |
| 23 | +function redEffect(pixels: ImageData) { |
| 24 | + for (let i = 0; i < pixels.data.length; i += 4) { |
| 25 | + pixels.data[i + 0] += 100; |
| 26 | + pixels.data[i + 1] += -50; |
| 27 | + pixels.data[i + 2] *= 0.5; |
| 28 | + } |
| 29 | + return pixels; |
| 30 | +} |
| 31 | + |
| 32 | +function rgbSplit(pixels: ImageData) { |
| 33 | + for (let i = 0; i < pixels.data.length; i += 4) { |
| 34 | + pixels.data[i - 150] = pixels.data[i + 0]; |
| 35 | + pixels.data[i + 100] = pixels.data[i + 1]; |
| 36 | + pixels.data[i - 150] = pixels.data[i + 2]; |
| 37 | + } |
| 38 | + return pixels; |
| 39 | +} |
| 40 | + |
| 41 | +function greenScreen(pixels: ImageData) { |
| 42 | + const levels: GreenScreenConfig = {}; |
| 43 | + |
| 44 | + const inputs = document.querySelectorAll('.rgb input'); |
| 45 | + if (!inputs) return pixels; |
| 46 | + inputs.forEach((input: HTMLInputElement) => levels[input.name] = Number(input.value)); |
| 47 | + |
| 48 | + for (let i = 0; i < pixels.data.length; i = i + 4) { |
| 49 | + const red = pixels.data[i + 0]; |
| 50 | + const green = pixels.data[i + 1]; |
| 51 | + const blue = pixels.data[i + 2]; |
| 52 | + |
| 53 | + if ( |
| 54 | + red >= levels.rmin |
| 55 | + && green >= levels.gmin |
| 56 | + && blue >= levels.bmin |
| 57 | + && red <= levels.rmax |
| 58 | + && green <= levels.gmax |
| 59 | + && blue <= levels.bmax |
| 60 | + ) { |
| 61 | + pixels.data[i + 3] = 0; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return pixels; |
| 66 | +} |
| 67 | + |
| 68 | +function paintToCanvas() { |
| 69 | + const width = video.videoWidth; |
| 70 | + const height = video.videoHeight; |
| 71 | + |
| 72 | + canvas.width = width; |
| 73 | + canvas.height = height; |
| 74 | + |
| 75 | + function paintVideoLoop() { |
| 76 | + if (!ctx) return; |
| 77 | + ctx.drawImage(video, 0, 0, width, height) |
| 78 | + let pixels = ctx.getImageData(0, 0, width, height); |
| 79 | + |
| 80 | + // pixels = redEffect(pixels); |
| 81 | + // pixels = rgbSplit(pixels); |
| 82 | + // ctx.globalAlpha = 0.1; |
| 83 | + pixels = greenScreen(pixels); |
| 84 | + ctx.putImageData(pixels, 0, 0); |
| 85 | + requestAnimationFrame(paintVideoLoop); |
| 86 | + } |
| 87 | + paintVideoLoop(); |
| 88 | +} |
| 89 | + |
| 90 | +function getVideo() { |
| 91 | + navigator.mediaDevices.getUserMedia({ video: true, audio: false }) |
| 92 | + .then(localMediaStream => { |
| 93 | + video.srcObject = localMediaStream; |
| 94 | + video.onloadedmetadata = () => { |
| 95 | + video.play(); |
| 96 | + paintToCanvas(); |
| 97 | + }; |
| 98 | + }) |
| 99 | + .catch(err => console.error(`😬 I'm gonna need your webcam for this...`, err)); |
| 100 | +} |
| 101 | +getVideo(); |
| 102 | + |
| 103 | +function takePhoto() { |
| 104 | + snap.currentTime = 0; |
| 105 | + snap.play(); |
| 106 | + |
| 107 | + const data = canvas.toDataURL('image/jpeg'); |
| 108 | + const link = document.createElement('a'); |
| 109 | + link.href = data; |
| 110 | + link.setAttribute('download', 'handsome'); |
| 111 | + link.innerHTML = `<img src="${data}" alt="Photo booth photo" />`; |
| 112 | + strip.insertBefore(link, strip.firstChild); |
| 113 | +} |
| 114 | + |
0 commit comments