Google

Showing posts with label Javascript. Show all posts
Showing posts with label Javascript. Show all posts

Learn to Make Games in Javascript

Monday, July 23, 2007

Michal Wallace from javascriptgamer.com have written a great tutorial on how to make a game, using only javascript ;)


This is a very nice tutorial especially to understand javascript a little bit more ;) well, the javascript that were being used is prototype.

The tutorials covers all of the basic javascript syntax to the OOP side of it :) With lots of addition e.g: Event handling, Sound, AJAX, etc.

Of course in order to understand this, you must have the prequisite knowledge of HTML and CSS ;) Which I'm sure you already have mastered it ;)

If you followed the tutorial step-by-step you will have a nice looking BrickSlayer in your web-page ;) and who knows if you can be a guru and built this kinda game ;)





[via: Simon Willison’s Weblog]

Blogger: Goodies Inside Blogger Javascript !

Friday, January 05, 2007

Today, i spent time looking at the (unnoticed) javascripts files that Blogger defaultly included in your blog.

I say that it was unnoticed, in terms that maybe just a few person, who will look at it completely, and maybe even play with it ! And that's including me :P

But now, with the recent EQ in Taiwan, i had experienced the slowness of my blog, even that i've reduced the size of my javascript. So i tried to eliminate some javascripts that i won't be needed anymore.

And guess what ? Blogger already GIVE US all the basic functions that WE NEED !! My gawd ! I wish i dwelled into these things earlier !

You see, currently Blogger provided us with 3 handy javascripts, which i'm sure it'll overcome the cross-browsers problem, it's GOOGLE's ! And the javascripts are :

- common.js
- dom.common.js
- cookies.common.js

Now i'm gonna REVEAL all the GOODIES inside these, and then you can GET RID of those prototype or framework or anything that you used :P (i'm planning to do so in the next version of zlyther ;) so stay tuned ... ) Note that i don't list all of the functions. But i guess i list almost all of it ! Brilliant Google indeed :P

__________________________________________________________________

1. common.js

Cookies

  • function SetCookie(name, value, opt_max_age, opt_path, opt_domain)
    Use for: Setting a cookie.

  • function ExpireCookie(name, opt_path, opt_domain)
    User for: Expiring a cookie.

  • function GetCookie(name)
    Use for: Returning the value for the first cookie with the given name.

Time
  • function Now()
    Use for: Returning the current Time.

Dynamic HTML/DOM utilities
  • function GetElement(win, id)
    Use for: Getting a element by its id, and throws an exception if it's null

  • function GetParentNode(n)
    Use for: Getting the parent of a html element.

  • function GetAttribute(node, attribute)
    Use for: Getting attribute value of a DOM node

  • function SetInnerHTML(win, id, html)
    Use for: Setting inner html of a html element

  • function GetInnerHTML(win, id)
    Use for: Getting inner-html of a html element

  • ClearInnerHTML(win, id)
    Use for: Clearing inner html of a html element

  • function SetCssStyle(win, id, name, value)
    Use for: Setting a CSS style of an element

  • function GetStyleProperty(style, name)
    Use for: Getting CSS property from a style attribute string

  • function ShowElement(el, show)
    Use for: Showing/hiding an element.

  • function ShowBlockElement(el, show)
    Use for: Showing/hiding a block element. The previous ShowElement() doesn't work if object has an initial class with display:none

  • function ShowInlineElement(el, show)
    Use for: Showing/hiding an inline element. The previous ShowElement() doesn't work when an element starts off display:none.

  • function SetButtonText(button, text)
    Use for: Setting the text of a button. This is to get around a bug in mozilla, where we can't set the text of a button by setting innerHTML.

  • function AppendNewElement(win, parent, tag)
    Use for: Appending a new HTML element to a HTML node.

  • function FindChildWithID(parent, id)
    Use for: Finding the child with the given ID, or null if there is node. This does not search the children's children.

  • function CreateDIV(win, id)
    Use for: Creating a new DIV (append it to the end of the document)

  • function Tr(win, tds)
    Use for: Creating a new TR containing the given td's

  • function Td(win, opt_colspan)
    Use for: Creating a new TD, with an optional colspan

  • function HasClass(el, cl)
    Use for: Checking if an element has a given class

  • function AddClass(el, cl)
    Use for: Adding a class to element

  • function RemoveClass(el, cl)
    Use for: Removing a class from an element

  • function GetElementsBySelector(root, selector)
    Use for: This method is a generalization of the DOM method "getElementsByTagName"

Window/screen utilities
Note from the js: TODO: these should be renamed (e.g. GetWindowWidth to GetWindowInnerWidth and moved to geom.js) Must keep an eye of the name changes, but i guess it'll took a long time to be done :P
  • - function GetPageOffsetLeft(el)
    - function GetPageOffsetTop(el)
    - function GetPageOffsetRight(el)
    - function GetPageOffsetBottom(el)
    - function GetPageOffset(el)
    Use for: Getting page offset of an element

  • - function GetScrollTop(win)
    - function GetScrollLeft(win)
    Use for: Getting the y (Top) and x (Left) position scroll offset.

  • function IsScrollAtEnd(win, opt_isHoriz)
    Use for: Checking if window scrollbar has reached its maximum offset

  • function ScrollTo(win, el, position)
    Use for: Scrolling window to pos (position: 0 = top, 0.5 = middle, 1 = bottom)

  • function ScrollIntoView(win, el, alignment)
    Use for: Scrolling so that as far as possible the entire element is in view.

  • - function GetWindowWidth(win)
    - function GetWindowHeight(win)
    - function GetAvailScreenWidth(win)
    - function GetAvailScreenHeight(win)
    - function GetNiceWindowHeight(win)
    Use for: Getting the window dimensions

  • function GetCenteringLeft(win, width)
    Use for: Horizontally centering a new window of the given width in the available screen. Set the new window's distance from the left of the screen equal to this function's return value.

  • function GetCenteringTop(win, height)
    Use for: Vertically centering a new window of the given height in the available screen. Set the new window's distance from the top of the screen equal to this function's return value.

  • function Popup(url, opt_name, opt_width, opt_height, opt_center, opt_hide_scrollbars, opt_noresize, opt_blocked_msg)
    Use for: Opening a child popup window that has no browser toolbar/decorations.

  • function OpenWindow(win, url, opt_name, opt_features, opt_blocked_msg)
    Use for: Opening a new window. Returns the new window handle.

String utilities
  • function HtmlEscape(str)
    Use for: Converting text to HTML format. For efficiency, we just convert '&', '<', '>' characters.

  • function HtmlUnescape(str)
    Use for: Converting html entities to plain text. It covers the most common named entities and numeric entities. It does not cover all named entities -- it covers &{lt,gt,amp,quot,nbsp}; but does not handle some of the more obscure ones like &{ndash,eacute};.

  • function HtmlWhitespaceEscape(str)
    Use for: Replacing multiple spaces with to retain whitespace formatting in addition to escaping '&', '<', '>'.

  • function QuoteEscape(str)
    Use for: Escape double quote '"' characters in addition to '&', '<', '>' so that a string can be included in an HTML tag attribute value within double quotes.

  • function ToJSString(s)
    Use for: Converting a string to a javascript string literal. This function has the property that the return value is also already html escaped, so the output can be embedded in an html handler attribute.

  • -function CollapseWhitespace(str)
    - function StripNewlines(str)
    - function CanonicalizeNewlines(str)
    - function HtmlifyNewlines(str)
    - function NormalizeSpaces(str)
    Use for: Converting multiple ws chars to a single space, and strips leading and trailing ws

  • function UrlEncode(str)
    Use for: URL encodes the string.

  • function Trim(str)

  • function EndsWith(str, suffix)

  • function IsEmpty(str)
    Use for: Checking if a string is empty

  • function IsLetterOrDigit(ch)
    Use for: Checking if a character is a letter

  • function IsSpace(ch)
    Use for: Checking if a character is a space character

  • function NormalizeText(str)
    Use for: Converting any instances of "\r" or "\r\n" style EOLs into "\n" (Line Feed), and also trim the extra newlines and whitespaces at the end.

  • function HtmlEscapeInsertWbrs(str, n, chars_to_break_after, chars_to_break_before)
    Use for: Inserting (word break tag) after every n non-space chars and/or after or before certain special chars. The input string should be plain text that has not yet been HTML-escaped.

  • function CompareStringsIgnoreCase(s1, s2)
    Use for: Comparing case-insensitive string

TextArea utilities
  • function GetCursorPos(win, textfield)
    Use for: Getting the cursor pos in a text area. Returns -1 if the cursor pos cannot be determined or if the cursor out of the textfield.

  • function SetCursorPos(win, textfield, pos)
    Use for: Setting the cursor pos in a text area.

Array utilities
  • function FindInArray(array, x)
    Use for: Finding an item in an array, returns the key, or -1 if not found

  • function InsertArray(array, x)
    Use for: Inserting an item into an array, if it's not already in the array

  • function DeleteArrayElement(array, x)
    Use for: Deleting an element from an array

  • function CopyArray(array)
    Use for: Copying a flat array

  • function CloneObject(x)
    Use for: Cloning an object (recursively)

  • function CloneEvent(ev)
    Use for: Cloning an event; cannot use CloneObject(event) because it suffers from infinite recursion. Thus, only a subset of the event properties are cloned.

  • function GetEventTarget(/*Event*/ ev)
    Use for: Getting an event. Note: Event is not a type in IE; IE uses Object for events

  • function CancelEvent(/*Event*/ ev)
    Use for: Canceling an event

Email address parsing
  • function ParseAddress(addr)
    Use for: Parsing an email address of the form "name" address into [name, address]

  • - function GetAddress(address)
    - function GetAddressUsername(address)
    - function GetPersonal(address)
    - function GetPersonalElseUsername(address)
    Use for: Getting a spesific field of email

  • function EmailsToArray(str)
    Use for: Converting a string containing list of email addresses into an array of strings

  • function CleanEmailAddress(str)
    Use for: Cleaning up email address which include remove extra spaces, and surround name with quotes if it contains special characters to check if we need " quotes. Note: do not use /g in the regular expression, otherwise the regular expression cannot be reusable.
__________________________________________________________________

2. dom.common.js
  • function toggleDisplay(o)
    Use for: Will toggle the display property of the style object for any DOM element or object that supports style as a property. Warning: This'll wreak havoc if applied to elements.

  • - function hideElement(o)
    - function showElement(o)
    Use for: Hiding an element from view. Showing an element that was hidden from view.

  • function getElement(id)
    Use for: Returning an element by its ID or shows an alert if it can't be found.

  • function setInnerHTML(id, html)
    Use for: Setting the innerHTML of an element or shows an alert if can't be set.

  • function setCssStyle(id, name, value)
    Use for: Setting the style of an element by its id or shows an alert if can't be set.

  • function getStyle(el, style)
    Use for: Getting the computed style of any object. WARNING: Produces unexpected results in Safari.

  • function getStyleAttribute(node)
    Use for: Returning the style attribute of the specified node.

  • function showProps(o)
    Use for: Displaying all the properties for a given element (this method is using document.write !!)

  • - function setIFrameEvent(iframe, eventName, func)
    - function setIFrameBody(iframe, strStyle, innerHtml)
    - function getIFrameDocument(iframe)
    - function getIFrame(strId)
    Use for: IFraming.

  • - function createElementandAppend(nodeName, strId, appendTo)
    - function createElementandInsertBefore(nodeName, strId, appendTo, sibling)
    Use for: Creating an element and appending/inserting before.

  • function getXY(el)
    Use for: Returning the position of any element as an object.
__________________________________________________________________

3. cookies.common.js
  • function setCookie(name, value, domain, expires, path, secure)
    Use for: Setting a Cookie with the given name and value.

  • function getCookie(name)
    Use for: Getting the value of the specified cookie.
__________________________________________________________________

So that's about it folks ! I surelly gonna work my way on maximizing this, and because of that i think i can remove the prototype and other thing :P

And in final words, hereby i challenged all of you the 'Bleet' to use it to your extent !!
.

Del.icio.us Tagometer Widget

Thursday, December 21, 2006

deusx posted a new and fun widget for inviting readers to bookmark your page on del.icio.us ^^

This is a great addition to del.icio.us !

Visitors can click on the count to see more detail on who bookmarked your page, when they did it, and how they tagged it. The list of tags shows visitors why your page is interesting, and clicking on a tag takes them to similar sites on del.icio.us.

Currently they offer us 2 nice badges, a TALL Badge and a One Line Badge :)

This widget claims to includes an up-to-date count of others who've already bookmarked the page, as well as a fresh list of the top tags applied.

Here's mine ^^



Cool eh ? ;) Have it yourself then !

Update: I don't know what happened when i inserted the scripts, the displays was kinda messed up, it gives a blank space almost like 20 lines *weird* so i took it off at the moment

Kudos to del.icio.us ^^
.

bLaugh JS For Your Delight

Thursday, November 16, 2006



If you don't know bLaugh, then maybe you'll interested on finding out :) bLaugh created a single-panel comicstrip involving events or things or people on the blogosphere :) which surely will brighten up your mornin ^^ they named themselves as The (un)Official Comic of the Blogosphere! :)

About 2 days ago, i found out that bLaugh provided an API for the geeks to play with ;) so as maybe i have the 'geek' spirit myself :P so i decided to make use of it :) and share it with everybody here :)

The script to use was simple, it was javascript, so you just hafta add these line to whereever you wanted this bLaugh comic strip to dock on your web-pages/blog :)

<script type="text/javascript" src="http://efendi.ning.com/bLaugh/displayComic.php"></script>


Now, the default is, the bLaugh Comic Strips will display in 3 column one, and there'll be 10 comics stripe.

But you can customize the script by adding some parameter ;)
There are (currently) 4 parameters you can add, which is : count, col, size

count is how much comic strip you wanted to display, the default is 10

col represents how much columns you wanted it to be displayed, like if you wanna put this on the sidebar maybe you wanted it to be just 1 column ;) the default is 3

size here is to adjust the image scaling size, the value would be in percentage as like if you wanted to have it half size then you entered 0.5 and so on ;) and 0.5 is default

So, with those parameters you can customized the script to looked like this:
<script type="text/javascript" src="http://efendi.ning.com/bLaugh/displayComic.php?count=5&col=2&size=0.3"></script>




Or you can have it to display the newest one like this :

<script type="text/javascript" src="http://efendi.ning.com/bLaugh/displayComic.php?count=1&size=1"></script>




No, why dont you try it yerself ;) if you have some trouble dont hesitate to contact me

Or maybe some suggestions, for further development ? ;P

Do U Reflect ?

Wednesday, November 08, 2006



In this era of Web2.0 trend, we saw almost everywhere that some logo or images have reflections under it ;)

With some knowledge of photoshop, you can easily achieve this :) but now i'm gonna introduce to you a little and handy reflections javascript that can be used on your websites !

YES ! Now you only need to include the scripts and you have a reflections like these :



Isn't this kewl ! It's also very very easy to implement ! Just add class="reflect" to your image like this :
<img src="yourimage.jpg" class="reflect" />


Above all, this script only 5KB! Lightweight !! And it's unobtrusive too, which mean if the user turns the javascript off, then it just your usual image ;) nothing changed :) it just dont show the reflections, besides nowadays almost every user dont have a problem with javascript rite ;)

Just take a look at Cow's Blog for more demonstration ;)

Lastly, if you reflect then tell me ;) i'll go have a look :)

Rainbow Javascript

Monday, October 16, 2006

Mr. Jim Bumgardner from KrazyDad which i have introduced to you as a great puzzle maker, now he has (again) impressed me with his new Rainbow Javascript function. Which will produce a sequence of colors, a rainbow colors, that can be implemented on your webpages ;) Mr. Jim have given us a detail explanation about how colors work, you can learn more in-depth tutorial over at Jim's. As for now, let have a look right into the javascript ;)

Now with his marvelous script you can make a nice rainbow like this (which you can add to the designs of your page) ;)


Isn't this beautiful ;) more of it you can make it a pastel colors :) like this:


Or maybe you can make some nice horizontal bars using the '_' characters, but in RAINBOW COLORS !!


Interesting eh ?! ;) Okay, then let's try to have a look what function you'll be needin' to make yer rainbow ^^

First of all you need to include these functions, which are the main core of color producing for the yer page ;)

function byte2Hex(n) {
var nybHexString = "0123456789ABCDEF";
return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n &amp;amp;amp;amp;amp; 0x0F,1);
}
function RGB2Color(r,g,b) {
return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
}


As for the function to produce the gradient rainbow, here it is
//f = frequency   p = phase
function makeColorGradient(f1, f2, f3, p1, p2, p3, center, width, len, chr) {
if (center == undefined) center = 128;
if (width == undefined) width = 127;
if (len == undefined) len = 50;
if (chr == undefined) chr = "█";

for (var i = 0; i < len; ++i) {
var r = Math.sin(f1*i + p1) * width + center;
var g = Math.sin(f2*i + p2) * width + center;
var b = Math.sin(f3*i + p3) * width + center;
document.write( '<font >' + chr + '</font>' );
}
}

Now, the main thing is, how to use those functions ??! Well, simply put these codes to your pages, and the script will do the rest ;) Note that you may try to play with the frequency, and phase ;)
<script>makeColorGradient(.3,.3,.3,0,2,4);</script>
<script>makeColorGradient(.3,.3,.3,0,2,4, 230,25);</script>
<script>makeColorGradient(.3,.3,.3,0,2,4, 128,127,67, "_");</script>


function colorText(str,p) {
if (p == undefined) p = 0;
center = 128;
width = 127;
f = Math.PI*2/str.length;
for (var i = 0; i < str.length; ++i) {
r = Math.sin(f*i+2+p) * width + center;
g = Math.sin(f*i+0+p) * width + center;
b = Math.sin(f*i+4+p) * width + center;
document.write( '<font color="' + RGB2Color(r,g,b) + '">' + str.substr(i,1) + '</font>');
}
}




Funky Javascript!!

Wednesday, October 11, 2006

OMG ! This is the most inventive and coolest tricks i've ever found ^^ (got it from digg ^^), if you are browsing a site that contains some images then try to copy and paste this code below and the paste it to the URL / Address bar ! Then ENTER !

javascript:{var R=0, x1=.1, y1=.05, x2=.25, y2=.24, x3=1.6, y3=.24, x4=300, y4=200, x5=300, y5=200, I=document.getElementsByTagName("img"), L=I.length; setInterval(function (){for(var i=0; i-L; i++){var S=I[ i ].style; S.position='absolute'; S.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+"px"; S.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+"px"}R++},5); void(0)}

and there's even some modification to the script, which ask you to enter the speed ;)

javascript:var speed=prompt(%22what speed for flying images? higher is slower, fastest and smoothest is 5%22); R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.getElementsByTagName(%22img%22); DIL=DI.length; function A(){for(i=0; i-DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+%22px%22; DIS.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+%22px%22}R++}setInterval('A()',speed); void(0);

or maybe not the images, but the text ? ;)

javascript:R=0; x1=.1; y1=.05; x2=.25; y2=.24; x3=1.6; y3=.24; x4=300; y4=200; x5=300; y5=200; DI=document.getElementsByTagName("font"); DIL=DI.length; function A(){for(i=0; i-DIL; i++){DIS=DI[ i ].style; DIS.position='absolute'; DIS.left=(Math.sin(R*x1+i*x2+x3)*x4+x5)+"px"; DIS.top=(Math.cos(R*y1+i*y2+y3)*y4+y5)+"px"}R++}setInterval('A()',75); void(0);


Hm.. maybe i'll play with these a bit further ;) if i have some spare time of course (don't expect much :P)

Try it on zlythern here ;) You'll be amazed! :)



[source: via]


CodeHighlighter 0.4

Monday, September 11, 2006

Mr. Dan Webb, a London-based web developer specialising in web application development, has made a great Code Highlighter javascript, which is a LIGHTWEIGHT (boy, how i loved those word ^^), unobstrusive and fully configurable script for displaying code examples highlighted. Like when you do scripting with text editor such as Ultra Edit, Dreamweaver, FlashDevelop and many others that support highlighting parts or your code. Anyway the size is just around 4K ;)

So, maybe i'll give a summary of how to installed those marvelous script on your website ;)

Okay, here we go :

1. Download Dan's CodeHighlighter 0.4 first ;)
2. Read the tutorials ;) (what the... ?!) :P kiddin' well i'll try to explain it my best :)

Well, after you downloaded the javascripts, you hafta add those into your website by using the <script> ... </script> tag, and of course you hafta host/upload those files !

So it goes on like these (make sure the src field is pointing at your host, well you know laa) :

<script type="text/javascript" src="code_highlighter.js"></script>
<script type="text/javascript" src="javascript.js"></script>
<script type="text/javascript" src="css.js"></script>
<script type="text/javascript" src="html.js"></script>
<script type="text/javascript" src="ruby.js"></script>

The most important file is the code_highlighter.js the others will serve as a style sets (as Mr. Dan refer to) it has some explanation when you download the files ;)

---------------------------------------------------------------------------

Anyway, for example we wanted to highlights the javascript codes, then the rules for what words that should be higlighted will be on the javascript.js file.

If you open up the javascript.js file you will see some piece of unreadable code (for some human :P), anyway for any of you who are familiar with RegExp you would find it beautiful ;) (i don't though :P)

Anyway if you wanted to add some words which you wanted to highlight, then simply add the word in these line:

keywords : {
exp : /\b(arguments|break|case|continue|default|delete|do|else|false|for|function|if|in|instanceof|new|null|return|switch|this|true|typeof|var|void|while|with)\b/
},

global : {
exp : /\b(toString|valueOf|window|element|prototype|constructor|document|escape|unescape|parseInt|parseFloat|setTimeout|clearTimeout|setInterval|clearInterval|NaN|isNaN|Infinity)\b/
}

just add it before the )\b/ on the end of the exp : line, of well, hope you understand it lah :P

---------------------------------------------------------------------------

And now for the main show, in your part of code that you wanted to add the highlights, simply add an appropriate class to each <code> element that contains code, as for the class, they were 'javascript', 'ruby', 'css' or 'html'.

so it goes like these :
<code class="javascript">
//paste your code here
//paste your code here
//paste your code here
//paste your code here
//paste your code here
</code>

and your code will be highlighted beautifully ;)

WAIT !! It doesn't produce the beautiful color ! How do i change the colors ? Nice questions ! Since there's no guide or explanation when you download the javascript :P So here we go again, a 'howto' on changing the colors

Okay first, take a look at the javascript.js file, you will then see some list such as comment, brackets, string, keywords, exp, and global. Now those are the words that you will hafta define the CSS styles !

so you write the CSS styles for those words like these :
code.javascript span.comment { color: YOUR COLOR HERE; }
code.javascript span.brackets { color: YOUR COLOR HERE; }
code.javascript span.string { color: YOUR COLOR HERE; }
code.javascript span.keywords { color: YOUR COLOR HERE; }
code.javascript span.exp { color: YOUR COLOR HERE; }
code.javascript span.global { color: YOUR COLOR HERE; }

as for the other class code, you simply change the word javascript to css or html, and find the words on css.js or html.js file, and then repeat the steps above ;)

that's it ! it should be working nice and beautiful ;) well, hope you understand it :P if you still haven't get it to work you can always drop me an email ;)

have fun ^^

Lightbox JS v2.02

Thursday, June 01, 2006

Today i've try this cool javascript by Lokesh Dhakar called Lightbox JS v2.0


Quoting from the author :
Lightbox JS is a simple, unobtrusive script used to overlay images on the current page. It's a snap to setup and works on all modern browsers.

So, time for a little demo about this lightbox thing, when the javascript is ready then you can click on the picture and wait for a sec as the picture is loading ;)

[Update 2006-06-30]: Moved the demo page to googlepages ^^ you can click here to see the demo ;)

About Me

My Photo
Efendi
An ordinary, a bit 'geeky' guy, who enjoy blogging about all unique things across the internet world. Blogs about the blogosphere, scripts, fun, games, interesting stuffs, etc. Currently staying and working in Singapore.
View my complete profile