Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / Javascript

Drawing lines in Mozilla based browsers and the Internet Explorer

Rate me:
Please Sign up or sign in to vote.
4.78/5 (15 votes)
28 Nov 20068 min read 159.5K   1.5K   47  
Implementing the Bresenham line drawing algorithm in JavaScript to be used in browsers.
/**
 * Copyright (C) 2002-2005
 * W3L GmbH
 * Technologiezentrum Ruhr
 * Universit�tsstra�e 142
 * D-44799 Bochum
 * 
 * Author: Dipl.Ing. Doga Arinir
 * E-Mail: doga.arinir@w3l.de
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the author or the company be held liable 
 * for any damages arising from the use of this software. EXPECT BUGS!
 * 
 * You may use this software in compiled form in any way you desire PROVIDING it is
 * not sold for profit without the authors written permission, and providing that this
 * notice and the authors name is included. If the source code in this file is used in 
 * any commercial application then acknowledgement must be made to the author of this file.
 */
function checkbrowser()
{
	this.b = document.body;
	this.dom = document.getElementById ? 1:0;
	this.ie = this.b && typeof this.b.insertAdjacentHTML != 'undefined';
	this.mozilla = typeof(document.createRange) != 'undefined' && typeof((document.createRange()).setStartBefore) != 'undefined';
}

function Graphics(container)
{
	this.bw = new checkbrowser();
	this.color = '#000000';
	this.backbuffer = '';
	//Drawing to div-element or to the document itself?
	if (typeof(container) == 'string' && container != '' && typeof(document.getElementById(container)) != 'undefined')
	{
		this.container = document.getElementById(container);
		
		this.clear = function() {this.container.innerHTML = "";this.backbuffer = '';}
		var paint_ie = function()
		{
			this.container.insertAdjacentHTML("BeforeEnd", this.backbuffer);
			this.backbuffer = '';
		}
		var paint_moz = function()
		{
			var r = document.createRange();
			r.setStartBefore(this.container);
			this.container.appendChild(r.createContextualFragment(this.backbuffer));
			this.backbuffer = '';
		}		
		this.paint = this.bw.ie ? paint_ie : paint_moz;
	}
	else
	{
		this.clear = function() {this.backbuffer = '';}
		this.paint = function() 
		{
			document.write(this.backbuffer);
			this.backbuffer = '';
		}
	}	
}

Graphics.prototype.setPixel = function(x, y, w, h)
{
	this.backbuffer += '<div style="position:absolute;left:'+x+'px;top:'+y+'px;width:'+w+'px;height:'+h+'px;background-color:'+this.color+';overflow:hidden;"></div>';
}

Graphics.prototype.drawLine = function(x1, y1, x2, y2)
{
	//Always draw from left to right. This makes the algorithm much easier...
	if (x1 > x2)	{
		var tmpx = x1; var tmpy = y1;
		x1 = x2; y1 = y2;
		x2 = tmpx; y2 = tmpy;
	}
	
	var dx = x2 - x1;	
	var dy = y2 - y1; var sy = 1;	
	if (dy < 0)	{
		sy = -1;
		dy = -dy;
	}
	
	dx = dx << 1; dy = dy << 1;
	if (dy <= dx)
	{	
		var fraction = dy - (dx>>1);
		var mx = x1;
		while (x1 != x2)
		{
			x1++;
			if (fraction >= 0)
			{
				this.setPixel(mx, y1,x1-mx,1);
				y1 += sy;
				mx = x1;
				fraction -= dx;
			}
			fraction += dy;
		}
		this.setPixel(mx,y1,x1-mx,1);
	} 
	else 
	{
		var fraction = dx - (dy>>1);
		var my = y1;
		if (sy > 0)
		{		
			while (y1 != y2)
			{
				y1++;
				if (fraction >= 0)
				{
					this.setPixel(x1++, my,1,y1-my);
					my = y1;
					fraction -= dy;
				}
				fraction += dx;
			}	
			this.setPixel(x1,my,1,y1-my);
		}
		else
		{
			while (y1 != y2)
			{
				y1--;
				if (fraction >= 0)
				{
					this.setPixel(x1++, y1,1,my-y1);
					my = y1;
					fraction -= dy;
				}
				fraction += dx;
			}	
			this.setPixel(x1,y1,1,my-y1);		
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Chief Technology Officer W3L
Germany Germany
-Since 1th August 2007: Chief Technology Officer of W3L
-2002/08/01-2007/07/31: PhD student
-1997/10/15-2002/07/31: Studied Electrical Engineering and Computer Science

Comments and Discussions