Skip to content

Instantly share code, notes, and snippets.

@tong
Last active March 22, 2016 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tong/2b9b64671d78e6cdf479 to your computer and use it in GitHub Desktop.
Save tong/2b9b64671d78e6cdf479 to your computer and use it in GitHub Desktop.
Violent depattern animation: http://temp.disktree.net/tafby/
import js.Browser.document;
import js.Browser.window;
import js.html.CanvasElement;
import js.html.CanvasRenderingContext2D;
class TAFby {
public var canvas(default,null) : CanvasElement;
//public var backgroundColor : String;
//public var strokeColor : String;
var ctx : CanvasRenderingContext2D;
var size : Int;
var d : Int;
var n : Int;
var dir = true;
public function new( canvas : CanvasElement, size : Int, backgroundColor : String, strokeColor : String, lineWidth = 1 ) {
this.canvas = canvas;
this.size = size;
//this.backgroundColor = backgroundColor;
//this.strokeColor = strokeColor;
ctx = canvas.getContext2d();
ctx.fillStyle = backgroundColor;
ctx.strokeStyle = strokeColor;
ctx.lineWidth = lineWidth;
d = Std.int( size/80 );
n = Std.int( size/d )+1;
}
public function draw() {
ctx.clearRect( 0, 0, canvas.width, canvas.height );
ctx.beginPath();
for( i in 0...n ) {
var v = Std.int(i*d);
if( dir ) {
line( 0, v, v, 0 );
line( size, v, v, size );
} else {
line( 0, size-v, v, size );
line( v, 0, size, size-v );
}
}
ctx.stroke();
dir = !dir;
}
inline function line( x1 : Int, y1 : Int, x2 : Int, y2 : Int ) {
ctx.moveTo( x1, y1 );
ctx.lineTo( x2, y2 );
}
static var tafby : TAFby;
//static function update( time : Float ) {
static function update() {
//window.requestAnimationFrame( update );
tafby.draw();
}
static function main() {
window.onload = function(_){
var w = window.innerWidth;
var h = window.innerHeight;
var size = (w > h) ? h : w;
var canvas = document.createCanvasElement();
canvas.width = size;
canvas.height = size;
tafby = new TAFby( canvas, size, '#000', '#fff', 1 );
document.body.appendChild( tafby.canvas );
//window.requestAnimationFrame( update );
var timer = new haxe.Timer(40);
timer.run = update;
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment