Skip to content

mariandev/template-blocks

Repository files navigation

Template Blocks

Build Status Coverage Status

Examples

Basic example

// Timestamp generator
const ts = new Timestamp();

// A snapshot of the current timestamp
const snapshot = ts.get();

// A constant containing the previous snapshot
const now = new Const(snapshot);

// The time since the code was executed
const delta = new Diff(ts, now);

setInterval(function() {
    console.log(delta.get());
}, 500);

Basic example - compact writing

// Timestamp generator
const ts = new Timestamp();

// The time since the code was executed
const delta = ts.pipe(Diff, ts.toConst());

setInterval(function() {
    console.log(delta.get());
}, 500);

Advanced example

const loop = new Loop();

const mousePosition = new GetMousePosition();
const mousePositionSnapshot = mousePosition.toVar();

const mouseMoving = new Var(false)
	.execute(function() {
		const snapshot = mousePosition.toConst();
		const equal = snapshot.pipe(Equals, mousePositionSnapshot);
		mousePositionSnapshot.set(snapshot);
		return equal;
	})
	.evaluatePeriodically(loop);

const lightIntensity = new Var(0)
	.execute(function(ref) {
		return ref
			.pipe(
				Add,
				new If(mouseMoving, new Const(1), new Const(-1))
					.pipe(Mul, new Const(4))
					.pipe(Mul, loop.dt)
			)
			.pipe(Clamp, new Const(0), new Const(1));
	})
	.observe(console.log)
	.evaluatePeriodically(loop);

About

A simple library for creating complex but structured code for small-scale projects

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •