Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Jonas Raoni Soares Silva http://www.jsfromhell.com

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

TimeLine //JavaScript Class




Simulates the Adobe Flash timeline. You define the amount of frames, the speed in fps (frames per second) and, at each frame passage an event is called, useful for animations.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/classes/timeline [v1.0]

TimeLine = function(fps, f){
	this.fps = fps, this.frames = f;
};
with({o: TimeLine, $: TimeLine.prototype}){
	o.timers = [];
	$.running = !!($.current = +(o.timer = $.time = null));
	o.run = function(){
		var o = this;
		o.timer || (o.timer = setInterval(function(){
			for(var h, d = +(new Date), t = o.timers, i = t.length; i--;){
				(!t[i].running || ((d - t[i].time) / (1e3 / t[i].fps) > t[i].current + 1 &&
				t[i].onframe(++t[i].current), t[i].current >= t[i].frames)) &&
				(h = t.splice(i, 1)[0], h.stop(1));
			}
		}, 1));
	};
	$.start = function(c){
		var o = this, t = TimeLine;
		if(o.running) return;
		o.running = true, o.current = c || 0;
		o.time = new Date, o.onstart && o.onstart();
		if(!o.onframe || o.frames <= 0 || o.fps <= 0)
			return o.stop(1);
		t.timers.push(this), t.run();
	};
	$.stop = function(){
		var o = this;
		o.running = false;
		if(!TimeLine.timers.length)
			TimeLine.timer = clearInterval(TimeLine.timer), null;
		arguments.length && o.onstop && o.onstop();
	};
}


Example


<div id="box" style="position: absolute; top: 100px; background: #efe; width: 100px; height: 100px">25 fps</div>
<div id="box2" style="position: absolute; top: 300px; background: #ff9; width: 100px; height: 100px">12 fps</div>

<strong>TimeLine working together with the ease in quad function.</strong><br />

<script type="text/javascript">
Math.ease = function (t, b, c, d) {
	if ((t /= d / 2) < 1)
		return c / 2 * t * t + b;
	return -c / 2 * (--t * (t - 2) - 1) + b;
};

var o = new TimeLine(25, 50), d = document, b = d.getElementById("box");
o.onframe = function(){
	b.style.left = Math.ease(this.current, 0, 400, 30) + "px";
};
o.onstart = function(){
	d.body.appendChild(d.createTextNode("Started"));
};
o.onstop = function(){
	d.body.appendChild(d.createTextNode(" - Finished (" + (((new Date) - this.time)) + " msec)"))
	d.body.appendChild(d.createElement("br"));
	this.start();
};
o.start();


var o2 = new TimeLine(12, 50), b2 = d.getElementById("box2");
o2.onframe = function(){
	b2.style.left = Math.ease(this.current, 0, 400, 30) + "px";
};
o2.onstop = function(){
	this.start();
};
o2.start();
</script>

Graphical Plotter //Javascript Class



[UPDATED CODE AND HELP CAN BE FOUND HERE]



An unuseful thing to draw using javascript, it's slow as hell :)

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/dhtml/graphical-plotter [v1.0]

Canvas = function(){
	var o = this;
	( o.penPos = { x: 0, y: 0 }, o.pixelSize = 10, o.pen = { style: "solid", size: 1, color: "#000" }, o.brush = { style: "solid", color: "#000" } );
};
with( { p: Canvas.prototype } ){
	p.pixel = function( x, y, color ) {
		var o = this, s = document.body.appendChild( document.createElement( "div" ) ).style;
		return ( s.position = "absolute", s.width = ( o.pen.size * o.pixelSize ) + "px", s.height = ( o.pen.size * o.pixelSize ) + "px", s.fontSize = "1px", s.left = ( x * o.pixelSize ) + "px", s.top = ( y * o.pixelSize ) + "px", s.backgroundColor = color || o.pen.color, o );
	};
	p.line = function( x1, y1, x2, y2 ){
		if( Math.abs( x1 - x2 ) < Math.abs( y1 - y2 ) )
			for( y = Math.min( y1, y2 ) - 1, x = Math.max( y1, y2 ); ++y <= x; canvas.pixel( ( y * ( x1 - x2 ) - x1 * y2 + y1 * x2 ) / ( y1 - y2 ), y ) );
		else
			for( x = Math.min( x1, x2 ) - 1, y = Math.max( x1, x2 ); ++x <= y; canvas.pixel( x, ( x * ( y1 - y2 ) - y1 * x2 + x1 * y2 ) / ( x1 - x2 ) ) );
		return this;
	};
	p.arc = function( x, y, raio, startAngle, degrees ) {
		for( degrees += startAngle; degrees --> startAngle; this.pixel( Math.cos( degrees * Math.PI / 180 ) * raio + x, Math.sin( degrees * Math.PI / 180 ) * raio + y ) ); return this;
	};
	p.rectangle = function( x, y, width, height, rotation ){
		return this.moveTo( x, y ).lineBy( 0, height ).lineBy( width, 0 ).lineBy( 0, -height ).lineBy( -width, 0 );
	};
	p.moveTo = function( x, y ){ var o = this; return ( o.penPos.x = x, o.penPos.y = y, o ); };
	p.moveBy = function( x, y ){ var o = this; return o.moveTo( o.penPos.x + x, o.penPos.y + y ); };
	p.lineTo = function( x, y ){ var o = this; return o.line( o.penPos.x, o.penPos.y, x, y ).moveTo( x, y ); };
	p.lineBy = function( x, y ){ var o = this; return o.lineTo( o.penPos.x + x, o.penPos.y + y ); };
	p.curveTo = function( cx, cy, x, y ){};
	p.polyBezier = function( points ){};
	p.path = function( points ){};
}



Example:

canvas = new Canvas;

canvas.pen.color = "#f00";
canvas.rectangle( 30, 20, 20, 20 );
canvas.pen.color = "#080";
canvas.rectangle( 35, 25, 10, 10 );
canvas.pen.color = "#008";
canvas.arc( 50, 30, 10, 180, 270 );
canvas.arc( 30, 30, 10, 0, 270 );
canvas.pen.color = "#ff0";

Lenght between a dot and a line //JavaScript Function


Given a dot and a line, it returns the distance between them, the last parameter tells if the line should be considered infinite or if the function should respect its limits.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/dot-line-length [v1.0]

dotLineLength = function( x, y, x0, y0, x1, y1, o ){
	function lineLength( x, y, x0, y0 ){
		return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );
	}
	if( o && !( o = function( x, y, x0, y0, x1, y1 ){
		if( !( x1 - x0 ) ) return { x: x0, y: y };
		else if( !( y1 - y0 ) ) return { x: x, y: y0 };
		var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );
		return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };
	}( x, y, x0, y0, x1, y1 ), o.x >= Math.min( x0, x1 ) && o.x <= Math.max( x0, x1 ) && o.y >= Math.min( y0, y1 ) && o.y <= Math.max( y0, y1 ) ) ){
		var l1 = lineLength( x, y, x0, y0 ), l2 = lineLength( x, y, x1, y1 );
		return l1 > l2 ? l2 : l1;
	}
	else {
		var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
		return Math.abs( a * x + b * y + c ) / Math.sqrt( a * a + b * b );
	}
};

"Intersection dot" between a dot and a line //JavaScript Function


Given a line and a dot, it return the coordinates of their intersection.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/dot-line-intersection [v1.0]

dotLineIntersection = function( x, y, x0, y0, x1, y1 ){
	if( !( x1 - x0 ) )
		return { x: x0, y: y };
	else if( !( y1 - y0 ) )
		return { x: x, y: y0 };
	var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );
	return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };
};

Line Length //JavaScript Function


Returns the length of a line.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/line-length [v1.0]

lineLength = function( x, y, x0, y0 ){
	return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );
};

Closest Line Point //JavaScript Function


Given a dot and a line, it returns the nearest dot over the line.

[UPDATED CODE AND HELP CAN BE FOUND HERE]



//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/closest-line-point [v1.0]

closestLinePoint = function( px, py, x, y, angle ){
	var tg = ( ( angle %= 360 ) < 0 && ( angle += 180 ), Math.tan( -angle * Math.PI / 180 ) );
	return angle < 45 || angle > 135 ? { x: px, y: ( px - x ) * tg + y } : { x: ( py - y ) / tg + x, y: py };
};


get line break type (mac, linux, win and binary file)

this code analyses the first 5*1024 bytes of data from a file and tells which break type it uses, or if it's a binary file...

//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com

type
  TBreakType = ( btNone, btWin, btMac, btLin, btBin );

---------

function GetBreakType( const Filename: string; const MaxDataToRead: Cardinal = 5*1024 ): TBreakType;
var
  FS: TFileStream;
  Buffer, BufferStart, BufferEnd: PChar;
begin
  Result := btNone;
  if not FileExists( Filename ) then
    raise Exception.Create( 'GetBreakType: nome de arquivo inválido' );
  try
    FS := TFileStream.Create( Filename, fmOpenRead );
    GetMem( Buffer, MaxDataToRead+1 );
    BufferEnd := ( Buffer + FS.Read( Buffer^, MaxDataToRead ) );
    BufferStart := Buffer;
    BufferEnd^ := #0;
  except
    raise Exception.Create( 'GetBreakType: erro alocando memória.' );
  end;
  try
    while Buffer^ <> #0 do begin
      if Result = btNone then
        if Buffer^ = ASCII_CR then begin
          if (Buffer+1)^ = ASCII_LF then begin
            Result := btWin;
            Inc( Buffer );
          end
          else
            Result := btMac;
        end
        else if Buffer^ = ASCII_LF then
          Result := btLin;
      Inc( Buffer );
    end;
    if Buffer <> BufferEnd then
      Result := btBin;
  finally
    FreeMem( BufferStart, MaxDataToRead+1 );
    FS.Free;
  end;
end;
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS