Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

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.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

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.

+ add another resource

Packages

Add Packages

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.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="container hasWedgeTop">
	<img class="canvasify" data-image-frame="wedgeTop" data-defaults-wedge-height="100" src="http://placekitten.com/1280/500" />
	<h1>Kitty with a clipped top</h1>
	<p>A regular image with a <code>wedgeTop</code> clipping. It looks weird because it has nothing to overlap. This would be a good case for clipping from the bottom, rather than the top.</p>
</div>



<div class="container hasWedgeTop hasWedgeBottom">
	<video controls muted class="canvasify" loop autoplay data-image-frame="wedgeBottom">
	 <source src="https://poc5.ssl.cdn.sdlmedia.com/web/635663565028367012PU.mp4">
	</video>
	<h1>Video clipping</h1>
	<p>This is a <code>wedgeTop</code> and <code>wedgeBottomReverse</code>clipping. It overlaps the one above.</p>
</div>
<div class="container ">
	<img class="canvasify"  src="http://placekitten.com/1280/600" />
	<h1>No Clip Kitty!</h1>
	<p>Here's an example where we don't do any clipping at all. </p>
</div>

<div class="container hasWedgeTop hasWedgeBottom">
	<img class="canvasify" data-image-frame="wedgeTop, wedgeBottom,wedgeBottomReverse" src="http://placekitten.com/g/1280/500" />
	<h1>Gray kitty with many clips</h1>
	<p><code>wedgeTop</code>, <code>wedgeBottom</code>, <code>wedgeBottomReverse</code>. It overlaps the top and also sets the bottom to overlap whatever's after. </p>
</div>
<div class="container ">
	<img class="canvasify" src="http://placebear.com/1280/500" />
	<h1>No Clippings for This Bear!</h1>
	<p>A regular image. There's no clippings here at all. But you'd think there were, because of the top and bottom image. If you don't see a bear, resize the screen really quick. It'll show up. </p>
</div>

<div class="container hasWedgeTop hasWedgeBottom">
	<img class="canvasify" data-image-frame="wedgeTopReverse, wedgeBottom" src="http://placebear.com/1280/700" />
	<h1>Top and Bottom Clipped bear</h1>
	<p><code>WedgeTopReverse</code> and <code>wedgeBottom</code> for this bad bear </p>
</div>
<div class="container hasWedgeTop">
	<img class="canvasify" data-image-frame="wedgeTop" src="http://placebear.com/g/1400/600" />
	<h1>Top Clipped bear</h1>
	<p>Items at the end of the page should only be clipped on top. So the ultimate ursidae is unaffected by bottom clippings. e.g.: <code>wedgeTop</code>.</p>
</div>



              
            
!

CSS

              
                body {
	background-color: #333;
	font-family: arial;
}


.container {
	height: 400px;
	width: 100%;
	position: relative;
	display: flex;
	flex-direction: column;
	justify-content: center;
}

.hasWedgeTop {
	margin-top: -50px;
	z-index: -2;
}

.hasWedgeTop:first-child {
	margin-top: 0;
}

.hasWedgeTop + * {
	margin-bottom: 50px;
}

.hasWedgeBottom {
	z-index: 1;
	margin-bottom: -50px;
}

.hasWedgeBottom + .hasWedgeTop {
	z-index: 1;
}

.hasWedgeBottom + * {
	top:0px
}

.canvasify {
	position:absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	width: 100%;
	z-index: -1;
}
video {
	width: 100%;
	max-height: 400px;
}
h1, p {
	padding: .5em;
	width: 22em;
}

h1 {
	font-size: 2em;
	color: rgb(125,150,225);
	background-color: rgba(30,30,30,.6);
}
p {
	color: rgb(125,150,225);
	background-color: rgba(30,30,30,.6);
}
              
            
!

JS

              
                const imageFrames =  function () {
	const defaults = {
		wedgeHeight: 50
	};
	
};

class ImageCanvasModule  {
	constructor(module) {
		this.originalElement = module;
		this.defaults = {
			wedgeHeight: 50
		};
		if(this.isVideo) {
			/*drawFrame has a check where it'll only draw on reqAnimationFrame if video.paused === false,
			so we need to fire drawFrame on both events because that boolean will be false when it's paused, thus cancelling the animation frame
			*/
			this.media.addEventListener('play', ()=>{
				this.drawOnCanvas();
			});

			this.media.addEventListener('pause', ()=> {
				this.drawOnCanvas();
			});
		}
	}

	get imageFrames() {
return {

		//all wedges draw paths clockwise: top right, bottom right, bottom left, top left
		wedgeTop: (wedgeHeight = this.defaults.wedgeHeight) => {
			var wedge = new Path2D();

			wedge.moveTo(this.dimensions.width, 0);
			wedge.lineTo(this.dimensions.width, this.dimensions.height);
			wedge.lineTo(0, this.dimensions.height);
			wedge.lineTo(0, wedgeHeight);
			wedge.closePath();
			this.frameContext.clip(wedge);
		},

		wedgeTopReverse: (wedgeHeight = this.defaults.wedgeHeight) => {
			var wedge = new Path2D();

			wedge.moveTo(this.dimensions.width, wedgeHeight);
			wedge.lineTo(this.dimensions.width, this.dimensions.height);
			wedge.lineTo(0, this.dimensions.height);
			wedge.lineTo(0, 0);
			wedge.closePath();
			this.frameContext.clip(wedge);

		},

		wedgeBottom: (wedgeHeight = this.defaults.wedgeHeight) => {
			const wedge = new Path2D();

			wedge.moveTo(this.dimensions.width, 0);
			wedge.lineTo(this.dimensions.width, this.dimensions.height - wedgeHeight);
			wedge.lineTo(0, this.dimensions.height);
			wedge.lineTo(0,0);
			wedge.lineTo(this.dimensions.width,0);
			wedge.closePath();
			this.frameContext.clip(wedge);
		},

		wedgeBottomReverse: (wedgeHeight = this.defaults.wedgeHeight) => {
			const wedge = new Path2D();

			wedge.moveTo(this.dimensions.width, 0);
			wedge.lineTo(this.dimensions.width, this.dimensions.height);
			wedge.lineTo(0, this.dimensions.height - wedgeHeight);
			wedge.lineTo(0, 0);
			wedge.closePath();
			this.frameContext.clip(wedge);
		}
	};
	}
	get isPicture() {
		return (this.originalElement.nodeName === 'PICTURE');
	}

	get isVideo() {
		return (this.originalElement.nodeName === 'VIDEO');
	}

	get isFitParent() {
		return this.originalElement.dataset.hasOwnProperty('fitparent');
	}

	get media() {
		return this.isPicture ? this.originalElement.querySelector('img') : this.originalElement;
	}

	get hasMatchMedia() {
		return (this.originalElement.dataset.matchmedia && this.originalElement.dataset.matchmedia.length > 1) ;
	}

	get matchMediaResult() {
		return window.matchMedia(this.originalElement.dataset.matchmedia).matches;
	}

	get context() {
		return this.canvas.getContext('2d');
	}

	get dimensions() {
		return {
			width: this.originalElement.offsetWidth,
			height: this.originalElement.offsetHeight
		};
	}

	getPictureFillSrc() {
		const style = this.media.currentStyle || window.getComputedStyle(this.media, false); // #IE11-2
		return style.backgroundImage.slice(4, -1).replace(/"/g,'');
	}

	getPictureFillMedia(src) {
		const img = new Image(this.dimensions.width, this.dimensions.height); // #IE11-3

		img.src = src;
		return img;
	}

	createCanvas () {
		let canvas = document.createElement('canvas');
		let frameCanvas = document.createElement('canvas');


		this.originalElement.parentNode.insertBefore(canvas, this.originalElement.nextSibling);
		canvas.className = this.originalElement.className;

		this.canvas = canvas;
		this.frameCanvas = frameCanvas;
		this.frameContext = this.frameCanvas.getContext('2d');
	}

	sizeParent () {
		if (this.hasMatchMedia && this.matchMediaResult ) {
			this.originalElement.parentElement.style.height = `${this.dimensions.height}px` ;
		}
		if (this.hasMatchMedia && !this.matchMediaResult) {
			this.originalElement.parentElement.style.height = null;
		}
	}

	sizeCanvas () {
		this.canvas.height = this.dimensions.height;
		this.canvas.width = this.dimensions.width;

		this.frameCanvas.height = this.dimensions.height;
		this.frameCanvas.width = this.dimensions.width;

		if (this.isFitParent) this.sizeParent();
	}

	drawFrame (media) { //#IE11-4 
		if (this.isVideo && media.paused) return false;
		if (this.hasMatchMedia && !this.matchMediaResult ) return false;

		let imageFrames = this.originalElement.dataset.imageFrame.split(',');

		imageFrames.forEach((imageFrame)=>{
			imageFrame = imageFrame.replace(/\s/g, '');
			if (imageFrame in this.imageFrames) this.imageFrames[imageFrame]();
			this.frameContext.globalCompositeOperation = 'copy';

		});

		var _this = this;
		var toggle = false; // toggle is used b/c we don't need 60fps. 30 will do.

		(function loop() {
			toggle= !toggle;
			/*Firefox and IE will show DOMException error b/c video hasn't fully loaded. Video will still play, though. so only start drawing when width > 0 */
			if (toggle && _this.dimensions.width > 0) {

				let x = 0;
				let width = _this.media.offsetWidth;
				let y = 0;

			_this.frameContext.drawImage(media, x, y, width, _this.canvas.height);
			_this.context.drawImage(_this.frameCanvas, x, y, width, _this.canvas.height);
			}
			_this.context.globalCompositeOperation = 'source-atop'; //smooths out jagged edges
			if (_this.isVideo) {
				window.requestAnimationFrame(loop);
			}

		})(); // this is an IIFE so that it will execute at least once
	}

	drawOnCanvas (media = this.media) { // #IE11-4 
		this.sizeCanvas();
		this.drawFrame(media);
	}

	hideOriginal () {
		//don't use display: none .... you can't get image dimensions when you do that.
		this.originalElement.style.opacity = 0;
	}

	initImageFrame (media = this.media) { // #IE11-4 
		this.drawOnCanvas(media);
		this.hideOriginal();
	}

}

// stuff unrelated to module
console.clear();

window.addEventListener('DOMContentLoaded', ()=> {
	var els = document.querySelectorAll('.canvasify');
	window.canvasified = [];

	// loop through and apply methods. I prefer `for of` but even with Babel, IE doesn't like it. #ThanksIE
	for (let i = 0; i < els.length; i++) {
		let el = els[i];
		
		// don't go applying anything unless there's an imageFrame attribute
		if (el.dataset.imageFrame) {
			let imageModule = new ImageCanvasModule(el);
			console.log(imageModule.originalElement.dataset);
			if (imageModule.originalElement.dataset.defaultsWedgeHeight) {
				imageModule.defaults.wedgeHeight = imageModule.originalElement.dataset.defaultsWedgeHeight;
			}
			
			imageModule.createCanvas();
			imageModule.initImageFrame();
			window.canvasified.push(imageModule);
	
			window.addEventListener('resize', ()=> {
				imageModule.drawOnCanvas(imageModule.media);
			})
		}

	}
	console.log(canvasified);
});
              
            
!
999px

Console