<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: mousemove code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 00:07:47 GMT</pubDate>
    <description>DZone Snippets: mousemove code</description>
    <item>
      <title>Drawing a single Polyline using SVG</title>
      <link>http://snippets.dzone.com/posts/show/5270</link>
      <description>This code is very similar to &lt;a href="http://snippets.dzone.com/posts/show/5269"&gt;Draw a series of lines using SVG&lt;/a&gt; [dzone.com] but now we can draw a series of points at the same time the mouse button is pressed and is moving. Here's an &lt;a href="http://twitxr.com/image/18451/"&gt;example of the SVG squiggle output&lt;/a&gt; [twitxr.com].&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;svg	xmlns="http://www.w3.org/2000/svg" width="100%"&lt;br /&gt;		xmlns:xlink="http://www.w3.org/1999/xlink" &gt;&lt;br /&gt;  &lt;g id="sketch" class="sketch"&gt;&lt;br /&gt;&lt;br /&gt;    &lt;rect x="0"&lt;br /&gt;y="0" width="100%" height="100%" fill="yellow" /&gt;&lt;br /&gt;  &lt;/g&gt;&lt;br /&gt;&lt;br /&gt;  &lt;script&gt;&lt;br /&gt;  &lt;![CDATA[&lt;br /&gt;  var	xmlns="http://www.w3.org/2000/svg"&lt;br /&gt;  Root=document.documentElement&lt;br /&gt;  on_start(Root)&lt;br /&gt;&lt;br /&gt;  function mouseUp(evt) {&lt;br /&gt;	  on_pen_up(Root);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_start(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"mouseMove",&lt;br /&gt;      "onmousedown":"add_line(evt)",	&lt;br /&gt;      "onmouseup":"mouseUp"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_pen_down(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"addPoint(evt)",&lt;br /&gt;	    "onmouseup":"on_pen_up(Root)"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function on_pen_up(R){&lt;br /&gt;	  var Attr={&lt;br /&gt;	    "onmousemove":"null"&lt;br /&gt;	  }&lt;br /&gt;	  assignAttr(R,Attr)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function add_line(evt){&lt;br /&gt;	  //if (evt.target.nodeName!="rect") return&lt;br /&gt;	  var C=document.createElementNS(xmlns,"polyline") &lt;br /&gt;	&lt;br /&gt;	  var Attr={&lt;br /&gt;		  "x":30,&lt;br /&gt;		  "y":180,&lt;br /&gt;		  "fill":'none',&lt;br /&gt;		  "stroke": '#44a',&lt;br /&gt;		  "id":'pl1',&lt;br /&gt;		  "stroke-width":2&lt;br /&gt;	  }&lt;br /&gt;    &lt;br /&gt;	  assignAttr(C,Attr)&lt;br /&gt;	  Root.appendChild(C)&lt;br /&gt;    &lt;br /&gt;	  on_pen_down(Root)&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function addPoint (evt) {&lt;br /&gt;    element = document.getElementById('pl1') 	&lt;br /&gt;    var point = element.ownerDocument.documentElement.createSVGPoint(); &lt;br /&gt;    point.x = evt.clientX; &lt;br /&gt;    point.y = evt.clientY;&lt;br /&gt;    &lt;br /&gt;    element.points.appendItem(point);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function assignAttr(O,A){&lt;br /&gt;	  for (i in A) O.setAttributeNS(null,i, A[i])&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  ]]&gt;&lt;br /&gt;  &lt;/script&gt;&lt;br /&gt;&lt;br /&gt;  &lt;text font-size="12pt" x="50" y="20" id="t1"&gt;Click something to move it&lt;/text&gt;&lt;br /&gt;  &lt;text font-size="12pt" x="80" y="40" id="t2"&gt;Click nothing to add something&lt;/text&gt;&lt;br /&gt;&lt;/svg&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;*update 7:44pm*&lt;br /&gt;By simply making the id unique using a global variable count it was possible to draw &lt;a href="http://www.twitxr.com/image/18474/"&gt;multiple polylines&lt;/a&gt; [twitxr.com].</description>
      <pubDate>Sat, 22 Mar 2008 19:15:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5270</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Capturing the mousemove coordinates</title>
      <link>http://snippets.dzone.com/posts/show/5264</link>
      <description>Source code copied from &lt;a href="http://javascript.internet.com/page-details/mouse-coordinates.html"&gt;The JavaScript Source: Page Details: Mouse Coordinates&lt;/a&gt; [internet.com]&lt;br /&gt;Tested on Firefox.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!-- ONE STEP TO INSTALL MOUSE COORDINATES:&lt;br /&gt;&lt;br /&gt;  1.  Copy the coding into the BODY of your HTML document  --&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- STEP ONE: Paste this code into the BODY of your HTML document  --&gt;&lt;br /&gt;&lt;br /&gt;&lt;BODY&gt;&lt;br /&gt;&lt;br /&gt;&lt;form name="Show"&gt;&lt;br /&gt;X &lt;input type="text" name="MouseX" value="0" size="4"&gt;&lt;br&gt;&lt;br /&gt;Y &lt;input type="text" name="MouseY" value="0" size="4"&gt;&lt;br&gt;&lt;br /&gt;&lt;/form&gt;&lt;br /&gt;&lt;br /&gt;&lt;script language="JavaScript1.2"&gt;&lt;br /&gt;&lt;!-- Original:  CodeLifter.com (support@codelifter.com) --&gt;&lt;br /&gt;&lt;!-- Web Site:  http://www.codelifter.com --&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- This script and many more are available free online at --&gt;&lt;br /&gt;&lt;!-- The JavaScript Source!! http://javascript.internet.com --&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- Begin&lt;br /&gt;  var IE = document.all?true:false;&lt;br /&gt;  if (!IE) document.captureEvents(Event.MOUSEMOVE)&lt;br /&gt;    document.onmousemove = getMouseXY;&lt;br /&gt;    &lt;br /&gt;  var tempX = 0;&lt;br /&gt;  var tempY = 0;&lt;br /&gt;  &lt;br /&gt;  function getMouseXY(e) {&lt;br /&gt;    if (IE) { // grab the x-y pos.s if browser is IE&lt;br /&gt;      tempX = event.clientX + document.body.scrollLeft;&lt;br /&gt;      tempY = event.clientY + document.body.scrollTop;&lt;br /&gt;    }&lt;br /&gt;    else {  // grab the x-y pos.s if browser is NS&lt;br /&gt;      tempX = e.pageX;&lt;br /&gt;      tempY = e.pageY;&lt;br /&gt;    }  &lt;br /&gt;    &lt;br /&gt;    if (tempX &lt; 0){tempX = 0;}&lt;br /&gt;    if (tempY &lt; 0){tempY = 0;}  &lt;br /&gt;    &lt;br /&gt;    document.Show.MouseX.value = tempX;&lt;br /&gt;    document.Show.MouseY.value = tempY;&lt;br /&gt;    &lt;br /&gt;    return true;&lt;br /&gt;  }&lt;br /&gt;//  End --&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;center&gt;&lt;br /&gt;&lt;font face="arial, helvetica" size"-2"&gt;Free JavaScripts provided&lt;br&gt;&lt;br /&gt;by &lt;a href="http://javascriptsource.com"&gt;The JavaScript Source&lt;/a&gt;&lt;/font&gt;&lt;br /&gt;&lt;/center&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;!-- Script Size:  1.33 KB --&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;*update 10:55am 21-Mar-08*&lt;br /&gt;I have decided to use the following code instead as it looks a bit cleaner, and more up-to-date.&lt;br /&gt;Tested on Firefox 2 and IE 6.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;document.onmousemove = mouseMove;&lt;br /&gt;&lt;br /&gt;function mouseMove(ev){&lt;br /&gt;	ev           = ev || window.event;&lt;br /&gt;	var mousePos = mouseCoords(ev);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function mouseCoords(ev){&lt;br /&gt;	if(ev.pageX || ev.pageY){&lt;br /&gt;		return {x:ev.pageX, y:ev.pageY};&lt;br /&gt;	}&lt;br /&gt;	return {&lt;br /&gt;		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,&lt;br /&gt;		y:ev.clientY + document.body.scrollTop  - document.body.clientTop&lt;br /&gt;	};&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Reference: &lt;a href="http://snipr.com/22a5e"&gt;How to Drag and Drop in JavaScript&lt;/a&gt; [webreference.com]</description>
      <pubDate>Thu, 20 Mar 2008 22:39:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5264</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
  </channel>
</rss>
