cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              /*!
 *
 * DATE: 2015-08-01
 *
 * @license Copyright (c) 2015, Sennep Ltd. All rights reserved.
 * 
 * @author: Christoph Lorenzi, christoph@sennep.com
 */

var TM = window.TM || {};

/* change the look */
TM.sliceWidth = 1;
TM.amplitude = 13;
TM.frequency = 129;
TM.amplitudeOffset = 1.5;
TM.message = 'slices';

/* constants */
TM.W = 500;
TM.H = 500;
TM.FONT_SIZE = 80;
TM.LOGO_RECT = {x:0,y:0,width:TM.W,height:TM.FONT_SIZE};


TM.init = function() {
	/* set-up canvas */
	TM.setUpCanvas();

	/* initialise logo animation */
	TM.SLICE_ANIM = new TM.SliceAnim();

	/* menu init */
  TM.initControls();	
};

TM.setUpCanvas = function() {

	/* set retina/ non-retina values */
	TM.LOGO_RECT.x = Math.round((TM.W-TM.LOGO_RECT.width)*0.5);
	TM.LOGO_RECT.y =  Math.round((TM.H-TM.LOGO_RECT.height)*0.5);

	//	sizing for retina/ non-retina
	var devicePixelRatio = window.devicePixelRatio || 1;
	TM.IS_RETINA = devicePixelRatio >= 1.5;

	/* set size */
	var canvas = window.document.getElementById("canvas"),
		ctx = canvas.getContext("2d"),
		backingStoreRatio = ctx.webkitBackingStorePixelRatio ||
							ctx.mozBackingStorePixelRatio ||
							ctx.msBackingStorePixelRatio ||
							ctx.oBackingStorePixelRatio ||
							ctx.backingStorePixelRatio || 1,
		ratio = devicePixelRatio / backingStoreRatio;
	canvas.width = TM.W;
	canvas.height = TM.H;

	//	width
	var oldw = canvas.width;
	canvas.width = oldw*ratio;
	canvas.style.width = oldw + 'px';
	//	height
	var oldh = canvas.height;
	canvas.height = oldh*ratio;
	canvas.style.height = oldh + 'px';
	// CRH.defaults.H = oldh;
	ctx.scale(ratio, ratio);

}


/*
*	Mouse controls
*/

TM.initControls = function() {

	$(document).mousemove(function(e){
		TM.onInput( e.pageX-$("#canvas").offset().left, e.pageY-$("#canvas").offset().top);
	});

}

TM.onInput = function(_x,_y) {
	if(!TM.SLICE_ANIM) return;
	var minX = -TM.LOGO_RECT.width + TM.LOGO_RECT.x,
		minY = -TM.LOGO_RECT.height + TM.LOGO_RECT.y
		maxX = 2 * TM.LOGO_RECT.width + TM.LOGO_RECT.x,
		maxY = 2 * TM.LOGO_RECT.height + TM.LOGO_RECT.y;
	if(_x>=minX && _x<=maxX && _y>=minY && _y<=maxY) {
		var px = (_x-TM.LOGO_RECT.x)/TM.LOGO_RECT.width;
		// TM.SLICE_ANIM.setUser();
		TM.SLICE_ANIM.userInput(px);
	} else {
		TM.SLICE_ANIM.userInput(0);
	}
};

/*
*	Window events
*/

$(window).ready(function(){
	TM.init();
});

// rAF implementation from http://paulirish.com/2011/requestanimationframe-for-smart-animating/
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
  return  window.requestAnimationFrame       ||
          window.webkitRequestAnimationFrame ||
          window.mozRequestAnimationFrame    ||
          function( callback ){
            window.setTimeout(callback, 1000 / 60);
          };
})();
;TM.SliceAnim = function(){
	var me = this;

	me.isPlaying = false;
	/* Constants */
	me.sliceWidth = TM.sliceWidth;
	//	wave
	me.amplitude = TM.amplitude;
	me.frequency = TM.frequency;
	me.amplitudeOffset = TM.amplitudeOffset;
	me.wavePosition = 1;
	/* Attributes */
	me.sets = [];
	//	Animation
	me.waveSpeed = 0.00013;
	/* images */
	me.canvas = 0;
	me.ctx = 0;
	me.ix = -1;

	/* Init */
	me.init = function() {
		me.canvas = document.getElementById("canvas");
		me.ctx = me.canvas.getContext("2d");

		setTimeout( function(){
			// me.refresh();
			me.start();
		},100);

	};

	//	create text buffer canvas
	//	basically drawing one line of text for each set/colour
	me.drawBuffer = function() {
		var f = TM.IS_RETINA ? 2 : 1,
			message = TM.message.toUpperCase(),
			colors = ["#FFFFFF","#b01cde","#e61d40","#f29613","#ffffd7"];
			buffer = document.createElement('canvas');

		buffer.width = 1000*f;
		buffer.height = TM.LOGO_RECT.height*colors.length*f;
		var tctx = buffer.getContext('2d');
		tctx.font = (TM.LOGO_RECT.height*f)+"px Arial Black, Gadget, sans-serif";


		TM.LOGO_RECT.width = Math.ceil(tctx.measureText(message).width/f);
		TM.LOGO_RECT.x = Math.round((TM.W-TM.LOGO_RECT.width)*0.5);

		for(var i=0, max=colors.length; i<max; i++) {
			tctx.fillStyle=colors[i];
			tctx.fillText(message, 0, -0.25*TM.LOGO_RECT.height+(i+1)*TM.LOGO_RECT.height*f);
		}

		return buffer;
	};

	//	create sets that handle a colour each
	me.createSets = function() {
		me.sets = [];

		TM.LOGO_SHEET = new Image();
		TM.LOGO_SHEET.src = me.drawBuffer().toDataURL();

		var amultiplier = 1;
		for(var i=0;i<5;i++) {
			var vo = {
				sliceWidth: me.sliceWidth, 
				wavePosition: me.wavePosition,
				waveSpeed: me.waveSpeed,
				frequency: me.frequency,
				amplitude: me.amplitude*amultiplier
			};
			var set = new TM.SliceSet(me.ctx,i,vo);
			me.sets.push(set);
			amultiplier *= me.amplitudeOffset;
		}
	};

	/* Controls */
	me.start = function() {
		if(me.isPlaying) return;
		me.isPlaying = true;

		me.createSets();
		me.tick();
	};

	me.userInput = function(_px) {
		me.ix = _px;
	};

	me.tick = function() {
		me.ctx.clearRect(0,0,TM.W,TM.H);

		for(var i=me.sets.length-1;i>=0;i--) {
			me.sets[i].tick(me.ix);
		}

		if(me.isPlaying) window.requestAnimFrame(me.tick.bind(this));
	};

	me.init();
};;TM.Slice = function(_img,_x,_w,_h,_rect) {
	var me = this;
	me.img = _img;
	me.rect = _rect;
	me.tx =_rect.x+TM.LOGO_RECT.x;
	me.ty = me.y = TM.LOGO_RECT.y;
	me.x = _x+TM.LOGO_RECT.x;
	me.amplitude = 0;
	me.height = _h;
	me.width = _w;
	
	me.update = function() {
		me.x += (me.tx-me.x)*0.4;
		me.y += (me.ty-me.y)*0.4;
	};

	me.draw = function(_ctx) {
		_ctx.drawImage(me.img,
			me.rect.x,me.rect.y,me.rect.width,me.rect.height,
			me.x,me.y,me.width,me.height);
	};
};;TM.SliceSet = function(_ctx,_setindex,_vo) {
	var me = this;
	/* attributes */
	me.slices = [];
	me.ctx = _ctx;
	me.setindex = _setindex;
	me.vo = _vo;
	me.frequency = me.vo.frequency;
	me.power = 0;
	me.powerEase = 0.1;

	me.init = function() {
		me.createSlices();
	};

	me.createSlices = function() {
		me.slices = [];
		var numslices = Math.ceil(TM.LOGO_RECT.width/me.vo.sliceWidth);
		for (var i = 0; i < numslices; i++) {

			var f = TM.IS_RETINA ? 2 : 1,
				srect = {
					x:i*f*me.vo.sliceWidth,
					y:me.setindex*f*TM.LOGO_RECT.height,
					width:f*me.vo.sliceWidth,
					height:f*TM.LOGO_RECT.height
				};

			me.slices.push( new TM.Slice( TM.LOGO_SHEET, i*me.vo.sliceWidth, me.vo.sliceWidth, TM.LOGO_RECT.height, srect) );
		}
	};

	me.tick = function(_ix) {
		var nums = me.slices.length;
		for(var i=0; i<nums; i++) {
			var slice = me.slices[i];

			if(_ix) {
				//	mouse input
				var inputDistance = Math.min(0.5, Math.abs(_ix-((i+1)/nums)))*2;
				var power = Easing.Circular.EaseInOut(1-inputDistance);
				slice.amplitude = power * me.vo.amplitude * me.power;
			} else {
				//	no mouse input
				slice.amplitude *= me.power;
			}

			var sinprogress = (((i+1)/395)+me.vo.wavePosition)%1;
			slice.ty = Math.sin(sinprogress*(Math.PI*2*me.frequency))*slice.amplitude + TM.LOGO_RECT.y;
			slice.tx = TM.LOGO_RECT.x+i*me.vo.sliceWidth;

			slice.update();
			slice.draw(me.ctx);
		}
		
		if(_ix) {
			me.vo.wavePosition += me.vo.waveSpeed;
			me.power += (1-me.power)*0.1;
		} else {
			me.power -= me.power*me.powerEase;
		}
	};

	me.init();
};;/**
 * easing equations taken from sole's Tween.js
 * originally adapted from Robert Penner's equations
 *
 * @author sole / http://soledadpenades.com
 * @author mr.doob / http://mrdoob.com
 * @author Robert Eisele / http://www.xarg.org
 * @author Philippe / http://philippe.elsass.me
 * @author Robert Penner / http://www.robertpenner.com/easing_terms_of_use.html
 */


Easing = { Circular: {} };


Easing.Circular.EaseInOut = function ( k ) {

	if ( ( k /= 0.5 ) < 1) return - 0.5 * ( Math.sqrt( 1 - k * k) - 1);
	return 0.5 * ( Math.sqrt( 1 - ( k -= 2) * k) + 1);

};

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console