CoderBlog.NET
Tengiz Tutisani's blog.

 
Useful Links:
 
LanguageBox 
 
Language

 
SearchBox 
 
 
ArchiveBox 
 
Archive
<February 2012>
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
 
CategoriesBox 
 
Categories
  ASP.NET (5)
  Ajax (1)
  C# (7)
  Common (1)
  HTML (3)
  JavaScript (5)
  Products (1)
  SEO (1)
  SQL (1)
  Visual Studio (1)
  Windows (1)
 
FriendSitesBox 
 
Friend Sites
  AdMoney Project
  Cайт о гитаре и музыке!
  Design Studio IMAGINE
 
CustomContentBox [1] 
 



 
 
HTML Editing
How it works
Posted By:   tengo
At:   5/8/2009 9:55:00 AM
 

Did you know that almost any HTML control can be switched into edit mode? This effect can be achieved easily: just add CONTENTEDITABLE attribute to the component that must be an editor.

Place the following tag into the source of you HTML page and view it in browser:

<div CONTENTEDITABLE>some editable text</div>

As you can see, the text "some editable text" is editable now. In the easiest editing scenario, you can copy-paste the content from Word document to that editable area. For more complicated scenarios you can use document.execCommand method. (For advanced scenarios you can use a free and ready to use HTML editor tinyMCE).
Now let's talk about how you can get the content of that div as HTML. Depending on your needs, you can just get the content and do some operation with it. In my practice I had few situations when I wanted the content to be submitted to the server. As you probably know, only HTML input controls have ability to submit their values to the server. For this purpose, here I consider the code fragment that shows how to move the HTML value of the editable div to a Textarea control that actually can submit its value. The Input-button tag is added to move the content from one control to another.

<div id="text" CONTENTEDITABLE>some editable text</div>
<textarea id="content"></textarea>
<input type="button" value="get HTML content" onclick="content.value=text.innerHTML">

That's all, a little but sometimes very useful thing.
And one more: Button element can be created using not only the standard input tag, but also using a button tag like this:

<button value="some value">some text</button>

Note, that the button has a text "some text" instead of "some value" as you might think at a first look.

Thanks for your time.


Filed Under:   JavaScript | HTML |
Post Tags:   button | contenteditable | div | editor | html | javascript | submit | textarea | value |
  Comments [0]



 
 
Table layout vs. DIV layout
Today's topic
Posted By:   tengo
At:   4/15/2009 2:40:00 PM
 

Today I was discussing this topic with one guy, who is interested in web development. As everyone, he also visits a lot of development forums and finds different approaches of the web site design, discussed on the forums. He said to me that he's interested to learn how to build a web page layout using DIV instead of tables, he gave me a link to the forum topic, where I found an idea of a developer, who supposes this kind of layout is more professional.
I don't want to criticize the developer, any person can have his/her own mind, but my mind sounds a little different than the one from the forum.

Before I continue writing around the topic, for those who don't know what we're talking about:
Table layout means that page's html source contains a <table> tag inside of it's <body> tag, such way, layout and positioning of each element depends on the table structure and layout, as all other elements of the page would be it's child elements.
DIV layout means that every element placed on the page is a child of a <div> element, or is itself, a <div> element. That means layout and positioning of all the elements are controlled by <div> elements.

As I think, both of the solutions are useful, interesting and rather professional. If you ask which one do I prefer, a table layout is the one I prefer.
But I want to mention, in the case of professional and correct using of these layouts, the result will be the same, be sure in that.

I didn't like the topic of the forum that I have read, because there was said that the table layout structure is old enough and in our days it's important to use only a DIV layout. As I understood the author has ambitions to use DIV layout because this one is developed later and supposed to be called as a newer technology approach. But I would say, it's not a reason to throw away a table layout.
At first, if table layout approach is old, that means that this approach is more supported by all the browsers as a default than the new one. Have in mind, none of a browser developer companies have announced the end of table layout support, am I right? (We all remember how everyone moved away from using frames, because such recommendations where announced)
At a second, I would say, a good developer must be familiar with both of these approaches, but not only with one of them.

One more thing: the guy asked me to explain the following fact:
He placed two div elements on the html page, on one line, and then he just resized the browser window, and when the size was smaller that the area containing both DIVs, the second div moved down, under the first one.

It happens because nothing has told to the second div to remain the position in first line, it's like you wrote a paragraph of text, which took several line on the page. Of course in the case of resizing the window, words of the paragraph move and change their line positions depending on the size of the browser's window. Imagine the div is a word, so everything is clear. The div moves to the next line when there is not enough space for it on the current line, like a word in the text.

How to avoid this:
Use a table layout. (A little funny, all the topic was about it)
In details: Insert a table with two cells in a row, place one div in each cells, they will remain in the same row, because table cells don't change their line positions depending on the browser's window size.

But I must say, there is another way also, using DIV layout:
Insert a div element having a width more than both of divs together, and place both divs inside the last inserted div element. That's all, but be careful, if someone entering your web site has a low resolution of a screen, he will have to scroll to the right and left to see everything on your page. But in some cases, using a table layout can bring the same problem.

Several other hints:
To make divs position on the same line, add the following attribute to both of them: style="float: left;"
To assign a width to the div, use the same style attribute as follows: <div style="width: 500px"></dv>

That's all for today.


Filed Under:   HTML |
Post Tags:   design | div | html | layout | style | table |
  Comments [0]



 
 
Javascript Drawing Framework
Making world better
Posted By:   tengo
At:   4/10/2009 6:00:00 PM
 

Hello everyone. How about making world better? Sounds good.

Do you know that JavaScript doesn't allow drawing? I wanted to create some drawing capabilities for some reason. However, it won't be very important, if your web site is built using ASP.NET technology. There is a great way for generating images dynamically in asp.net.
A little more difficulties are met while using PHP, or no server side scripting. I remember once I had a web-site written in PHP, but a hosting server was not supporting image generating. Thus, I had no chance to draw any user friendly images to the client browser.
And of course, there is no way to generate any kind of images without server side script hosting.
Yesterday I decided to write a little JavaScript code. And today I want to consider that code with you.
My only wish was, to create some drawing capabilities, as simple as possible. Sometimes a human wants to terminate the borders, so called Rules. In the end of this article you will understand that there can be created something for drawing purposes in JavaScript.
As there is nothing for drawing in JavaScript, the first thing that I needed is something that would be a drawing board, and as long as JavaScript works well with HTML components (furthermore, it's created for working with HTML components), I decided to use them as a boards. Decision stopped on the html DIV component. So I placed the DIV on my empty HTML page.
Then the logic built itself from the scratch.

What is drawing? What means to draw a line, a circle, rectangle, etc.? Any of them contain points, right? Anything can be drawn if we can draw a point. So, let's start thinking how we can do that.
I remember my first impression, when I looked at the picture containing just a point very closely. When I zoomed into a 500%, I was surprised; point became a rectangle, great effect of the computer technologies. Think of that as a great point to start from, indeed everything placed on the HTML page is displayed like rectangles. Did you catch an idea? Yes, I mean that we can create a component on the drawing board (actually HTML DIV component), with a very little sizes, and believe me, that wouldn't be nothing else than a point. Let's see how I wrote a code:

function Graphics_DrawPoint(x,y)
{
var div = document.getElementById(DivID);
var point = document.createElement("<div>");
point.style.position="absolute";
point.style.top=document.body.offsetTop + div.offsetTop + y;
point.style.left=document.body.offsetLeft + div.offsetLeft + x;
point.style.backgroundColor=Brush_Color;
point.style.width=Brush_Size;
point.style.height=Brush_Size;
div.appendChild(point);
}

Now, as we can draw a point, we can draw a line as well. The only thing we need is to remember a formula of line from the mathematics. As a theory says, if we have two points (x0,y0) and (x1,y1), then any (x,y) point on the line will satisfy the following formula: (x-x0)/(x1-x0) = (y-y0)/(y1-y0). So how can we use that? If given two points, we must generate all the points between them. Of course it will be easier if we generate only one from the pair of coordinates, and then calculate another one using the first one. So, functions calculating x using y and vice versa will look like this:

function Graphics_CalculateLineX(y,x0,x1,y0,y1)
{
return ((y-y0)*(x1-x0)/(y1-y0))+x0;
}

function Graphics_CalculateLineY(x,x0,x1,y0,y1)
{
return ((x-x0)*(y1-y0)/(x1-x0))+y0;
}

and after these functions we can of course write a function that will draw a line (having in mind the fact we already can draw a point):

function Graphics_DrawLine(x0,y0,x1,y1)
{
var x;
var y;
if (Math.abs(x0-x1)>Math.abs(y0-y1))
{
x = Math.min(x0,x1);
while (x<x1)
{
y=Graphics_CalculateLineY(x,x0,x1,y0,y1);
Graphics_DrawPoint(x,y);
x++;
}
}
else
{
y = Math.min(y0,y1);
while (y<y1)
{
x=Graphics_CalculateLineX(y,x0,x1,y0,y1);
Graphics_DrawPoint(x,y);
y++;
}
}
}

Very easy, isn't it?
Now let's think a little about a fact, that all these functions are executed on the client machine, meaning that they are using a memory of the client machine. As more points are drawn as more memory is needed. But don't forget a fact that: in our days, when the internet network is developing and growing, this must not be a problem. Any web page, that is accessed across the web, using a simple HTML browser, parses and displays a large amount of HTML components. To prove that, you can open any average size web site and view it's HTML source. But anyway, trying to make a world better must not become a warranty of memory overflow. Yes, a developer must make a decision to make a little pressing on the client machine during execution of some code, if there's no other way to do that. My next functions will show that some drawing purposes can be achieved without crating a large amount of points dynamically.
Next step is to draw a rectangle. As mentioned above, I'm not going to draw the rectangle using lines, because in this case I consider a rectangle that is positioned so as its sides are parallel to the edges of our screens. Any other king of rectangles, or polygons, can be drawn using a line drawing functions. In my case, rectangle is the same starting point, saying that every component on an html page is a simple rectangle. So, a task of drawing rectangle becomes a task of creating a transparent component that displays only borders. Those borders will appear as a rectangle itself. It will work fast, and the solution will be the same as needed:

function Graphics_DrawRectangle(x0,y0,x1,y1)
{
var div = document.getElementById(DivID);
var rect = document.createElement("<div>");
rect.style.position="absolute";
rect.style.top=document.body.offsetTop + div.offsetTop + Math.min(y0,y1);
rect.style.left=document.body.offsetLeft + div.offsetLeft + Math.min(x0,x1);
rect.style.background="transparent";
rect.style.border=Brush_Color + " " + Brush_Size + "px solid";
rect.style.width=Math.abs(x1-x0);
rect.style.height=Math.abs(y1-y0);
div.appendChild(rect);
}

I don't want to waste a time explaining how to create a function that will allow us drawing a filled rectangle. Think a little about it.
An interesting step is to draw a circle. Let's again, remember a little mathematics. Again, a formula says that if we draw a circle with center at point with coordinates (0,0) then any point with coordinates (x,y) satisfies any of the next two formulas: 1) x*x + y*y = r*r. 2) sin(alpha) = y/r, cos(alpha) = x/r. where alpha is an angle calculated in the backward direction of a clock. First formula is a little more difficult to use, because calculating all x and y coordinates will take a little more time. It's easier to change alpha from 0 to 360 and find all the coordinates. But wait, the circle's bottom side repeats it's upper side, so we can calculate only one side from them and draw the same points as transformed to the middle line of the circle... But wait again, we can see that a part of circle contained between angles 90 and 180 repeats a part contained between angles 0 and 90, and applying the same logic we can calculate only a one-forth part of the circle and draw all other points as transformed to the middle line in horizontal or in vertical direction. And one more thing, as I found the topic in the documentation, JavaScript Math.sin() function needs a parameter in radians, not in degrees. So a function looks like this:

function Graphics_DrawCircle(x,y,r)
{
var div = document.getElementById(DivID);
var alpha = 0;
while (alpha <= (Math.PI/2))
{
var x1 = r * Math.cos(alpha);
var y1 = r * Math.sin(alpha);
Graphics_DrawPoint(x+x1,y+y1);
Graphics_DrawPoint(x+x1,y-y1);
Graphics_DrawPoint(x-x1,y+y1);
Graphics_DrawPoint(x-x1,y-y1);

alpha += (Math.PI/180);
}
}

Ok. I think I have explained a big amount of my thoughts about JavaScript drawing capabilities. All the code can be found in the attached file. I just changed a code so that callings to these functions look like object oriented, like this:

var g = new Graphics("divContainer");
g.Brush.Color='green';
g.DrawLine(10,10,200,50);

There are also added a little more functions, like displaying a string and filling a rectangle, and of course drawing an arc. You can add more and more functions as your needs request.
When you can use it?
For example, when displaying a result of user surveys on the page, and indeed without any ready images saved on the server. A simple shape can be drawn without any other tools.

I hope you found something interesting here, in this topic.

Have a good day!

Attached File: Sample.htm


Filed Under:   JavaScript |
Post Tags:   arc | circle | component | div | draw | fill | html | javascript | rectangle |
  Comments [2]



 
CustomContentBox [2] 
 
Useful Links:
 
CategoryViewBox 
 
CoderBlog.NET
  Get your own Blog Engine
  SMS Sending Service
  What do you want to see on CoderBlog.NET?
  IMAGINE.GE created a header image for CoderBlog.NET
  CoderBlog.NET offers link exchange
  Quick language selection added
  CoderBlog.NET ‘s what is what
  Welcome to CoderBlog.NET

  Jump to Category...
 
TagsBox 
 
Popular Tags
aspnetblogcsharpdesigndivengineerrorformhtmljavascript
 
AuthorsBox 
 
Authors
  Tengiz Tutisani  (25)
 
SubscriptionsBox 
 
Subscriptions
Your Email:
 
LoginStatusBox 
 
Site Members
Login
 
Copyright © 2009 . CoderBlog.NET