Skip to content

Instantly share code, notes, and snippets.

//https://stackoverflow.com/a/52357595/2690846
// usage: let frames = await extractFramesFromVideo("https://example.com/video.webm");
async extractFramesFromVideo(videoUrl: string, fps=3) {
return new Promise(async (resolve) => {
// fully download it first (no buffering):
let videoBlob = await fetch(videoUrl).then(r => r.blob());
let videoObjectUrl = URL.createObjectURL(videoBlob);
let video = document.createElement('video');
@schmidtsonian
schmidtsonian / load-video.js
Created April 12, 2019 20:48
Creates video and load it
/**
* Creates video and load it
* @param {Object} videoAttrs - Video element attributes
* @param {Object} sourceAttrs - Source element attributes
* @returns {Promise} {elm: video}
* @example
* loadVideo({autoPlay: false, controls: true}, {src: 'foo.webm', type: 'video/mp4'})
* .then(obj => console.log(obj.elem));
*/
export function loadVideo(videoAttrs, sourceAttrs) {
@schmidtsonian
schmidtsonian / load-image.js
Created April 12, 2019 20:47
Creates and loads a single image
/**
* Creates and loads a single image
* @param {Object} attrs - Image element attributes
* @returns {Promise} {elm: img}
* @example
* loadImage({ src: 'foo.jpg', alt: 'foo' })
* .then(obj => console.log(obj.elem));
*/
export function loadImage(attrs) {
@schmidtsonian
schmidtsonian / watcher.js
Created April 12, 2019 14:34
JavaScript Singleton watcher utility
// Events class is here: https://gist.github.com/schmidtsonian/8a023034a2f5600b455afd2e2aed7019
import Events from './events';
/**
* Watcher
*
* @class Watcher
*/
export default class Watcher {
@schmidtsonian
schmidtsonian / events.js
Created April 12, 2019 14:22
A super-basic Javascript (publish subscribe) pattern
/**
* A super-basic Javascript (publish subscribe) pattern
* https://www.youtube.com/watch?v=nQRXi1SVOow
* https://gist.github.com/learncodeacademy/777349747d8382bfb722
*
* @export
* @class Events
*/
export default class Events {
@schmidtsonian
schmidtsonian / 0_reuse_code.js
Last active August 29, 2015 14:11
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
loader = {
// arreglo con ruta de las imágenes
images: [],
// Se ejecuta al cargar las imágenes
onComplete: function(){},
init: function(){
var self = this, count = 0;
@schmidtsonian
schmidtsonian / wordWrap.js
Last active August 29, 2015 13:58
Find character and wrap the word(s) containing it
/**
*
* Verifica si existe la funcion, sino la crea (solo existe en firefox)
*
**/
if(!('contains' in String.prototype)){
String.prototype.contains = function( str, startIndex ) {
return -1 !== String.prototype.indexOf.call( this, str, startIndex );
};
};
@schmidtsonian
schmidtsonian / truncate.js
Created April 2, 2014 18:07
Truncate text
String.prototype.trunc =
function( n, useWordBoundary ){
var toLong = this.length > n,
s_ = toLong ? this.substr(0,n-1) : this;
s_ = useWordBoundary && toLong ? s_.substr( 0, s_.lastIndexOf( ' ' ) ) : s_;
return toLong ? s_ + ' ...' : s_;
};
var s = "this is a text"
s.trunc( "6", true ); // "this ..."
@schmidtsonian
schmidtsonian / youtube.js
Last active August 29, 2015 13:57
Get latest post youtube
var Youtube = (function() {
'use strict';
function Youtube( args ) {
// enforces new
if (!(this instanceof Youtube)) {
return new Youtube( args );
}
this.settings = {