├── .gitignore ├── brush.png ├── erase.png ├── favicon.ico ├── package.json ├── tsconfig.json ├── README.md ├── style.css ├── LICENSE ├── index.htm ├── script.ts ├── underscore.js ├── script.js └── jquery.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /brush.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qix/webcam-slitscan/HEAD/brush.png -------------------------------------------------------------------------------- /erase.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qix/webcam-slitscan/HEAD/erase.png -------------------------------------------------------------------------------- /favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/qix/webcam-slitscan/HEAD/favicon.ico -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "@types/jquery": "^3.3.29" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "system", 4 | "lib": ["es2015", "dom"], 5 | "outFile": "script.js" 6 | }, 7 | "include": ["*.ts"] 8 | } 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Slitscan webcam demo in HTML5 2 | ============================= 3 | 4 | A simple demo of a slitscan effect for time delaying video. 5 | 6 | Demo available at http://qix.github.io/webcam-slitscan/ 7 | 8 | Future plans 9 | ------------ 10 | 11 | One of the better parts is being able to edit the image. Integrating an online 12 | image editor would be a lot of fun. 13 | -------------------------------------------------------------------------------- /style.css: -------------------------------------------------------------------------------- 1 | #draw { 2 | border: 2px solid #aaa; 3 | } 4 | 5 | #output { 6 | border: 2px solid #aaa; 7 | } 8 | 9 | #config { 10 | display: flex; 11 | align-items: baseline; 12 | } 13 | 14 | #config div { 15 | padding-left: 20px; 16 | } 17 | 18 | #status h2 { 19 | margin-top: 20px; 20 | margin-bottom: 0; 21 | font-family: monospace; 22 | } 23 | #status-content { 24 | font-family: monospace; 25 | white-space: pre; 26 | } 27 | 28 | #video, 29 | #snapshot { 30 | display: none; 31 | } 32 | 33 | ul { 34 | margin: 5px 0 0 15px; 35 | padding: 0; 36 | } 37 | 38 | ul li { 39 | float: left; 40 | list-style: none; 41 | margin: 0 8px 0 0; 42 | } 43 | 44 | ul li a { 45 | color: #444; 46 | text-decoration: none; 47 | } 48 | 49 | ul li a:hover { 50 | color: #000; 51 | } 52 | 53 | pre { 54 | clear: both; 55 | } 56 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 Josh Yudaken 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | 9 | 10 | -------------------------------------------------------------------------------- /index.htm: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Slitscan via webcam demo 6 | 7 | 8 | 9 | 10 |

11 | Slitscan Demo (github.com/qix/webcam-slitscan) 14 |

15 | 16 | 17 | 18 | 19 |
20 |
21 | Choose: 22 | 23 |
24 | 25 |
26 | Frames: 27 | 34 |
35 | 36 |
37 | 38 |
39 | 40 |
41 | Drag to draw, right click to erase 42 |
43 |
44 | 45 |
46 | 47 | 48 |
49 | 50 |
51 |

Status

52 |
Trying to connect to webcam...
53 |
54 | 55 | 56 | 57 | 58 | 59 | 60 | -------------------------------------------------------------------------------- /script.ts: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | /*** 3 | * Setup a webcam and fetch data from it 4 | **/ 5 | 6 | function delay(ms) { 7 | return new Promise(resolve => { 8 | setTimeout(resolve, ms); 9 | }); 10 | } 11 | 12 | class Webcam { 13 | snapshot: HTMLCanvasElement; 14 | video: HTMLVideoElement; 15 | context: any; 16 | 17 | async init() { 18 | this.snapshot = document.getElementById("snapshot") as HTMLCanvasElement; 19 | this.video = document.getElementById("video") as HTMLVideoElement; 20 | this.context = this.snapshot.getContext("2d"); 21 | 22 | const stream = await navigator.mediaDevices.getUserMedia({ video: true }); 23 | this.video.srcObject = stream; 24 | 25 | for (let i = 0; i < 5000; i += 100) { 26 | if (this.video.videoWidth > 0) { 27 | return this.video; 28 | } 29 | await delay(100); 30 | } 31 | 32 | throw new Error("Unable to fetch video width from webcam"); 33 | } 34 | 35 | fetch() { 36 | this.context.drawImage(this.video, 0, 0); 37 | return this.context.getImageData( 38 | 0, 39 | 0, 40 | this.video.videoWidth, 41 | this.video.videoHeight 42 | ).data; 43 | } 44 | } 45 | 46 | class App { 47 | drawCanvas: HTMLCanvasElement; 48 | video: HTMLVideoElement; 49 | outputCanvas: HTMLCanvasElement; 50 | snapshotCanvas: HTMLCanvasElement; 51 | videoStream: any; 52 | width: any; 53 | height: any; 54 | drawContext: CanvasRenderingContext2D; 55 | outputContext: CanvasRenderingContext2D; 56 | outputImage: any; 57 | outputData: any; 58 | frames: any[]; 59 | times: any[]; 60 | frameNumber: number; 61 | webcam: Webcam; 62 | frameInterval: number; 63 | async start() { 64 | this.drawCanvas = document.getElementById("draw") as HTMLCanvasElement; 65 | this.video = document.getElementById("video") as HTMLVideoElement; 66 | this.outputCanvas = document.getElementById("output") as HTMLCanvasElement; 67 | this.snapshotCanvas = document.getElementById( 68 | "snapshot" 69 | ) as HTMLCanvasElement; 70 | 71 | this.videoStream = null; 72 | 73 | this.width = this.drawCanvas.width; 74 | this.height = this.drawCanvas.height; 75 | 76 | this.drawContext = this.drawCanvas.getContext("2d"); 77 | this.outputContext = this.outputCanvas.getContext("2d"); 78 | this.outputImage = null; 79 | this.outputData = null; 80 | 81 | this.frames = []; 82 | this.times = []; 83 | 84 | this.frameNumber = 0; 85 | 86 | // Write an initial message to the outputCanvas 87 | this.outputContext.fillStyle = "red"; 88 | this.outputContext.font = "bold 16px Arial"; 89 | this.outputContext.fillText("Waiting for webcam feed...", 20, 28); 90 | 91 | this.webcam = new Webcam(); 92 | const { videoWidth, videoHeight } = await this.webcam.init(); 93 | 94 | for (const canvas of [this.snapshotCanvas, this.outputCanvas]) { 95 | canvas.width = videoWidth; 96 | canvas.height = videoHeight; 97 | } 98 | 99 | if ( 100 | this.drawCanvas.width != videoWidth || 101 | this.drawCanvas.height != videoHeight 102 | ) { 103 | this.drawCanvas.width = videoWidth; 104 | this.drawCanvas.height = videoHeight; 105 | $("#defaults").change(); 106 | } 107 | 108 | // Create output image and fill alpha channel 109 | this.outputImage = this.outputContext.getImageData( 110 | 0, 111 | 0, 112 | videoWidth, 113 | videoHeight 114 | ); 115 | this.outputData = this.outputImage.data; 116 | for (var i = 0; i < this.outputData.length; i += 4) { 117 | this.outputData[i + 3] = 255; 118 | } 119 | 120 | this.frame(); 121 | } 122 | 123 | frame() { 124 | var data = this.webcam.fetch(); 125 | var count = $("#frames").val(); 126 | var now = new Date().getTime() / 1000.0; 127 | 128 | this.times.push(now); 129 | this.frames.push(data); 130 | while (this.frames.length > count) { 131 | this.times.shift(); 132 | this.frames.shift(); 133 | } 134 | 135 | // quickly iterate over all pixels 136 | const drawData = this.drawContext.getImageData( 137 | 0, 138 | 0, 139 | this.width, 140 | this.height 141 | ).data; 142 | 143 | var dataLength = drawData.length; 144 | var lineLength = dataLength / this.height; 145 | 146 | var delay, outputPos, inputPos; 147 | 148 | var drawScale = (this.frames.length - 1) / 255.0; 149 | 150 | // Render the entire image line by line 151 | for ( 152 | var linePosition = 0; 153 | linePosition < dataLength; 154 | linePosition += lineLength 155 | ) { 156 | for (var i = 0; i < lineLength; i += 4) { 157 | // Flip the output for a mirror-like effect 158 | outputPos = linePosition + lineLength - i - 4; 159 | inputPos = linePosition + i; 160 | delay = Math.floor(drawData[outputPos] * drawScale); 161 | 162 | // Draw out all of the frames 163 | this.outputData[outputPos] = this.frames[delay][inputPos]; 164 | this.outputData[outputPos + 1] = this.frames[delay][inputPos + 1]; 165 | this.outputData[outputPos + 2] = this.frames[delay][inputPos + 2]; 166 | } 167 | } 168 | 169 | this.outputContext.putImageData(this.outputImage, 0, 0); 170 | 171 | // Update the status bar 172 | this.frameNumber += 1; 173 | $("#status-content").text( 174 | "Frame: " + 175 | this.frameNumber + 176 | "\n" + 177 | "Frames loaded: " + 178 | this.frames.length + 179 | "\n" + 180 | "Total delay: " + 181 | (now - this.times[0]).toFixed(1) 182 | ); 183 | setTimeout(() => this.frame(), 0); 184 | } 185 | } 186 | 187 | const app = new App(); 188 | 189 | $(function() { 190 | app 191 | .start() 192 | .then(() => { 193 | addOptions(); 194 | 195 | $("#fullscreen").click(() => { 196 | $("#output")[0].requestFullscreen(); 197 | }); 198 | 199 | $("#defaults") 200 | .change(function() { 201 | $(this) 202 | .find("option:selected") 203 | .data("callback")(); 204 | }) 205 | .keyup(function() { 206 | $(this).change(); 207 | }) 208 | .change(); 209 | console.log("Running"); 210 | }) 211 | .catch(err => { 212 | alert("Unable to start webcam: " + err.toString()); 213 | }); 214 | }); 215 | 216 | (function() { 217 | var brush = new Image(); 218 | brush.src = "brush.png"; 219 | 220 | var erase = new Image(); 221 | erase.src = "erase.png"; 222 | 223 | var image: HTMLImageElement = null; 224 | 225 | var $draw = $("#draw"); 226 | $draw.mousedown(function(ev) { 227 | if ((ev.which || 1) == 1) { 228 | image = brush; 229 | } else { 230 | image = erase; 231 | } 232 | }); 233 | $draw.mouseup(function() { 234 | image = null; 235 | }); 236 | $draw.mousemove(function(ev) { 237 | if (image) { 238 | var off = $draw.offset(); 239 | app.drawContext.drawImage( 240 | image, 241 | ev.pageX - off.left - image.width / 2, 242 | ev.pageY - off.top - image.height / 2 243 | ); 244 | } 245 | }); 246 | 247 | $draw.bind("contextmenu", function() { 248 | return false; 249 | }); 250 | })(); 251 | 252 | function addOptions() { 253 | var ctx = app.drawContext; 254 | 255 | var addDefault = function(caption, cb) { 256 | $("#defaults").append( 257 | $("