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

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

Create and drag circles in SVG

Demonstrates creating creating and dragging circles using SVG and ECMAScript.

Tested on Firefox 3 beta 4.

file: makeDragDrop.svg
   1  
   2  <svg	xmlns="http://www.w3.org/2000/svg" width="100%"
   3  		xmlns:xlink="http://www.w3.org/1999/xlink" >
   4  <script><![CDATA[
   5  var	xmlns="http://www.w3.org/2000/svg"
   6  	xlink="http://www.w3.org/1999/xlink" 
   7  	Root=document.documentElement
   8  standardize(Root)
   9  
  10  function standardize(R){
  11  	var Attr={
  12  		"onmouseup":"add(evt)",
  13  		"onmousedown":"grab(evt)",
  14  		"onmousemove":null,
  15  		"onmouseover":"hilight(evt)",
  16  		"onmouseout":"hilight(evt)"
  17  	}
  18  	assignAttr(R,Attr)
  19  }
  20  function hilight(evt){
  21  	var T=evt.target
  22  	if (T.nodeName=="rect") return
  23  	if (evt.type=="mouseover") T.setAttributeNS(null,"stroke-opacity",1)
  24  	else T.setAttributeNS(null,"stroke-opacity",.5)
  25  }
  26  function add(evt){
  27  	if (evt.target.nodeName!="rect") return
  28  	var C=document.createElementNS(xmlns,"circle") 
  29  	var stroke=Color()
  30  	var rad=10+Math.random()*50
  31  	var Attr={
  32  		r:rad,
  33  		cx:evt.clientX,
  34  		cy:evt.clientY,
  35  		"fill": Color(),
  36  		"fill-opacity":.75,
  37  		"stroke": stroke,
  38  		"stroke-opacity":.5,
  39  		"id":stroke,
  40  		"stroke-width":10+Math.random()*(55-rad)
  41  	}
  42  	assignAttr(C,Attr)
  43  	Root.appendChild(C)
  44  }
  45  function grab(evt){
  46  	var O=evt.target
  47  	if (evt.target.nodeName=="rect") return
  48  	var Attr={
  49  		"onmousemove":"slide(evt,'"+O.id+"')",
  50  		"onmouseup":"standardize(Root)"
  51  	}
  52  	assignAttr(Root,Attr)
  53  }
  54  function slide(evt,id){
  55  	var o=document.getElementById(id)
  56  	var c=""; if (o.nodeName=="circle") c="c"
  57  	o.setAttributeNS(null, c+"x", evt.clientX)
  58  	o.setAttributeNS(null, c+"y", evt.clientY)
  59  }
  60  function assignAttr(O,A){
  61  	for (i in A) O.setAttributeNS(null,i, A[i])
  62  }
  63  function Color(){
  64  	return "rgb("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+")"
  65  }
  66  ]]>
  67  </script>
  68  <rect width="100%" height="100%" fill="white"/>
  69  <text font-size="12pt" x="50" y="20" id="t1">Click something to move it</text>
  70  <text font-size="12pt" x="80" y="40" id="t2">Click nothing to add something</text>
  71  </svg>

Source file copied from http://srufaculty.sru.edu/david.dailey/svg/makeDragDrop.svg

javascript drag and drop

Ever wanted to drag an element or image on your webpage? You can try using this very lightweight javascript drag and drop. You can attach this drag handler to any relative or absolute positioned element. More code snippets you can find on free code and tutorials website.

   1  
   2  /**
   3  *
   4  *  Crossbrowser Drag Handler
   5  *  http://www.webtoolkit.info/
   6  *
   7  **/
   8  
   9  var DragHandler = {
  10  
  11  
  12  	// private property.
  13  	_oElem : null,
  14  
  15  
  16  	// public method. Attach drag handler to an element.
  17  	attach : function(oElem) {
  18  		oElem.onmousedown = DragHandler._dragBegin;
  19  
  20  		// callbacks
  21  		oElem.dragBegin = new Function();
  22  		oElem.drag = new Function();
  23  		oElem.dragEnd = new Function();
  24  
  25  		return oElem;
  26  	},
  27  
  28  
  29  	// private method. Begin drag process.
  30  	_dragBegin : function(e) {
  31  		var oElem = DragHandler._oElem = this;
  32  
  33  		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
  34  		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }
  35  
  36  		var x = parseInt(oElem.style.left);
  37  		var y = parseInt(oElem.style.top);
  38  
  39  		e = e ? e : window.event;
  40  		oElem.mouseX = e.clientX;
  41  		oElem.mouseY = e.clientY;
  42  
  43  		oElem.dragBegin(oElem, x, y);
  44  
  45  		document.onmousemove = DragHandler._drag;
  46  		document.onmouseup = DragHandler._dragEnd;
  47  		return false;
  48  	},
  49  
  50  
  51  	// private method. Drag (move) element.
  52  	_drag : function(e) {
  53  		var oElem = DragHandler._oElem;
  54  
  55  		var x = parseInt(oElem.style.left);
  56  		var y = parseInt(oElem.style.top);
  57  
  58  		e = e ? e : window.event;
  59  		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
  60  		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';
  61  
  62  		oElem.mouseX = e.clientX;
  63  		oElem.mouseY = e.clientY;
  64  
  65  		oElem.drag(oElem, x, y);
  66  
  67  		return false;
  68  	},
  69  
  70  
  71  	// private method. Stop drag process.
  72  	_dragEnd : function() {
  73  		var oElem = DragHandler._oElem;
  74  
  75  		var x = parseInt(oElem.style.left);
  76  		var y = parseInt(oElem.style.top);
  77  
  78  		oElem.dragEnd(oElem, x, y);
  79  
  80  		document.onmousemove = null;
  81  		document.onmouseup = null;
  82  		DragHandler._oElem = null;
  83  	}
  84  
  85  }

DragLibrary //Java Script Object



[UPDATED CODE AND HELP CAN BE FOUND HERE]


@REQUIRES Event-Listener


Code

   1  
   2  <script type="text/javascript">
   3  //Requires http://www.jsfromhell.com/geral/event-listener
   4  
   5  //+ Jonas Raoni Soares Silva
   6  //@ http://jsfromhell.com/dhtml/drag-library [v1.0]
   7  
   8  DragLibrary = {
   9  	e: null,
  10  	diff: { x: 0, y: 0 },
  11  	lineLength: function( x, y, x0, y0 ){
  12  		return Math.sqrt( ( x -= x0 ) * x + ( y -= y0 ) * y );
  13  	},
  14  	dotLineLength: function( x, y, x0, y0, x1, y1, o ){
  15  		if( o && !( o = function( x, y, x0, y0, x1, y1 ){
  16  			if( !( x1 - x0 ) ) return { x: x0, y: y };
  17  			else if( !( y1 - y0 ) ) return { x: x, y: y0 };
  18  			var left, tg = -1 / ( ( y1 - y0 ) / ( x1 - x0 ) );
  19  			return { x: left = ( x1 * ( x * tg - y + y0 ) + x0 * ( x * - tg + y - y1 ) ) / ( tg * ( x1 - x0 ) + y0 - y1 ), y: tg * left - tg * x + y };
  20  		}( 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 ) ) ){
  21  			var l1 = this.lineLength( x, y, x0, y0 ), l2 = this.lineLength( x, y, x1, y1 );
  22  			return l1 > l2 ? l2 : l1;
  23  		}
  24  		else {
  25  			var a = y0 - y1, b = x1 - x0, c = x0 * y1 - y0 * x1;
  26  			return Math.abs( a * x + b * y + c ) / Math.sqrt( a * a + b * b );
  27  		}
  28  	},
  29  	beginDrag: function ( e ){
  30  		var dL = DragLibrary, o = dL.e = e.target;
  31  		dL.diff = { x: o.offsetLeft - e.clientX, y: o.offsetTop - e.clientY };
  32  		addEventListener( document, "mousemove", dL.drag );
  33  		addEventListener( document, "mouseup", dL.endDrag );
  34  	},
  35  	drag: function( e ){
  36  		var dL = DragLibrary, pt = dL.e.dragHandler.call( dL.e.dragHandler.data, e.clientX, e.clientY );
  37  		dL.e.style.left = ( pt.x += dL.diff.x ) + "px";
  38  		dL.e.style.top = ( pt.y += dL.diff.y ) + "px";
  39  		dL.e.ondrag instanceof Function && dL.e.ondrag( e, pt.x, pt.y );
  40  	},
  41  	endDrag: function(){
  42  		removeEventListener( document, "mouseup", DragLibrary.endDrag );
  43  		removeEventListener( document, "mousemove", DragLibrary.drag );
  44  	},
  45  	setHandler: function( e, data, handler ){
  46  		( e.dragHandler = handler ).data = data;
  47  		e.style.position = "absolute";
  48  		addEventListener( e, "mousedown", this.beginDrag );
  49  	},
  50  	freeDrag: function( e ){
  51  		this.setHandler( e, null, function( x, y ){
  52  			return { x: x, y: y };
  53  		} );
  54  	},
  55  	squareDrag: function( e, x, y, width, height ){
  56  		this.setHandler( e, { x: x, y: y, w: width, h: height }, function( x, y ){
  57  			var o = this;
  58  			return { x: x < o.x ? o.x : x > o.x + o.w ? o.x + o.w : x, y: y < o.y ? o.y : y > o.y + o.h ? o.y + o.h : y };
  59  		});
  60  	},
  61  	circleDrag: function( e, x, y, ray ){
  62  		this.setHandler( e, { x: x + ray, y: y + ray, ray: ray }, function( x, y ){
  63  			var o = this, tg;
  64  			return DragLibrary.lineLength( x, y, o.x, o.y ) > o.ray ?
  65  				{ x: Math.cos( tg = Math.atan2( y - o.y, x - o.x ) ) * o.ray + o.x, y: Math.sin( tg ) * o.ray + o.y }
  66  				: { x: x, y: y };
  67  		} );
  68  	},
  69  	freeLineDrag: function( e, x, y, angle ){
  70  		this.setHandler( e, { x: x, y: y, angle: angle }, function( x, y ){
  71  			var o = this, tg = ( ( o.angle %= 360 ) < 0 && ( o.angle += 180 ), Math.tan( -o.angle * Math.PI / 180 ) );
  72  			return o.angle < 45 || o.angle > 135 ? { x: x, y: ( x - o.x ) * tg + o.y } : { x: ( y - o.y ) / tg + o.x, y: y };
  73  		} );
  74  	},
  75  	polylineDrag: function( e, x0, y0, x1, y1, etc, etc, etc ){
  76  		for( var args = [].slice.call( arguments, 0 ), lines = []; args.length > 4; lines[lines.length] = { y1: args.pop(), x1: args.pop(), y0: args.pop(), x0: args.pop() } );
  77  		this.setHandler( e, lines, function( x, y ){
  78  			if( !this.length )
  79  				return { x: x, y: y };
  80  			var l, dL = DragLibrary, o = this[0], lower = { i: 0, l: dL.dotLineLength( x, y, o.x0, o.y0, o.x1, o.y1, 1 ) };
  81  			for( var i in this )
  82  				lower.l > ( l = dL.dotLineLength( x, y, ( o = this[i] ).x0, o.y0, o.x1, o.y1, 1 ) ) && ( lower = { i: i, l: l } );
  83  			y = y < Math.min( ( o = this[lower.i] ).y0, o.y1 ) ? Math.min( o.y0, o.y1 ) : y > Math.max( o.y0, o.y1 ) ? Math.max( o.y0, o.y1 ) : y;
  84  			x = x < Math.min( o.x0, o.x1 ) ? Math.min( o.x0, o.x1 ) : x > Math.max( o.x0, o.x1 ) ? Math.max( o.x0, o.x1 ) : x;
  85  			return Math.abs( o.x0 - o.x1 ) < Math.abs( o.y0 - o.y1 ) ? { x: ( y * ( o.x0 - o.x1 ) - o.x0 * o.y1 + o.y0 * o.x1 ) / ( o.y0 - o.y1 ), y: y }
  86  			: { x: x, y: ( x * ( o.y0 - o.y1 ) - o.y0 * o.x1 + o.x0 * o.y1 ) / ( o.x0 - o.x1 ) };
  87  		} );
  88  	}
  89  };



Example

   1  
   2  <style type="text/css">
   3  .box{
   4  	position: absolute;
   5  	border: 1px solid #000;
   6  	width: 50px;
   7  	height: 50px;
   8  	font: 12px monospace;
   9  	font-weight: bold;
  10  }
  11  #circle{ background-color: #fee; }
  12  #square{ background-color: #ccc; left: 50px; }
  13  #freeLine{ background-color: #eff; left: 100px; }
  14  #polyLine{ background-color: #efe; left: 150px; }
  15  #free{ background-color: #eef; left: 200px; }
  16  </style>
  17  
  18  
  19  <script type="text/javascript">
  20  //<![CDATA[
  21  
  22  function newBox( id ){
  23  	var r = document.body.appendChild( document.createElement( "div" ) ).appendChild( document.createTextNode( "kind: " + id ) ).parentNode;
  24  	return ( r.setAttribute( "id", id ), r.setAttribute( "class", "box" ),	r.className = "box", r );
  25  }
  26  
  27  DragLibrary.freeDrag( newBox( "free" ) );
  28  DragLibrary.polylineDrag( newBox( "polyLine" ), 0, 0, 200, 200, 200, 200, 400, 200, 400, 200, 600, 0 );
  29  DragLibrary.freeLineDrag( newBox( "freeLine" ), 200, 400, 60 );
  30  DragLibrary.squareDrag( newBox( "square" ), 200, 200, 400, 200 );
  31  DragLibrary.circleDrag( newBox( "circle" ), 100, 100, 100 );
  32  
  33  //]]>
  34  </script>
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS