Forum Moderators: open

Message Too Old, No Replies

drawing rectangle

         

yllai

5:15 am on Aug 19, 2004 (gmt 0)

10+ Year Member



can i draw a rectangle with javascript?

Bernard Marx

7:30 am on Aug 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



window.onload = function()
{
document.body.appendChild( rectangle(200,100,250,250,"blue","red") )
}

function rectangle(w,h,x,y,bg,bCol)
{
var rect = document.createElement("div");
var style = rect.style;
style.width = w +"px";
style.height = h +"px";
style.left = x +"px";
style.top = y +"px";
style.backgroundColor = bg;
style.borderColor = bCol;
// fixed
style.position = "absolute";
style.borderStyle = "solid"
style.borderWidth = 1;
return rect
}

yllai

12:47 am on Aug 20, 2004 (gmt 0)

10+ Year Member



how about if i want to add some text in the rectangle?
can i set the font type and font size?

Bernard Marx

12:54 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Simple..
..
style.fontSize = "18px"
style.font = "fantasy"
rect.innerHTML = "hello"
..

Style property names are formed by removing the hyphens (-) from the normal css-version, and using interCapping.

font-size --> fontSize

yllai

1:19 am on Aug 20, 2004 (gmt 0)

10+ Year Member



after i add in 3 lines above, it does not show any thing..is it any problem for the way i do it?

yllai

3:30 am on Aug 20, 2004 (gmt 0)

10+ Year Member



where can i get others similar example?

Bernard Marx

7:26 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> after i add in 3 lines above, it does not show any thing..is it any problem for the way i do it?

You'll have to post your new version of the function.

> where can i get others similar example?

Google: DHTML beginners tutorial [google.com]

I would recommend concentrating as much as possible on learning core Javascript first.

This site is good: [howtocreate.co.uk...]

yllai

7:51 am on Aug 20, 2004 (gmt 0)

10+ Year Member



<script language="JavaScript" type="text/JavaScript">
window.onload = function() {
document.body.appendChild( rectangle(200,100,250,250,"blue","red") )
}

function rectangle(w,h,x,y,bg,bCol) {
var rect = document.createElement("div");
var style = rect.style;
style.width = w +"px";
style.height = h +"px";
style.left = x +"px";
style.top = y +"px";
style.backgroundColor = bg;
style.borderColor = bCol; // fixed
style.position = "absolute";
style.borderStyle = "solid"
style.borderWidth = 1;
style.fontSize = "18px";
style.font = "Arial";
rect.innerHTML = "hello";

return rect }

yllai

7:55 am on Aug 20, 2004 (gmt 0)

10+ Year Member



thanks..i fixed the problem..

the font type syntax should be:

style.fontfamily="Arial";

Bernard Marx

8:00 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Silly me (although you can use the font shortcut property too, but it's picky about how you use it).

yllai

8:21 am on Aug 20, 2004 (gmt 0)

10+ Year Member



i have tried to put the text in the center of the rectangle,, i coded as style.textalign="center"; but it doesnt show it in the center...any ideas?

Bernard Marx

8:46 am on Aug 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



style.textAlign = "center"

See msg #4 :)

Remember that with innerHTML you can insert HTML code:

rect.innerHTML = "<p>hello</p><p>goodbye</p>"

..and that innerHTML is not a "standard" property. It's just a MS proprietary property that most other browsers now support too - because it's so useful.

yllai

9:29 am on Aug 20, 2004 (gmt 0)

10+ Year Member



thanks a lot...

yllai

2:38 am on Aug 23, 2004 (gmt 0)

10+ Year Member



if i want to include these script for drawing rectangle in php, how i do it?

i had try on it, but it cant work. i using for loop because i want to draw a few rectangle based on user request...can i do it? any ideas or examples?

Bernard Marx

8:25 am on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



in php?

yllai

8:57 am on Aug 23, 2004 (gmt 0)

10+ Year Member



yes

Bernard Marx

10:30 am on Aug 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the moment, I don't see the connection with PHP - sorry!

A page containing Javascript can be served with PHP, but that's a different issue.
What have you tried so far? Is the code postable?