HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URL's added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by Skypack, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ES6 import
usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<video playsinline muted autoplay loop></video>
<!--
Notes!
- seeThru.js https://github.com/m90/seeThru
- playsinline + muted added to allow inline autoplay on iOS
- video grand thefted from https://jakearchibald.com/scratch/alphavid/
-->
canvas { width: 100%; }
body { background-color: #ccc; }
/**
* jQuery seeThru - transparent HTML5 video - written by Frederik Ring (frederik.ring@gmail.com)
* based on http://jakearchibald.com/scratch/alphavid/ by Jake Archibald (jaffathecake@gmail.com)
* Copyright (c) 2015 Frederik Ring
* 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:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
* 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.
* see https://github.com/m90/seeThru for documentation
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
root.seeThru = factory();
}
})(this, function () {
/**
* convert an image's alpha channel into a black & white canvasPixelArray
* @param {DOMElement} maskObj
* @param {Object} dimensions
* @returns {CanvasPixelArray} RGBA
*/
function convertAlphaMask (maskObj, dimensions) {
var convertCanvas = document.createElement('canvas');
var convertCtx = convertCanvas.getContext('2d');
var RGBA;
convertCanvas.width = dimensions.width;
convertCanvas.height = dimensions.height;
convertCtx.drawImage(maskObj, 0, 0, dimensions.width, dimensions.height);
RGBA = convertCtx.getImageData(0, 0, dimensions.width, dimensions.height);
//alpha data is on each 4th position -> [0+(4*n)] => R, [1+(4*n)] => G, [2+(4*n)] => B, [3+(4*n)] => A
for (var i = 3, len = RGBA.data.length; i < len; i = i + 4) {
RGBA.data[i - 1] = RGBA.data[i - 2] = RGBA.data[i - 3] = RGBA.data[i]; //alpha into RGB
RGBA.data[i] = 255; //alpha is always 100% opaque
}
return RGBA;
}
/**
* unmultiply an image with alpha information
* @param {Array} rgb - canvasPixelArray representing the image to be unmultiplied
* @param {Array} alphaData - canvasPixelArray representing the alpha to use
* @returns {Array} rgb
*/
function unmultiply (rgb, alphaData) {
for (var i = 3, len = rgb.data.length; i < len; i = i + 4) {
rgb.data[i] = alphaData[i - 1]; //copy B value into A channel
rgb.data[i - 3] = rgb.data[i - 3] / (alphaData[i - 1] ? (alphaData[i - 1] / 255) : 1); //un-premultiply B
rgb.data[i - 2] = rgb.data[i - 2] / (alphaData[i - 1] ? (alphaData[i - 1] / 255) : 1); //un-premultiply G
rgb.data[i - 1] = rgb.data[i - 1] / (alphaData[i - 1] ? (alphaData[i - 1] / 255) : 1); //un-premultiply R
}
return rgb;
}
/**
* gets a prefixed rAF version or a polyfill to use if window.requestAnimationFrame is not available
* @returns {Function}
*/
function getRequestAnimationFrame () {
var lastTime = 0;
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length; x++) {
if (window[vendors[x] + 'RequestAnimationFrame']) { return window[vendors[x] + 'RequestAnimationFrame']; }
}
return function (callback) {
var currTime = new Date().getTime();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () {
callback(currTime + timeToCall);
}, timeToCall);
lastTime = currTime + timeToCall;
return id;
};
}
/**
* gets a prefixed cAF version or a polyfill to use if window.cancelAnimationFrame is not available
* @returns {Function}
*/
function getCancelAnimationFrame () {
var vendors = ['ms', 'moz', 'webkit', 'o'];
for (var x = 0; x < vendors.length; x++) {
if (window[vendors[x] + 'CancelAnimationFrame']) { return window[vendors[x] + 'CancelAnimationFrame']; }
if (window[vendors[x] + 'CancelRequestAnimationFrame']) { return window[vendors[x] + 'CancelRequestAnimationFrame']; }
}
return function (id) { clearTimeout(id); };
}
/**
* turn array like object into real array
* @param {Object} el
* @returns {Array}
*/
function slice (el) {
return [].slice.call(el);
}
/**
* check [[Class]] by calling {}.toString on any object
* @param {Object} el
* @returns {String}
*/
function toString (el) {
return Object.prototype.toString.call(el);
}
/**
* insert a DOM Node after another
* @param {DOMElement} node
* @param {DOMElement} after
*/
function insertAfter (node, after) {
if (after.nextSibling) {
after.parentNode.insertBefore(node, after.nextSibling);
} else {
after.parentNode.appendChild(node);
}
}
/**
* return a DOM Node matching variable input
* input might be a DOMElement, a DOMCollection or a string
* @param input
* @returns DOMElement
*/
function getNode (input) {
if (input.tagName) {
return input;
} else if (toString(input) === '[object String]') {
return document.querySelector(input);
} else if (input.length) {
return input[0];
} else {
return null;
}
}
/**
* serialize an object into a string of CSS to use for style.cssText
* @param {Object} obj
* @returns {String}
*/
function cssObjectToString (obj) {
var res = [];
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
res.push(prop + ': ' + obj[prop] + ';');
}
}
return res.join('');
}
/**
* make the script available as a plugin on the passed jQuery instance
* @param {jQuery} $
*/
function attachSelfAsPlugin ($) {
if (!$.fn || $.fn.seeThru) { return; }
$.fn.seeThru = function () {
var args = slice(arguments);
return this.each(function () {
var $this = $(this);
if (!args.length || (args.length === 1 && toString(args[0]) === '[object Object]')) {
if ($this.data('seeThru')) { return; }
$this.data('seeThru', new SeeThru(this, args[0])._init());
} else if (args.length && toString(args[0]) === '[object String]') {
if (!$this.data('seeThru')) { return; }
// all methods other then init will be deferred until `.ready()`
$this.data('seeThru').ready(function () {
$this.data('seeThru')[args[0]](args[1]);
if (args[0] === 'revert') {
$this.data('seeThru', null);
}
});
}
});
};
}
/**
* @constructor Store
* simple wrapper around [] to keep track of video elements that are currently
* handled by the script
*/
function Store () {
var elements = [];
this.push = function (el) {
if (el) {
elements.push(el);
return el;
} else {
return null;
}
};
this.has = function (el) {
return elements.some(function (video) {
return video === el;
});
};
this.remove = function (el) {
elements = elements.filter(function (video) {
return video !== el;
});
};
}
/**
* @constructor TransparentVideo
* handles the transformation of a video into the canvas elements and keeps track
* of intervals and animation loop logic
* @param {DOMElement} video
* @param {Object options}
*/
function TransparentVideo (video, options) {
var initialDisplayProp;
var divisor = options.mask ? 1 : 2; //static alpha data will not cut the image dimensions
var dimensions = { // calculate dimensions
width: parseInt(options.width, 10),
height: parseInt(options.height, 10)
};
var bufferCanvas = document.createElement('canvas');
var buffer = bufferCanvas.getContext('2d');
var displayCanvas = document.createElement('canvas');
var display = displayCanvas.getContext('2d');
var posterframe;
var interval;
var requestAnimationFrame = window.requestAnimationFrame || getRequestAnimationFrame();
var cancelAnimationFrame = window.cancelAnimationFrame || getCancelAnimationFrame();
var lastDrawnFrameTime = null;
var drawFrame = function (recurse) {
var image, alphaData, i, len, currentFrameTime = video.currentTime;
if (lastDrawnFrameTime !== currentFrameTime && video.readyState > 1) {
lastDrawnFrameTime = currentFrameTime;
buffer.drawImage(video, 0, 0, dimensions.width, dimensions.height * divisor); //scales if <video>-dimensions are not matching
image = buffer.getImageData(0, 0, dimensions.width, dimensions.height);
alphaData = buffer.getImageData(0, dimensions.height, dimensions.width, dimensions.height).data; //grab from video;
if (options.unmult) unmultiply(image, alphaData);
//calculate luminance from buffer part, no weighting needed when alpha mask is used
for (i = 3, len = image.data.length; i < len; i = i + 4) {
image.data[i] = options.alphaMask ? alphaData[i - 1] : Math.max(alphaData[i - 1], alphaData[i - 2], alphaData[i - 3]);
}
display.putImageData(image, 0, 0, 0, 0, dimensions.width, dimensions.height);
}
if (recurse) {
interval = requestAnimationFrame(function () {
drawFrame(true);
});
}
};
var drawStaticMask = function (node) {
if (node.tagName !== 'IMG') throw new Error('Cannot use non-image element as mask!');
node.width = dimensions.width;
node.height = dimensions.height; //adjust image dimensions to video dimensions
if (options.alphaMask) { //alpha channel has to be converted into RGB
buffer.putImageData(convertAlphaMask(node, dimensions), 0, dimensions.height);
} else { //no conversion needed, draw image into buffer
buffer.drawImage(node, 0, dimensions.height, dimensions.width, dimensions.height);
}
node.style.display = 'none';
};
/**
* @method startRendering
* kick off the animation loop
* @returns self
* @API public
*/
this.startRendering = function () {
drawFrame(true);
return this;
};
/**
* @method stopRendering
* ends the animation loop
* @returns self
* @API public
*/
this.stopRendering = function () {
cancelAnimationFrame(interval);
return this;
};
/**
* @method stopRendering
* ends the animation loop, removes the canvas elements and unhides the video
* @returns self
* @API public
*/
this.teardown = function () {
cancelAnimationFrame(interval);
video.parentNode.removeChild(video.nextSibling);
video.parentNode.removeChild(video.nextSibling);
video.style.display = initialDisplayProp;
return this;
};
/**
* @method updateMask
* draws a new image onto the alpha portion of the buffer
* @param {DOMElement} node
* @returns self
* @API public
*/
this.updateMask = function (node) {
drawStaticMask(node);
return this;
};
/**
* @method getCanvas
* gets the visible canvas element that is used for display
* @returns {DOMElement}
* @API public
*/
this.getCanvas = function () {
return displayCanvas;
};
/**
* @method getPoster
* gets the posterframe element
* @returns {DOMElement}
* @API public
*/
this.getPoster = function () {
return posterframe;
};
var elementDimensions = video.getBoundingClientRect();
if (!dimensions.height || !dimensions.width) { //we need to find out at least one dimension parameter as it is not explicitly set
if (!video.width && !video.height) { //<video> has no width- or height-attribute -> source dimensions from video source meta
dimensions.width = dimensions.width || video.videoWidth;
dimensions.height = dimensions.height || video.videoHeight / divisor;
} else if (!video.height) { //<video> has no height-attribute -> source dimensions from video source meta
dimensions.width = dimensions.width || elementDimensions.width;
dimensions.height = dimensions.height || elementDimensions.width / (video.videoWidth / Math.floor(video.videoHeight / divisor));
} else if (!video.width) { //<video> has no height-attribute -> source dimensions from video source meta
dimensions.width = dimensions.width || elementDimensions.height * (video.videoWidth / Math.floor(video.videoHeight / divisor));
dimensions.height = dimensions.height || elementDimensions.height;
} else { //get values from height and width attributes of <video>
dimensions.width = dimensions.width || elementDimensions.width;
dimensions.height = dimensions.height || elementDimensions.height / divisor;
}
}
bufferCanvas.width = dimensions.width;
bufferCanvas.height = dimensions.height * 2;
bufferCanvas.style.display = 'none';
bufferCanvas.className = 'seeThru-buffer';
displayCanvas.width = dimensions.width;
displayCanvas.height = dimensions.height;
displayCanvas.className = 'seeThru-display';
insertAfter(bufferCanvas, video);
insertAfter(displayCanvas, video);
// draw static mask if needed
if (options.mask) drawStaticMask(getNode(options.mask));
// append "posterframe" if option is set and attribute is present on the video
if (options.poster && video.poster) {
posterframe = document.createElement('div');
posterframe.className = 'seeThru-poster';
posterframe.style.cssText = cssObjectToString({
width: dimensions.width + 'px',
height: dimensions.height + 'px',
position: 'absolute',
top: 0,
left: 0,
'background-size': 'cover',
'background-position': 'center',
'background-image': 'url("' + video.poster + '")'
});
insertAfter(posterframe, video);
}
initialDisplayProp = window.getComputedStyle(video).display;
video.style.display = 'none';
if (options.start === 'autoplay') { video.play(); }
}
/**
* @constructor SeeThru
* handles a video element turned into a transparent mock version of itself
* @param {String|DOMElement|DOMNode} DOMNode
* @param {Object} [options]
*/
function SeeThru (DOMNode, options) {
var self = this;
var ready = false;
var callbacks = [];
var defaultOptions = {
start: 'autoplay', //'autoplay', 'clicktoplay', 'external' (will display the first frame and make the video wait for an external interface) - defaults to autoplay
end: 'loop', //'loop', 'rewind', 'stop' any other input will default to 'loop'
mask: false, //this lets you define a <img> (selected by #id or .class - class will use the first occurence)used as a black and white mask instead of adding the alpha to the video
alphaMask: false, //defines if the used `mask` uses black and white or alpha information - defaults to false, i.e. black and white
width: null, //lets you specify a pixel value used as width -- overrides all other calculations
height: null, //lets you specify a pixel value used as height -- overrides all other calculations
poster: false, // the plugin will display the image set in the video's poster-attribute when not playing if set to true
unmult: false, //set this to true if your video material is premultiplied on black - might cause performance issues
};
var canConstructEvents = (function () {
try {
if (new Event('submit', { bubbles: false }).bubbles !== false) {
return false;
} else if (new Event('submit', { bubbles: true }).bubbles !== true) {
return false;
} else {
return true;
}
} catch (e) {
return false;
}
})();
var eventsToEcho = [
'mouseenter',
'mouseleave',
'click',
'mousedown',
'mouseup',
'mousemove',
'mouseover',
'hover',
'dblclick',
'contextmenu',
'focus',
'blur'
];
options = options || {};
this._video = getNode(DOMNode);
if (!this._video || this._video.tagName !== 'VIDEO') throw new Error('Could not use specified source');
this._options = (function (options) {
for (var key in defaultOptions) {
if (defaultOptions.hasOwnProperty(key)) {
if (!(key in options)) {
options[key] = defaultOptions[key];
}
}
}
return options;
})(options);
/**
* @method init
* sets up transparent video once the video has metadata
* @returns self
* @API private
*/
this._init = function () {
var runInit = function () {
function playSelfAndUnbind () {
self._video.play();
if (self._options.poster) {
self._seeThru.getPoster().removeEventListener('click', playSelfAndUnbind);
} else {
self._seeThru.getCanvas().removeEventListener('click', playSelfAndUnbind);
}
}
if (elementStore.has(this._video)) { throw new Error('seeThru already initialized on passed video element!'); }
this._seeThru = new TransparentVideo(this._video, this._options);
// attach behavior for start options
if (this._options.start === 'clicktoplay') {
if (this._options.poster) {
this._seeThru.getPoster().addEventListener('click', playSelfAndUnbind);
} else {
this._seeThru.getCanvas().addEventListener('click', playSelfAndUnbind);
}
} else if (this._options.start === 'autoplay' && options.poster) {
this._seeThru.getPoster().style.display = 'none';
}
// attach behavior for end options
if (this._options.end === 'rewind') {
this._video.addEventListener('ended', function () {
self._video.currentTime = 0;
self._seeThru.getCanvas().addEventListener('click', playSelfAndUnbind);
});
} else if (this._options.end !== 'stop') {
this._video.addEventListener('ended', function () {
self._video.currentTime = 0;
self._video.play();
});
}
// attach behavior for posterframe option
if (this._options.poster && this._video.poster) {
this._video.addEventListener('play', function () {
self._seeThru.getPoster().style.display = 'none';
});
this._video.addEventListener('pause', function () {
self._seeThru.getPoster().style.display = 'block';
});
}
// some events that are registered on the canvas representation
// will be echoed on the original video element so that pre-existing
// event handlers bound to the video element will keep working
// it is recommended to use `.getCanvas()` to bind new behavior though
eventsToEcho.forEach(function (eventName) {
self._seeThru.getCanvas().addEventListener(eventName, function () {
var evt;
if (canConstructEvents) {
evt = new Event(eventName);
} else {
evt = document.createEvent('Event');
evt.initEvent(eventName, true, true);
}
self._video.dispatchEvent(evt);
});
});
this._seeThru.startRendering();
ready = true;
elementStore.push(this._video);
callbacks.forEach(function (cb) { cb(self, self._video, self.getCanvas()); });
}.bind(this);
if (this._video.readyState > 0) {
runInit();
} else {
this._video.addEventListener('loadedmetadata', function () {
runInit();
});
}
return this;
};
/**
* @method getCanvas
* returns the canvas element that is used for on-screen displaylay
* @returns {DOMElement}
* @API public
*/
this.getCanvas = function () {
return this._seeThru.getCanvas();
};
/**
* @method play
* starts playback of the associated video element
* @returns self
* @API public
*/
this.play = function () {
this._video.play();
return this;
};
/**
* @method pause
* halts playback of the associated video element
* @returns self
* @API public
*/
this.pause = function () {
this._video.pause();
return this;
};
/**
* @method revert
* reverts the transparent video back to its original state
* @API public
*/
this.revert = function () {
this._seeThru.teardown();
elementStore.remove(this._video);
};
/**
* @method updateMask
* @param {String|DOMElement|DOMCollection} mask
* swaps the static mask currently used for sourcing alpha
* @returns self
* @API public
*/
this.updateMask = function (mask) {
this._seeThru.updateMask(getNode(mask));
return this;
};
/**
* @method ready
* @param {Function} cb
* defers the passed callback until the associated video element has metadata
* passes itself as the 1st argument of the callback
* @returns self
* @API public
*/
this.ready = function (cb) {
if (ready) {
setTimeout(function () {
cb(self, self._video, self.getCanvas());
}, 0);
} else {
callbacks.push(cb);
}
return this;
};
}
// if we have a global version of jQuery we'll automatically attach the script as a plugin
if (window.jQuery) attachSelfAsPlugin(window.jQuery);
var elementStore = new Store();
return {
create: function (DOMCollection, options) {
return new SeeThru(DOMCollection, options)._init();
},
attach: attachSelfAsPlugin
};
});
// ==================================================
// Using it
// <video playsinline muted autoplay loop></video>
// ==================================================
const video = document.querySelector('video');
video.addEventListener('loadedmetadata', ()=> {
seeThru.create(video);
});
loadAsObjectURL(video, 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/68939/compressed.mp4');
function loadAsObjectURL(video, url){
const xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = (response) => video.src = URL.createObjectURL(xhr.response);
xhr.onerror = () => { /* Houston we have a problem */};
xhr.open('GET', url, true);
xhr.send();
video.onload = () => URL.revokeObjectURL(video.src);
}
Also see: Tab Triggers