Logo
Filmmaskiner.dk

Variable Modulator script

The script
function varModulator(val, newvar, rate){
	this.val = val;
	this.newvar = newvar;
	this.rate = rate;
	this.calcVar = calcVar;
}

function calcVar(){
	if(this.rate == 0){
		// IMMEDIATE
		this.val = eval(this.newvar);
	}else if(this.newvar > this.val){
		this.val = eval(this.val + this.rate);
		if(this.newvar < this.val){
			this.val = eval(this.newvar);
		}
	}else if(this.newvar < this.val){
		this.val = eval(this.val - this.rate);
		if(this.newvar > this.val){
			this.val = eval(this.newvar);
		}
	}
}
Example of use:
Create vars
var stepvar = new varModulator(0, 0, 1/10);
var yinvvar = new varModulator(0, 0, 10);
var pinvvar = new varModulator(0, 0, 10);
Update var using jQuery
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>  $(function() {
    $("#yinv").change(function(){
	yinvvar.newvar = eval($(this).val());
    });

    $("#pinv").change(function(){
	pinvvar.newvar = eval($(this).val());
    });

    $("#newstep").change(function(){
	stepvar.newvar = eval($(this).val());
    });

    $("#steprate").change(function(){
	stepvar.rate = eval($(this).val());
    });
  });
</script>
calcVar for each frame and get new value
function sequence1() {
    setTimeout(function () {
	resetCanvas();
	stepvar.calcVar();
	yinvvar.calcVar();
	pinvvar.calcVar();
	drawArabesque(stepvar.val, yinvvar.val, pinvvar.val);
	sequence1();
    }, fps);
}

Creative Commons License
Filmmaskiner.dk by Kasper Lauritzen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
(unless other is noted)