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-7 of 7 total  RSS 

Drawing a single Polyline using SVG

This code is very similar to Draw a series of lines using SVG [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 example of the SVG squiggle output [twitxr.com].
<svg	xmlns="http://www.w3.org/2000/svg" width="100%"
		xmlns:xlink="http://www.w3.org/1999/xlink" >
  <g id="sketch" class="sketch">

    <rect x="0"
y="0" width="100%" height="100%" fill="yellow" />
  </g>

  <script>
  <![CDATA[
  var	xmlns="http://www.w3.org/2000/svg"
  Root=document.documentElement
  on_start(Root)

  function mouseUp(evt) {
	  on_pen_up(Root);
  }

  function on_start(R){
	  var Attr={
	    "onmousemove":"mouseMove",
      "onmousedown":"add_line(evt)",	
      "onmouseup":"mouseUp"
	  }
	  assignAttr(R,Attr)
  }

  function on_pen_down(R){
	  var Attr={
	    "onmousemove":"addPoint(evt)",
	    "onmouseup":"on_pen_up(Root)"
	  }
	  assignAttr(R,Attr)
  }

  function on_pen_up(R){
	  var Attr={
	    "onmousemove":"null"
	  }
	  assignAttr(R,Attr)
  }

  function add_line(evt){
	  //if (evt.target.nodeName!="rect") return
	  var C=document.createElementNS(xmlns,"polyline") 
	
	  var Attr={
		  "x":30,
		  "y":180,
		  "fill":'none',
		  "stroke": '#44a',
		  "id":'pl1',
		  "stroke-width":2
	  }
    
	  assignAttr(C,Attr)
	  Root.appendChild(C)
    
	  on_pen_down(Root)
  }

  function addPoint (evt) {
    element = document.getElementById('pl1') 	
    var point = element.ownerDocument.documentElement.createSVGPoint(); 
    point.x = evt.clientX; 
    point.y = evt.clientY;
    
    element.points.appendItem(point);
  }

  function assignAttr(O,A){
	  for (i in A) O.setAttributeNS(null,i, A[i])
  }

  ]]>
  </script>

  <text font-size="12pt" x="50" y="20" id="t1">Click something to move it</text>
  <text font-size="12pt" x="80" y="40" id="t2">Click nothing to add something</text>
</svg>



*update 7:44pm*
By simply making the id unique using a global variable count it was possible to draw multiple polylines [twitxr.com].

Draw a series of lines using SVG

This code draws a line every time the user presses the mouse button. Here is an example of the SVG output [twitxr.com].

Tested in Firefox 3 beta 4.
<?xml version="1.0" encoding="UTF-8"?> 
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
 onclick="addPoint(evt, document.getElementById('pl1'));"> 
  <title>click to add point to polyline</title>
  <script type="text/ecmascript">
    <![CDATA[ 
      function addPoint (evt, element) {
        var point = element.ownerDocument.documentElement.createSVGPoint(); 
        point.x = evt.clientX; 
        point.y = evt.clientY; 
        element.points.appendItem(point);
      }
    ]]>
  </script> 
  <rect x="0" y="0" width="100%" height="100%" fill-opacity="0" />
  <polyline id="pl1" fill="none" stroke="green" stroke-width="2" points="30,180 50,150" />
</svg> 

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
<svg	xmlns="http://www.w3.org/2000/svg" width="100%"
		xmlns:xlink="http://www.w3.org/1999/xlink" >
<script><![CDATA[
var	xmlns="http://www.w3.org/2000/svg"
	xlink="http://www.w3.org/1999/xlink" 
	Root=document.documentElement
standardize(Root)

function standardize(R){
	var Attr={
		"onmouseup":"add(evt)",
		"onmousedown":"grab(evt)",
		"onmousemove":null,
		"onmouseover":"hilight(evt)",
		"onmouseout":"hilight(evt)"
	}
	assignAttr(R,Attr)
}
function hilight(evt){
	var T=evt.target
	if (T.nodeName=="rect") return
	if (evt.type=="mouseover") T.setAttributeNS(null,"stroke-opacity",1)
	else T.setAttributeNS(null,"stroke-opacity",.5)
}
function add(evt){
	if (evt.target.nodeName!="rect") return
	var C=document.createElementNS(xmlns,"circle") 
	var stroke=Color()
	var rad=10+Math.random()*50
	var Attr={
		r:rad,
		cx:evt.clientX,
		cy:evt.clientY,
		"fill": Color(),
		"fill-opacity":.75,
		"stroke": stroke,
		"stroke-opacity":.5,
		"id":stroke,
		"stroke-width":10+Math.random()*(55-rad)
	}
	assignAttr(C,Attr)
	Root.appendChild(C)
}
function grab(evt){
	var O=evt.target
	if (evt.target.nodeName=="rect") return
	var Attr={
		"onmousemove":"slide(evt,'"+O.id+"')",
		"onmouseup":"standardize(Root)"
	}
	assignAttr(Root,Attr)
}
function slide(evt,id){
	var o=document.getElementById(id)
	var c=""; if (o.nodeName=="circle") c="c"
	o.setAttributeNS(null, c+"x", evt.clientX)
	o.setAttributeNS(null, c+"y", evt.clientY)
}
function assignAttr(O,A){
	for (i in A) O.setAttributeNS(null,i, A[i])
}
function Color(){
	return "rgb("+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+","+parseInt(Math.random()*255)+")"
}
]]>
</script>
<rect width="100%" height="100%" fill="white"/>
<text font-size="12pt" x="50" y="20" id="t1">Click something to move it</text>
<text font-size="12pt" x="80" y="40" id="t2">Click nothing to add something</text>
</svg>

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

Creating a DateTime object with Ruby

This example creates a date and time variable which represents 22nd March 2008 4:30pm and 12 seconds.
d2 = DateTime.new(y=200,m=3,d=22, h=16,min=30,s=12)

or convert a date string into a DataTime object:
"17/03/2009 17:48:00"[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]
d2 = DateTime.new(y=$3.to_i,m=$2.to_i,d=$1.to_i, h=$4.to_i,min=$5.to_i,s=$6.to_i)

or convert a date string into a Time object:
"22/03/2008 17:48:00"[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]
iyear = $3.to_i; imonth = $2.to_i; iday = $1.to_i; ihour = $4.to_i; imin = $5.to_i; isec = $6.to_i
twork = Time.local(iyear,imonth,iday,ihour,imin,isec) 
puts 'we still have time to party' if Time.now < twork 

Reference:
Class: DateTime [ruby-doc.org]
Class: Time [ruby-doc.org]

Use Ruby to create a directory

Create a directory named 'temp' if it doesn't already exist. Source: 'Create a new directory if it's missing' http://snippets.dzone.com/posts/show/2554 [snippets.dzone.com] Reference: mkdir_p http://ruby-doc.org/core/classes/FileUtils.html#M004346 [ruby-doc.org]
FileUtils.mkdir_p 'temp'

Creating a date with Ruby

This example creates a date variable which represents 22nd March 2008.
  require 'date'
  d1 = Date.new(y=2008,m=3,d=22)
  puts d1

output: 2008-03-22

Create a 'countries' table for rails

I use rails. It's great. It automatically creates a select list of countries. It doesn't, however, know about the 2 and 3 character country code.

So, I need a country table, with countries.

This migration creates and populates a 'countries' table.

You'll have to

  ./script/generate model Country


before you run it.



class CreateCountryTable < ActiveRecord::Migration
  def self.up
    ## Create country table
    create_table :countries do |t|
      t.column :iso, :string, :size => 2
      t.column :name, :string, :size => 80
      t.column :printable_name, :string, :size => 80
      t.column :iso3, :string, :size => 3
      t.column :numcode, :integer
    end

    Country.reset_column_information
    Country.create(:iso => 'AF', :name => 'AFGHANISTAN', :printable_name => 'Afghanistan', :iso3 => 'AFG', :numcode => '004') 
    Country.create(:iso => 'AL', :name => 'ALBANIA', :printable_name => 'Albania', :iso3 => 'ALB', :numcode => '008') 
    Country.create(:iso => 'DZ', :name => 'ALGERIA', :printable_name => 'Algeria', :iso3 => 'DZA', :numcode => '012') 
    Country.create(:iso => 'AS', :name => 'AMERICAN SAMOA', :printable_name => 'American Samoa', :iso3 => 'ASM', :numcode => '016') 
    Country.create(:iso => 'AD', :name => 'ANDORRA', :printable_name => 'Andorra', :iso3 => 'AND', :numcode => '020') 
    Country.create(:iso => 'AO', :name => 'ANGOLA', :printable_name => 'Angola', :iso3 => 'AGO', :numcode => '024') 
    Country.create(:iso => 'AI', :name => 'ANGUILLA', :printable_name => 'Anguilla', :iso3 => 'AIA', :numcode => '660') 
    Country.create(:iso => 'AG', :name => 'ANTIGUA AND BARBUDA', :printable_name => 'Antigua and Barbuda', :iso3 => 'ATG', :numcode => '028') 
    Country.create(:iso => 'AR', :name => 'ARGENTINA', :printable_name => 'Argentina', :iso3 => 'ARG', :numcode => '032') 
    Country.create(:iso => 'AM', :name => 'ARMENIA', :printable_name => 'Armenia', :iso3 => 'ARM', :numcode => '051') 
    Country.create(:iso => 'AW', :name => 'ARUBA', :printable_name => 'Aruba', :iso3 => 'ABW', :numcode => '533') 
    Country.create(:iso => 'AU', :name => 'AUSTRALIA', :printable_name => 'Australia', :iso3 => 'AUS', :numcode => '036') 
    Country.create(:iso => 'AT', :name => 'AUSTRIA', :printable_name => 'Austria', :iso3 => 'AUT', :numcode => '040') 
    Country.create(:iso => 'AZ', :name => 'AZERBAIJAN', :printable_name => 'Azerbaijan', :iso3 => 'AZE', :numcode => '031') 
    Country.create(:iso => 'BS', :name => 'BAHAMAS', :printable_name => 'Bahamas', :iso3 => 'BHS', :numcode => '044') 
    Country.create(:iso => 'BH', :name => 'BAHRAIN', :printable_name => 'Bahrain', :iso3 => 'BHR', :numcode => '048') 
    Country.create(:iso => 'BD', :name => 'BANGLADESH', :printable_name => 'Bangladesh', :iso3 => 'BGD', :numcode => '050') 
    Country.create(:iso => 'BB', :name => 'BARBADOS', :printable_name => 'Barbados', :iso3 => 'BRB', :numcode => '052') 
    Country.create(:iso => 'BY', :name => 'BELARUS', :printable_name => 'Belarus', :iso3 => 'BLR', :numcode => '112') 
    Country.create(:iso => 'BE', :name => 'BELGIUM', :printable_name => 'Belgium', :iso3 => 'BEL', :numcode => '056') 
    Country.create(:iso => 'BZ', :name => 'BELIZE', :printable_name => 'Belize', :iso3 => 'BLZ', :numcode => '084') 
    Country.create(:iso => 'BJ', :name => 'BENIN', :printable_name => 'Benin', :iso3 => 'BEN', :numcode => '204') 
    Country.create(:iso => 'BM', :name => 'BERMUDA', :printable_name => 'Bermuda', :iso3 => 'BMU', :numcode => '060') 
    Country.create(:iso => 'BT', :name => 'BHUTAN', :printable_name => 'Bhutan', :iso3 => 'BTN', :numcode => '064') 
    Country.create(:iso => 'BO', :name => 'BOLIVIA', :printable_name => 'Bolivia', :iso3 => 'BOL', :numcode => '068') 
    Country.create(:iso => 'BA', :name => 'BOSNIA AND HERZEGOVINA', :printable_name => 'Bosnia and Herzegovina', :iso3 => 'BIH', :numcode => '070') 
    Country.create(:iso => 'BW', :name => 'BOTSWANA', :printable_name => 'Botswana', :iso3 => 'BWA', :numcode => '072') 
    Country.create(:iso => 'BR', :name => 'BRAZIL', :printable_name => 'Brazil', :iso3 => 'BRA', :numcode => '076') 
    Country.create(:iso => 'BN', :name => 'BRUNEI DARUSSALAM', :printable_name => 'Brunei Darussalam', :iso3 => 'BRN', :numcode => '096') 
    Country.create(:iso => 'BG', :name => 'BULGARIA', :printable_name => 'Bulgaria', :iso3 => 'BGR', :numcode => '100') 
    Country.create(:iso => 'BF', :name => 'BURKINA FASO', :printable_name => 'Burkina Faso', :iso3 => 'BFA', :numcode => '854') 
    Country.create(:iso => 'BI', :name => 'BURUNDI', :printable_name => 'Burundi', :iso3 => 'BDI', :numcode => '108') 
    Country.create(:iso => 'KH', :name => 'CAMBODIA', :printable_name => 'Cambodia', :iso3 => 'KHM', :numcode => '116') 
    Country.create(:iso => 'CM', :name => 'CAMEROON', :printable_name => 'Cameroon', :iso3 => 'CMR', :numcode => '120') 
    Country.create(:iso => 'CA', :name => 'CANADA', :printable_name => 'Canada', :iso3 => 'CAN', :numcode => '124') 
    Country.create(:iso => 'CV', :name => 'CAPE VERDE', :printable_name => 'Cape Verde', :iso3 => 'CPV', :numcode => '132') 
    Country.create(:iso => 'KY', :name => 'CAYMAN ISLANDS', :printable_name => 'Cayman Islands', :iso3 => 'CYM', :numcode => '136') 
    Country.create(:iso => 'CF', :name => 'CENTRAL AFRICAN REPUBLIC', :printable_name => 'Central African Republic', :iso3 => 'CAF', :numcode => '140') 
    Country.create(:iso => 'TD', :name => 'CHAD', :printable_name => 'Chad', :iso3 => 'TCD', :numcode => '148') 
    Country.create(:iso => 'CL', :name => 'CHILE', :printable_name => 'Chile', :iso3 => 'CHL', :numcode => '152') 
    Country.create(:iso => 'CN', :name => 'CHINA', :printable_name => 'China', :iso3 => 'CHN', :numcode => '156') 
    Country.create(:iso => 'CO', :name => 'COLOMBIA', :printable_name => 'Colombia', :iso3 => 'COL', :numcode => '170') 
    Country.create(:iso => 'KM', :name => 'COMOROS', :printable_name => 'Comoros', :iso3 => 'COM', :numcode => '174') 
    Country.create(:iso => 'CG', :name => 'CONGO', :printable_name => 'Congo', :iso3 => 'COG', :numcode => '178') 
    Country.create(:iso => 'CD', :name => 'CONGO, THE DEMOCRATIC REPUBLIC OF THE', :printable_name => 'Congo, the Democratic Republic of the', :iso3 => 'COD', :numcode => '180') 
    Country.create(:iso => 'CK', :name => 'COOK ISLANDS', :printable_name => 'Cook Islands', :iso3 => 'COK', :numcode => '184') 
    Country.create(:iso => 'CR', :name => 'COSTA RICA', :printable_name => 'Costa Rica', :iso3 => 'CRI', :numcode => '188') 
    Country.create(:iso => 'CI', :name => 'COTE D\'IVOIRE', :printable_name => 'Cote D\'Ivoire', :iso3 => 'CIV', :numcode => '384') 
    Country.create(:iso => 'HR', :name => 'CROATIA', :printable_name => 'Croatia', :iso3 => 'HRV', :numcode => '191') 
    Country.create(:iso => 'CU', :name => 'CUBA', :printable_name => 'Cuba', :iso3 => 'CUB', :numcode => '192') 
    Country.create(:iso => 'CY', :name => 'CYPRUS', :printable_name => 'Cyprus', :iso3 => 'CYP', :numcode => '196') 
    Country.create(:iso => 'CZ', :name => 'CZECH REPUBLIC', :printable_name => 'Czech Republic', :iso3 => 'CZE', :numcode => '203') 
    Country.create(:iso => 'DK', :name => 'DENMARK', :printable_name => 'Denmark', :iso3 => 'DNK', :numcode => '208') 
    Country.create(:iso => 'DJ', :name => 'DJIBOUTI', :printable_name => 'Djibouti', :iso3 => 'DJI', :numcode => '262') 
    Country.create(:iso => 'DM', :name => 'DOMINICA', :printable_name => 'Dominica', :iso3 => 'DMA', :numcode => '212') 
    Country.create(:iso => 'DO', :name => 'DOMINICAN REPUBLIC', :printable_name => 'Dominican Republic', :iso3 => 'DOM', :numcode => '214') 
    Country.create(:iso => 'EC', :name => 'ECUADOR', :printable_name => 'Ecuador', :iso3 => 'ECU', :numcode => '218') 
    Country.create(:iso => 'EG', :name => 'EGYPT', :printable_name => 'Egypt', :iso3 => 'EGY', :numcode => '818') 
    Country.create(:iso => 'SV', :name => 'EL SALVADOR', :printable_name => 'El Salvador', :iso3 => 'SLV', :numcode => '222') 
    Country.create(:iso => 'GQ', :name => 'EQUATORIAL GUINEA', :printable_name => 'Equatorial Guinea', :iso3 => 'GNQ', :numcode => '226') 
    Country.create(:iso => 'ER', :name => 'ERITREA', :printable_name => 'Eritrea', :iso3 => 'ERI', :numcode => '232') 
    Country.create(:iso => 'EE', :name => 'ESTONIA', :printable_name => 'Estonia', :iso3 => 'EST', :numcode => '233') 
    Country.create(:iso => 'ET', :name => 'ETHIOPIA', :printable_name => 'Ethiopia', :iso3 => 'ETH', :numcode => '231') 
    Country.create(:iso => 'FK', :name => 'FALKLAND ISLANDS (MALVINAS)', :printable_name => 'Falkland Islands (Malvinas)', :iso3 => 'FLK', :numcode => '238') 
    Country.create(:iso => 'FO', :name => 'FAROE ISLANDS', :printable_name => 'Faroe Islands', :iso3 => 'FRO', :numcode => '234') 
    Country.create(:iso => 'FJ', :name => 'FIJI', :printable_name => 'Fiji', :iso3 => 'FJI', :numcode => '242') 
    Country.create(:iso => 'FI', :name => 'FINLAND', :printable_name => 'Finland', :iso3 => 'FIN', :numcode => '246') 
    Country.create(:iso => 'FR', :name => 'FRANCE', :printable_name => 'France', :iso3 => 'FRA', :numcode => '250') 
    Country.create(:iso => 'GF', :name => 'FRENCH GUIANA', :printable_name => 'French Guiana', :iso3 => 'GUF', :numcode => '254') 
    Country.create(:iso => 'PF', :name => 'FRENCH POLYNESIA', :printable_name => 'French Polynesia', :iso3 => 'PYF', :numcode => '258') 
    Country.create(:iso => 'GA', :name => 'GABON', :printable_name => 'Gabon', :iso3 => 'GAB', :numcode => '266') 
    Country.create(:iso => 'GM', :name => 'GAMBIA', :printable_name => 'Gambia', :iso3 => 'GMB', :numcode => '270') 
    Country.create(:iso => 'GE', :name => 'GEORGIA', :printable_name => 'Georgia', :iso3 => 'GEO', :numcode => '268') 
    Country.create(:iso => 'DE', :name => 'GERMANY', :printable_name => 'Germany', :iso3 => 'DEU', :numcode => '276') 
    Country.create(:iso => 'GH', :name => 'GHANA', :printable_name => 'Ghana', :iso3 => 'GHA', :numcode => '288') 
    Country.create(:iso => 'GI', :name => 'GIBRALTAR', :printable_name => 'Gibraltar', :iso3 => 'GIB', :numcode => '292') 
    Country.create(:iso => 'GR', :name => 'GREECE', :printable_name => 'Greece', :iso3 => 'GRC', :numcode => '300') 
    Country.create(:iso => 'GL', :name => 'GREENLAND', :printable_name => 'Greenland', :iso3 => 'GRL', :numcode => '304') 
    Country.create(:iso => 'GD', :name => 'GRENADA', :printable_name => 'Grenada', :iso3 => 'GRD', :numcode => '308') 
    Country.create(:iso => 'GP', :name => 'GUADELOUPE', :printable_name => 'Guadeloupe', :iso3 => 'GLP', :numcode => '312') 
    Country.create(:iso => 'GU', :name => 'GUAM', :printable_name => 'Guam', :iso3 => 'GUM', :numcode => '316') 
    Country.create(:iso => 'GT', :name => 'GUATEMALA', :printable_name => 'Guatemala', :iso3 => 'GTM', :numcode => '320') 
    Country.create(:iso => 'GN', :name => 'GUINEA', :printable_name => 'Guinea', :iso3 => 'GIN', :numcode => '324') 
    Country.create(:iso => 'GW', :name => 'GUINEA-BISSAU', :printable_name => 'Guinea-Bissau', :iso3 => 'GNB', :numcode => '624') 
    Country.create(:iso => 'GY', :name => 'GUYANA', :printable_name => 'Guyana', :iso3 => 'GUY', :numcode => '328') 
    Country.create(:iso => 'HT', :name => 'HAITI', :printable_name => 'Haiti', :iso3 => 'HTI', :numcode => '332') 
    Country.create(:iso => 'VA', :name => 'HOLY SEE (VATICAN CITY STATE)', :printable_name => 'Holy See (Vatican City State)', :iso3 => 'VAT', :numcode => '336') 
    Country.create(:iso => 'HN', :name => 'HONDURAS', :printable_name => 'Hondu