Skip to content

Instantly share code, notes, and snippets.

@tracend
Last active September 1, 2015 12:30
Show Gist options
  • Save tracend/f47dc226ad33481096c3 to your computer and use it in GitHub Desktop.
Save tracend/f47dc226ad33481096c3 to your computer and use it in GitHub Desktop.
Web Worker Easing
var Box = function( num, worker ){
this.name = 'box-'+ num;
// create element
var el = document.createElement("div");
el.setAttribute( 'class', 'box' );
el.setAttribute( 'id', this.name );
el.style.top = parseInt(num) * 1 +"px";
document.body.appendChild(el);
this.el = el;
this.state.start = Date.now() || (new Date()).getTime();
if( worker ){
this.workerInit( worker );
} else {
this.animate();
}
}
Box.prototype.options = {
time: 1000,
distance: window.innerWidth
}
Box.prototype.state = {
start: Date.now() || (new Date()).getTime(),
direction: 1
}
Box.prototype.animate = function(){
var now = Date.now() || (new Date()).getTime();
var decimal = ((now - this.state.start) / this.options.time) || 0;
var pos = easing.easeInExpo( decimal );
//this.el.style.left = ( ( ( this.state.direction > 0 ) ? pos : 1 - pos ) * this.options.distance ) +"px";
this.el.style.left = ( pos * this.options.distance ) +"px";
if( decimal >= 1 || decimal <= 0 ){
this.state.direction = -1 * this.state.direction;
this.state.start = e.data.now;
}
requestAnimFrame(this.animate.bind(this));
}
Box.prototype.workerInit = function( worker ){
var self = this;
this.worker = worker;
this.worker.onmessage = function(e) {
// process relative callback
if( e.data.name != self.name ) return;
// Get fresh data from the worker
pos = e.data.pos;
//self.el.style.left = ( ( ( self.state.direction > 0 ) ? pos : 1 - pos ) * self.options.distance ) +"px";
self.el.style.left = ( pos * self.options.distance ) +"px";
if( e.data.decimal >= 1 || e.data.decimal <= 0 ){
self.state.direction = -1 * self.state.direction;
self.state.start = e.data.now;
}
}
// update worker
this.workerUpdate();
}
Box.prototype.workerUpdate = function(){
var self = this;
var now = Date.now() || (new Date()).getTime();
var options = {
name: this.name,
now : now,
start : this.state.start,
time : this.options.time
};
this.worker.postMessage( options );
requestAnimFrame(this.workerUpdate.bind(this));
}
var boxes = [];
// shim layer with setTimeout fallback
window.requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
function( callback ){
window.setTimeout(callback, 1000 / 60);
};
})();
function init(){
var i = 0;
var createBoxes = setInterval(function(){
boxes.push( new Box(i) );
i++;
if( i > 1000) clearInterval(createBoxes);
}, 10);
}
function initWorkers(){
var i = 0;
// create worker
// Build a worker from an anonymous function body
var blobURL = URL.createObjectURL( new Blob([ '(',
function(){
//Long-running work here
self.onmessage = function(e) {
// wait for easing lib
if (typeof easing == "undefined") {
importScripts("http://rawgit.com/danro/easing-js/master/easing-min.js");
}
var decimal = ((e.data.now - e.data.start) / e.data.time) || 0;
var pos = easing.easeInExpo(decimal);
// Send data back to the main thread
self.postMessage({ pos: pos, decimal: decimal, now: e.data.now, name: e.data.name });
};
}.toString(),
')()' ], { type: 'application/javascript' } ) ),
worker = new Worker( blobURL );
// Won't be needing this anymore
URL.revokeObjectURL( blobURL );
var createBoxes = setInterval(function(){
boxes.push( new Box(i, worker) );
i++;
if( i > 100) clearInterval(createBoxes);
}, 10);
}
<html>
<head>
<script type="text/javascript" src="http://rawgit.com/danro/easing-js/master/easing-min.js"></script>
<script type="text/javascript" src="box.js"></script>
<script type="text/javascript" src="main.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<script type="text/javascript">
init();
</script>
</body>
</html>
.box {
position: absolute;
width: 1px;
height: 1px;
left: 0;
top: 0;
background: #f00;
}
#time{
position: absolute;
bottom: 0;
}
<html>
<head>
<script type="text/javascript" src="http://rawgit.com/danro/easing-js/master/easing-min.js"></script>
<script type="text/javascript" src="box.js"></script>
<script type="text/javascript" src="main.js"></script>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<script type="text/javascript">
initWorkers();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment