Connecting Tech Pros Worldwide Help | Site Map

javascript basics draw a line

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 24th, 2005, 07:45 PM
R
Guest
 
Posts: n/a
Default javascript basics draw a line

Hello everybody!

I'm newbie to JavaScript and I have a basic question.

I have X and Y coordinates of A and B points.

How can I draw a line connecting A and B?

thanks in advance
cheers
R


  #2  
Old July 24th, 2005, 07:55 PM
web.dev
Guest
 
Posts: n/a
Default Re: javascript basics draw a line

Hi R,

R wrote:[color=blue]
> Hello everybody!
>
> I'm newbie to JavaScript and I have a basic question.
>
> I have X and Y coordinates of A and B points.
>
> How can I draw a line connecting A and B?
>
> thanks in advance
> cheers
> R[/color]

Javascript does not have a mechanism to do graphics (such as drawing a
line). But there are ways to simulate this. Check out this page:
http://www.walterzorn.com/jsgraphics/jsgraphics_e.htm

  #3  
Old July 24th, 2005, 08:35 PM
ASM
Guest
 
Posts: n/a
Default Re: javascript basics draw a line

R wrote:[color=blue]
> Hello everybody!
>
> I'm newbie to JavaScript and I have a basic question.
>
> I have X and Y coordinates of A and B points.
>
> How can I draw a line connecting A and B?[/color]

you take your ruler and your pencil and trace it

not possible in JavaScript

you can simulate a vertical or horizontal line
with a div reduced to 1 px (width or height)
and no more

You can also use an image of a sqare with a diagonal drawn
and resize the image betwen the 2 points

+---+ B
| /|
| / |
|/ |
+---+
A



--
Stephane Moriaux et son [moins] vieux Mac
  #4  
Old July 24th, 2005, 11:45 PM
Randy Webb
Guest
 
Posts: n/a
Default Re: javascript basics draw a line

R said the following on 7/24/2005 3:32 PM:
[color=blue]
> Hello everybody!
>
> I'm newbie to JavaScript and I have a basic question.
>
> I have X and Y coordinates of A and B points.
>
> How can I draw a line connecting A and B?
>[/color]

<URL: http://members.aol.com/hikksnotathom...hit/index.html >

Something like that?
Click two points in the blue, it "draws" a red line between them.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
  #5  
Old July 25th, 2005, 07:05 AM
Christopher J. Hahn
Guest
 
Posts: n/a
Default Re: javascript basics draw a line

Randy Webb wrote:[color=blue]
> R said the following on 7/24/2005 3:32 PM:
>[color=green]
> > Hello everybody!
> >
> > I'm newbie to JavaScript and I have a basic question.
> >
> > I have X and Y coordinates of A and B points.
> >
> > How can I draw a line connecting A and B?
> >[/color]
>
> <URL: http://members.aol.com/hikksnotathom...hit/index.html >
>
> Something like that?
> Click two points in the blue, it "draws" a red line between them.
> --
> Randy
> comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly[/color]

I must say, I've always toyed with the idea of line drawing but... 400
DIVs is a bit excessive, in my opinion. It's an incredibly brute-force
solution, but it does get the mental cogs clicking.

Seems simpler to JS-generate an arbitrary number of spans with custom
widths based on each line's pixel requirements. The height would always
be 1px, and the top would always be 1px different from the previous.

i.e. for a line 0,0 to 100,100, JS-create 100 spans of 1px width, with
incremented values for top.

For a line 0,0 to 100,1, JS-create two spans of 50px width, the first
from 0,0 to 50,0, and the second from 50,1 to 100,1.

Interesting... I may end up doodling out a Line object because of this
madness.

hm.

Always seemed of very limited practical use, though.

  #6  
Old July 25th, 2005, 03:35 PM
VK
Guest
 
Posts: n/a
Default Re: javascript basics draw a line



Christopher J. Hahn wrote:[color=blue]
> Randy Webb wrote:[color=green]
> > R said the following on 7/24/2005 3:32 PM:
> >[color=darkred]
> > > Hello everybody!
> > >
> > > I'm newbie to JavaScript and I have a basic question.
> > >
> > > I have X and Y coordinates of A and B points.
> > >
> > > How can I draw a line connecting A and B?
> > >[/color]
> >
> > <URL: http://members.aol.com/hikksnotathom...hit/index.html >
> >
> > Something like that?
> > Click two points in the blue, it "draws" a red line between them.
> > --
> > Randy
> > comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly[/color]
>
> I must say, I've always toyed with the idea of line drawing but... 400
> DIVs is a bit excessive, in my opinion. It's an incredibly brute-force
> solution, but it does get the mental cogs clicking.
>
> Seems simpler to JS-generate an arbitrary number of spans with custom
> widths based on each line's pixel requirements. The height would always
> be 1px, and the top would always be 1px different from the previous.
>
> i.e. for a line 0,0 to 100,100, JS-create 100 spans of 1px width, with
> incremented values for top.
>
> For a line 0,0 to 100,1, JS-create two spans of 50px width, the first
> from 0,0 to 50,0, and the second from 50,1 to 100,1.
>
> Interesting... I may end up doodling out a Line object because of this
> madness.
>
> hm.
>
> Always seemed of very limited practical use, though.[/color]


Why so complicated?! Internet Explorer has build-in VML module (IE 5
and higher). For others there is SVG plug-in.
Both VML and SVG can be fully JavaScript driven, so you can do
anything: from simple lines to interactive virtual worlds.

I'm just started with SVG (after VML), so instead of the requested line
I used a 3rd party sample. But the idea is rather clear I hope.


<html xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<title>VML Test</title>

<script type="text/javascript">
function drawLine() {
/*@cc_on @*/
/*@if (@_jscript_version >= 4)
var A = [10,150];
var B = [400,200];
var myLine = document.getElementById('line01');
myLine.from = A;
myLine.to = B;
/*@end @*/
}
window.onload = drawLine;
</script>

<style type="text/css">
v\:* { behavior: url(#default#VML);}
body { background-color: #FFFFFF}
</style>

</head>

<body>

<!--[if gte IE 5]>
<v:group id="vmlApp"
style="position: absolute; left: 50px; top: 50px; height: 300px;
width: 500px"
coordorigin="0,0"
coordsize="500,300">
<v:rect id="vmlMainFrame"
style="position:absolute; top:0px; left:0px; width:500px;
height:300px">
<v:fill
color="#FFFFFF"/>
<v:stroke
color="#000000"
weight="2px"/>
</v:rect>
<v:line id="line01">
</v:line>
</v:group>
<script type="text/JScript">
self.isVMLEnabled = true;
</script>
<![endif]-->
<script>
if(self.isVMLEnabled == undefined) {
document.write('<embed width="222" height="300"
src="http://www.carto.net/papers/svg/samples/shapes.svg"
name="printable_map" type="image/svg+xml">');
}
</script>
</body>
</html>


More interesting reading:

<http://msdn.microsoft.com/library/default.asp?url=/workshop/author/vml/>
<http://www.mozilla.org/projects/svg/>
<http://plugindoc.mozdev.org/windows.html>

  #7  
Old July 26th, 2005, 02:05 PM
VK
Guest
 
Posts: n/a
Default Re: javascript basics draw a line

If anyone is still interested:

A ready to use VML/SVG compatible block is located here:

<http://www.geocities.com/schools_ring/VML_SVG.html>

To get a copy of the page w/o Yahoo's crap go to:
<<http://www.geocities.com/schools_ring/VML_SVG.zip>

This test line drawer is fully functional for IE users from the scratch
(no additional soft is needed).

For other browsers it will be proposed to install Adobe SVG plugin.

SVG version of the line drawer is not ready (I'm still studying SVG) so
a 3rd party placeholder is used.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.