Extend Guide

KUTE.js is a very flexible animation engine that allows you to extend beyond measure. In this tutorial we'll dig into what's best to do to extend/customize the functionality of KUTE.js core, plugins and features.

Basic Plugin Template

The best way to extend, no matter what you would like to achieve is to use a specific closure, here's an example:

/* KUTE.js - The Light Tweening Engine
 * by dnp_theme
 * package - pluginName
 * desc - what your plugin does
 * pluginName by yourNickname aka YOUR NAME
 * Licensed under MIT-License
 */

(function (factory) {
  if (typeof define === 'function' && define.amd) {
    define(["./kute.js"], function(KUTE){ factory(KUTE); return KUTE; });
  } else if(typeof module == "object" && typeof require == "function") {
    var KUTE = require("./kute.js");   
    module.exports = factory(KUTE);
  } else if ( typeof window.KUTE !== 'undefined' ) {		
    factory(KUTE);
  } else {
    throw new Error("pluginName require KUTE.js.");
  }
}( function (KUTE) {
    // your code goes here
    
    // in this function body
    
    // the plugin returns this
    return this;
}));

As suggested in the above template, your function body could be written with one or more of the examples below.

Extend Tween Control

In some cases, you may want to extend with additional tween control methods, so before you do that, make sure to check KUTE.Tween function to get the internal references notation:

//add a reference to KUTE object
var K = window.KUTE;

// let's add a timescale method
K.Tween.prototype.timescale = function(factor){
    this._dr *= factor; // this._dr is the internal tween duration
    return this;
}

// or let's add a reverse method
K.Tween.prototype.reverse = function(){
    for (var p in this._vE) {
        var tmp = this._vSR[p];         // we cache this object first
        this._vSR[p] = this._vE[p];     // this._vSR is the internal valuesStartRepeat object
        this._vE[p] = tmp;              // this._vE is the internal valuesEnd object
        this._vS[p] = this._vSR[p];     // this._vSR is the internal valuesStart object
    }
    return this;
}

// go back in time
K.Tween.prototype.seek = function (time) {
    this._sT -= time;  // this._sT is the startTime
    return this;
};

// how about a restart method
K.Tween.prototype.restart = function(){
    if (this.playing) {
        this.stop();
        this.start();
    }
    return this;
}

// methods to queue callbacks with ease
K.Tween.prototype.onUpdate = function(){
    this._uC = arguments; 
    return this;
}
// _sC = startCallback | _cC = completeCallback | _stC = stopCallback
// _pC = pauseCallback | _rC = resumeCallback

For some reasons these methods aren't included into the core/plugins by default, but let you decide what you need and how to customize the animation engine for your very secific need.

Support For Additional CSS Properties

KUTE.js core engine and plugins cover what I consider to be most essential for animation, but you may have a different opinion. In case you may want to know how to animate properties that are not currently supported, stick to this guide and you'll master it real quick, it's very easy.

We need basically 3 functions:

So let's add support for boxShadow! It should be a medium difficulty guide most developers can follow and the purpose of this guide is to showcase how easy it actually is to extend KUTE.js. So grab the above template and let's break it down to pieces:

// add a reference to KUTE object
var K = window.KUTE;

// filter unsupported browsers
if (!('boxShadow' in document.body.style)) {return;}

// the preffixed boxShadow property, mostly for legacy browsers
// maybe the browser is supporting the property with its vendor preffix
// box-shadow: none|h-shadow v-shadow blur spread color |inset|initial|inherit;
var _boxShadow = K.property('boxShadow'); // note we're using the KUTE.property() autopreffix utility
var colRegEx = /(\s?(?:#(?:[\da-f]{3}){1,2}|rgba?\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\))\s?)/gi; // a full RegEx for color strings

Now we have access to the KUTE object, prototypes and it's utility functions, let's write a prepareStart function that will read the current boxShadow value:

// for the .to() method, you need to prepareStart the boxShadow property
// which means you need to read the current computed value
K.prS['boxShadow'] = function(element,property,value){
    var cssBoxShadow = K.gCS(element,'boxShadow'); // where K.gCS() is an accurate getComputedStyle() core method
    return /^none$|^initial$|^inherit$|^inset$/.test(cssBoxShadow) ? '0px 0px 0px 0px rgb(0,0,0)' : cssBoxShadow;
}

// note that in some cases the window.getComputedStyle(element,null) can be more accurate
// we are using a hybrid function that's trying to get proper colors and other stuff that
// some legacy browsers lack in this matter

// also to read the current value of an attribute, replace first line of the above function body with this
// var attrValue = element.getAttribute(property);
// and return the value or a default value, mostly rgb(0,0,0) for colors or 0 for other types  

Next we'll need to write a processProperty function that will prepare the property value and build an Object or Array of values ready to tween. This function also registers the K.dom['boxShadow'] function into the KUTE object, and this way we avoid filling the main object with unnecessary functions, just to keep performance tight.

// the processProperty for boxShadow 
// registers the K.dom['boxShadow'] function 
// returns an array of 6 values in the following format
// [horizontal, vertical, blur, spread, color: {r:0,g:0,b:0}, inset]
K.pp['boxShadow'] = function(property,value,element){
  // the DOM update function for boxShadow registers here
  // we only enqueue it if the boxShadow property is used to tween
  if ( !('boxShadow' in K.dom) ) {
    K.dom['boxShadow'] = function(w,p,v) {       
      // let's start with the numbers | set unit | also determine inset
      var numbers = [], unit = 'px', // the unit is always px
        inset = w._vS[p][5] !== 'none' || w._vE[p][5] !== 'none' ? ' inset' : false; 
      for (var i=0; i<4; i++){
        numbers.push( (w._vS[p][i] + (w._vE[p][i] - w._vS[p][i]) * v ) + unit);
      }

      // now we handle the color
      var color, _color = {}; 
      for (var c in w._vE[p][4]) {
        _color[c] = parseInt(w._vS[p][4][c] + (w._vE[p][4][c] - w._vS[p][4][c]) * v )||0;
      }
      color = 'rgb(' + _color.r + ',' + _color.g + ',' + _color.b + ') ';          
      
      // last piece of the puzzle, the DOM update
      w._el.style[_boxShadow] = inset ? color + numbers.join(' ') + inset : color + numbers.join(' ');
    };
  }

  // processProperty for boxShadow, builds basic structure with ready to tween values
  if (typeof value === 'string'){
    var color, inset = 'none';
    // make sure to always have the inset last if possible
    inset = /inset/.test(value) ? 'inset' : inset;
    value = /inset/.test(value) ? value.replace(/(\s+inset|inset+\s)/g,'') : value;

    // also getComputedStyle often returns color first "rgb(0, 0, 0) 15px 15px 6px 0px inset"
    color = value.match(colRegEx); 
    value = value.replace(color[0],'').split(' ').concat([color[0].replace(/\s/g,'')],[inset]);
    
    value = K.processBoxShadowArray(value);
  } else if (value instanceof Array){
    value = K.processBoxShadowArray(value);
  }
  return value;
}

Notice we've used an utility function that fixes the Array values and makes sure the structure is right.

// utility function to process values accordingly
// numbers must be floats/integers and color must be rgb object
K.processBoxShadowArray = function(shadow){
  var newShadow, i;
  // properly process the shadow based on amount of values
  if (shadow.length === 3) { // [h-shadow, v-shadow, color]
    newShadow = [shadow[0], shadow[1], 0, 0, shadow[2], 'none'];
  } else if (shadow.length === 4) { // [h-shadow, v-shadow, color, inset] | [h-shadow, v-shadow, blur, color]
    newShadow = /inset|none/.test(shadow[3]) ? [shadow[0], shadow[1], 0, 0, shadow[2], shadow[3]] : [shadow[0], shadow[1], shadow[2], 0, shadow[3], 'none'];
  } else if (shadow.length === 5) { // [h-shadow, v-shadow, blur, color, inset] | [h-shadow, v-shadow, blur, spread, color]
    newShadow = /inset|none/.test(shadow[4]) ? [shadow[0], shadow[1], shadow[2], 0, shadow[3], shadow[4]] : [shadow[0], shadow[1], shadow[2], shadow[3], shadow[4], 'none'];           
  } else if (shadow.length === 6) { // ideal [h-shadow, v-shadow, blur, spread, color, inset]
    newShadow = shadow; 
  } 

  // make sure the numbers are ready to tween
  for (i=0;i<4;i++){
    newShadow[i] = parseFloat(newShadow[i]);  
  }
  // make sure color is an rgb object
  newShadow[4] = K.truC(newShadow[4]); // where K.truC() is a core method to return the true color in rgb object format
  return newShadow;
}

And now, we are ready to tween both .to() and .fromTo() methods:

// tween to a string value
var myBSTween1 = KUTE.to('selector', {boxShadow: '15px 25px #069'}).start();

// or a fromTo with string and array, hex and rgb
var myBSTween2 = KUTE.fromTo('selector', {boxShadow: [15, 25, 0, '#069']}, {boxShadow: '0px 0px rgb(0,0,0)'}).start();

// maybe you want to animate an inset boxShadow?
var myBSTween3 = KUTE.fromTo('selector', {boxShadow: [5, 5, 0, '#069', 'inset']}, {boxShadow: '0px 0px rgb(0,0,0)'}).start();

You are now ready to demo!

This plugin should be compatible with IE9+ and anything that supports boxShadow CSS property. As you can see it can handle pretty much anything you throw at it, but it requires at least 3 values: h-shadow, v-shadow, and color because Safari doesn't work without a color. Also this plugin won't be able to handle multiple instances of boxShadow for same element, because the lack of support on legacy browsers, also the color cannot be RGBA, but hey, it supports both outline and inset shadows and you can fork it anyway to your liking.

If you liked this tutorial, feel free to write your own, a great idea would be for textShadow, it's very similar to the above example plugin.

Utility Methods