A pure JavaScript tweening engine.
Modify from tweenjs/tween.js
nuintun / tween Goto Github PK
View Code? Open in Web Editor NEWA pure JavaScript tweening engine.
Home Page: https://nuintun.github.io/tween
License: MIT License
A pure JavaScript tweening engine.
Home Page: https://nuintun.github.io/tween
License: MIT License
A pure JavaScript tweening engine.
Modify from tweenjs/tween.js
/**
* @module events
* @license MIT
* @author nuintun
*/
import * as Utils from './utils';
/**
* @class Events
* @constructor
*/
export default function Events() {
this._events = {};
}
// Set prototype
Events.prototype = {
/**
* @method on
* @description Bind callback tos event
* @param {string} events
* @param {Function} callback
* @param {any} context
* @returns {Events}
*/
on: function(event, callback) {
var context = this;
var events = context._events;
var callbacks = events[event] || (events[event] = []);
if (Utils.typeIs(callback, 'Function')) {
callbacks.push(callback);
}
return context;
},
/**
* @method off
* @description Remove callback of event
* If `callback` is null, removes all callbacks for the event.
* If `event` is null, removes all bound callbacks for the event.
* @param {string} event
* @param {Function} callback
* @returns {Events}
*/
off: function(event, callback) {
var context = this;
switch (arguments.length) {
case 0:
context._events = {};
break;
case 1:
delete context._events[event];
break;
default:
var callbacks = events[event];
if (callbacks) {
var i = Utils.indexOf(callbacks, callback);
if (i !== -1) {
callbacks.splice(i, 1);
}
}
break;
}
return context;
},
/**
* @method once
* @description Bind a event only emit once
* @param {string} events
* @param {Function} callback
* @returns {Events}
*/
once: function(events, callback) {
var that = this;
function feedback() {
that.off(events, feedback);
Utils.apply(callback, this, arguments);
}
return that.on(events, feedback);
},
/**
* @method emitWith
* @description Emit event with context
* @param {string} event
* @param {Array} args
* @param {any} context
* @returns {Events}
*/
emitWith: function(event, args, context) {
var that = this;
var events = that._events;
var callbacks = events[event];
if (!callbacks || callbacks.length === 0) {
return false;
}
// Arguments length
var length = arguments.length;
// Default context
if (length < 3) {
context = that;
}
// Format args
if (length < 2) {
args = [];
} else {
args = Utils.typeIs(args, 'Array') ? args : [args];
}
Utils.forEach(callbacks, function(callback) {
Utils.apply(callback, context, args);
});
return true;
},
/**
* @method emit
* @description Emit event
* @param {string} event
* @param {any} [...args]
* @returns {Events}
*/
emit: function(event) {
var context = this;
var events = context._events;
var callbacks = events[event];
if (!callbacks || callbacks.length === 0) {
return false;
}
var rest = [];
// Fill up `rest` with the callback arguments. Since we're only copying
// the tail of `arguments`, a loop is much faster than Array#slice.
for (var i = 1, length = arguments.length; i < length; i++) {
rest[i - 1] = arguments[i];
}
return context.emitWith(event, rest);
}
};
/**
* @function add
* @param {number} x
* @param {number} y
* @returns {number}
*/
function add(x, y) {
var decimal = /\.\d+/;
var x1 = decimal.exec(String(x));
var y1 = decimal.exec(String(y));
var e = Math.max(x1 ? x1[0].length - 1 : 0, y1 ? y1[0].length - 1 : 0);
if (e) {
e = Math.pow(10, e);
return (x * e + y * e) / e;
} else {
return x + y;
}
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.