Last active
October 18, 2017 19:15
Star
You must be signed in to star a gist
Interpolator with NanoTimer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Node gameloop | |
var NanoTimer = require('nanotimer'); | |
var count = 0; | |
var source = msg.payload.source; | |
var target = msg.payload.target; | |
var interval = 16; | |
var time = 2500; | |
//corrected interval time / steps | |
function main(){ | |
var timer = new NanoTimer(); | |
var values = valuesToInterpolate(source, target, interval, time); | |
//timer.setInterval(outputSteps, [values], `${interval}m`); | |
var realInterval; | |
if (Array.isArray(values[0])) { | |
realInterval = (time / values[0].length) * 1000; | |
} else { | |
realInterval = (time / values.length) * 1000; | |
} | |
timer.setInterval(outputSteps, [values], `${realInterval}u`); | |
timer.setTimeout(finish, [timer], `${time}m`); | |
} | |
function outputSteps(values){ | |
//console.log('T - ' + values[count]); | |
if (Array.isArray(values[0])) { | |
var output = []; | |
for (var e in values) { | |
output.push(values[e][count]); | |
} | |
msg.payload = output; | |
node.send(msg); | |
} else { | |
msg.payload = values[count]; | |
node.send(msg); | |
} | |
count++; | |
} | |
function finish(timer){ | |
timer.clearInterval(); | |
count = 0; | |
console.log('And we\'re done'); | |
} | |
main(); | |
function valuesToInterpolate(source, target, interval, time) { | |
//source 0 | |
//target 255 | |
let steps = Math.floor(time/interval); | |
var output = []; | |
if (Array.isArray(source) && Array.isArray(target)) { | |
for (var i = 0; i < source.length; i++) { | |
var currentArray = []; | |
let increment = (target[i]-source[i]) / steps; | |
for (let v = 0 ; v <= steps ; v++) { | |
currentArray.push(source[i] + (v * increment)); | |
} | |
output.push(currentArray); | |
} | |
} else { | |
let increment = (target - source) / steps; | |
for (let v = 0 ; v <= steps ; v++) { | |
//node.warn(v); | |
output.push(source + (v * increment)); | |
} | |
} | |
return output; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var count = 0; | |
if (msg.payload.hasOwnProperty('source') && msg.payload.hasOwnProperty('target')) { | |
var source = msg.payload.source; | |
var target = msg.payload.target; | |
context.target = target; | |
} | |
// If only target is present | |
if (!msg.payload.hasOwnProperty('source') && msg.payload.hasOwnProperty('target')) { | |
var source = context.target; | |
var target = msg.payload.target; | |
context.target = target; | |
} | |
var interval = 25; | |
var time = msg.payload.time; | |
var timer1; | |
//corrected interval time / steps | |
function main(){ | |
var values = valuesToInterpolate(source, target, interval, time); | |
var realInterval; | |
if (Array.isArray(values[0])) { | |
realInterval = (time / values[0].length); | |
} else { | |
realInterval = (time / values.length); | |
} | |
timer1 = setInterval(outputSteps, realInterval, values); | |
} | |
function outputSteps(values){ | |
var endOfArray; | |
if (Array.isArray(values[0])) { | |
endOfArray = values[0].length; | |
var output = []; | |
for (var e in values) { | |
output.push(values[e][count]); | |
} | |
msg.payload = output; | |
node.send(msg); | |
} else { | |
endOfArray = values.length; | |
msg.payload = values[count]; | |
node.send(msg); | |
} | |
if (count == endOfArray-1) { | |
clearInterval(timer1); | |
count = 0; | |
return; | |
} | |
count++; | |
} | |
function finish(timer){ | |
clearInterval(timer); | |
count = 0; | |
console.log('And we\'re done'); | |
} | |
main(); | |
function valuesToInterpolate(source, target, interval, time) { | |
//source 0 | |
//target 255 | |
let steps = Math.floor(time/interval); | |
var output = []; | |
if (Array.isArray(source) && Array.isArray(target)) { | |
for (var i = 0; i < source.length; i++) { | |
var currentArray = []; | |
let increment = (target[i]-source[i]) / steps; | |
for (let v = 0 ; v <= steps ; v++) { | |
currentArray.push(source[i] + (v * increment)); | |
} | |
output.push(currentArray); | |
} | |
} else { | |
let increment = (target - source) / steps; | |
for (let v = 0 ; v <= steps ; v++) { | |
//node.warn(v); | |
output.push(source + (v * increment)); | |
} | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment