Pages

Showing posts with label Creating. Show all posts
Showing posts with label Creating. Show all posts

Friday, August 5, 2011

Creating a plugin for jQuery


Learn how to create a plug-in for jQuery from scratch - the basics, options, compatibility and examples.

The Basics
A plugin is written as a method or function.

Creating a jQuery function

Syntax
The function must return this.each (..) to maintain the ability to "chaining" - so that the function can be used with one or several objects jQuery.

jQuery.fn.myFunction = function () {
this.each return (function () {
/ / Element-specific code here
});
};


Example

jQuery.fn.makeTextRed = function () {
this.each return (function () {
$ (This). Css ('color', 'red');
});
};
/ / Example usage

$ ('# My-div'). MakeTextRed () / / make text in "my-div" red

$ ('P'). MakeTextRed () / / make all red Paragraphs

Creating a jQuery method

Syntax
jQuery.myMethod = function () {
/ / Code here
};
Example
jQuery.sayHelloWorld = function () {

alert ('Hello World');

};
/ / Example usage

$. SayHelloWorld () / / alerts "Hello World"

Options


Let your plugin more flexible and user friendly as possible, using options. The method $. Extend () takes two or more objects as arguments and combines their contents within the first object.

Example
A function that sets the text color (red by default).

jQuery.fn.makeTextColored = function (settings) {
var config = {
'Color': 'red'
};
if (settings) {$. extend (config, settings);}
this.each return (function () {

$ (This). Css ('color', config.color);

});

};

Now we can choose to use this function passing the parameter settings or not.

$ ('# My-div'). MakeTextColored () / / make red text (default)
$ ('# My-div'). MakeTextColored ('blue') / / make text blue

Compatibility

Because the variable $ can be used by other plugins, use a technique "disguised" to make your plugin compatible future.

(Function ($) {
$. Fn.myFunction = function () {
this.each return (function () {
/ / Element-specific code here
});
};
}) (JQuery);

We went to the jQuery function, and we can now use jQuery for any outfit you want. So, instead of the $, you could use any valid JavaScript variable name.

A checklist of the jQuery plugin
  • This is a list of important points to remember when developing a jQuery plugin ( the jQuery.com ).

  • Name your file jquery. [Insert the plugin name]. Js, eg. jquery.debug.js.

  • All new methods are attached to the object jQuery.fn object, and all functions to the jQuery object.

  • For internal methods, this is a reference to the current jQuery object.

  • Any methods or functions that you attach must have a semicolon (;) at the end - otherwise the code will break when compressed.

  • Your method must return the jQuery object, unless it was explicitly noted otherwise.

  • Use this.each to iterate over the current set of matching elements.

  • Always attach the plugin to jQuery instead of $, so that users can use a custom outfit via noConflict ().

Templates jQuery plugin

These are good code templates to begin developing a jQuery plugin.
Template Function

(Function ($) {
$. Fn.myPlugin = function (settings) {
var config = {
'Foo': 'bar'
};
if (settings) {$. extend (config, settings);}
this.each return (function () {

/ / Element-specific code here

});

};

}) (JQuery);

Template Method

(Function ($) {
$. MyPlugin = function (settings) {
var config = {
'Foo': 'bar'
};
if (settings) {$. extend (config, settings);}
/ / Code here
return this;

};

}) (JQuery);

Example: jQuery Slideshow Plugin

I decided to use very simple examples so far that you might be familiar. The following example is a little more complex and can help you find your inspiration.

He uses the setInterval () together with the effects fadeOut () and fadeIn () jQuery to generate the cycle of any images within a HTML element.

Configuration

HTML

<div id="slideshow">
<img src="img/sample-image-1.png" alt="" />
<img src="img/sample-image-2.png" alt="" />
<img src="img/sample-image-3.png" alt="" />
<img src="img/sample-image-4.png" alt="" />
</ Div>

CSS

# Slideshow img {
display: none;
position: absolute;
}

JavaScript

(Function ($) {
$. SimpleSlideShow = function (selector, settings) {
/ / Settings
var config = {
'Delay': 2000,
'FadeSpeed': 500
};
if (settings) {$. extend (config, settings);}
/ / Variables

var obj = $ (selector);

var img = obj.children ('img');

var count = img.length;

var i = 0;
/ / Show first image

img.eq (0). show ();
/ / Run slideshow

setInterval (function () {

img.eq (i). fadeOut (config.fadeSpeed);

i = (i +1 == count)? 0: i +1;

img.eq (i). fadeIn (config.fadeSpeed);

}, Config.delay);
return this;

};

}) (JQuery);


Use

To activate the slideshow div # slideshow, we simply call it using the following JavaScript code:

<script type="text/javascript">
$. SimpleSlideShow ('# slideshow');
</ Script>

How do we allow the configuration to change the behavior of the slideshow, we could leave 5 seconds between the images and set the duration of the "fade" for 200 ms using:

<script type="text/javascript">
$. SimpleSlideShow ('# slideshow', {'delay': 5000, 'fadeSpeed': 200});
</ Script>



Wednesday, August 3, 2011

Using Comapping for Creating a Classroom Agenda

One of the handiest tools that I use when I teach is Comapping- it has been a fantastic tool for developing the agenda for each and every class that I teach on campus. Comapping allows me to map out what I am going to cover with links and attachments that makes it very easy for my students to access and use. Once I have completed my agenda map for the classroom session, I simply Publish to Web from within Comapping and grab the embedding code that is generated. Once I have the embedding code I simply paste it into Moodle to generate the mind map. Here is an example below of a agenda map that I recently used in a class this week.


Photoshop Tutorial Creating the storm effects

See below the picture? Transform the image field in an afternoon cloudy stormy and dramatic by adding simple effects lightning!

FINAL REVIEW


open cloud picture




Start off by duplicating the default layer (CTRL+J) as Layer 1.

Reduce brightness of duplicate image by dragging center point of the Curves tool (CTRL+M).  

Duplicate Layer 1 as Layer 1 copy. Go to Filter > Radial Blur > Zoom : 80. Set the blend mode for Layer 1 copy as 'Multiply'.



You should get something like this:



Now, create a new layer (Shift+Ctrl+N) and name it as “storm”. Go to Filter > Render > Clouds.



Next up, select Filter > Render > Difference Clouds. Apply the same filter (Ctrl+F) several times (preferably 5 times or above).



Bring out the Levels Window (Ctrl+L) and apply settings until you see lumps of clouds appearing (Input Levels: 30/0.75/175). 

NOTE: Results/settings may vary as each clouds/difference clouds rendered may differ.

Once done, set storm’s blend mode to 'Color Dodge'.



Transform (CTRL+T) and scale the image. You can also adjust the perspective a little, based on the nature of your chosen image. Press Enter to apply the transformation.  

Next, we should remove areas which are too bright. Follow these sub-steps:
1) Go to Windows > Channel (to bring out the channels window).
2) Hold CTRL and click on Red channel (to make a selection).
3) Go to Select > Inverse (or press Shift+CTRL+I) to invert the selection.
4) Go to Select > Feather (or press CTRL+ALT+D) and set radius : 10 pixels.

Click on the masking tool to hide the brighter parts of the layer.





Select layer mask of 'storm' layer and further adjust the levels (CTRL+L) until you have something like this (Input Levels: 0/1.70/180):



NOTE: Again, results/settings may vary as each clouds/difference clouds rendered from previous steps may differ. I’ve decided to remove some bright areas of the storm by brushing over the layer mask with a soft black brush.

Now that we have got the storm going, lets add a few bolts of lightning.



First, create a new layer (Shift+CTRL+N) and name it as ‘lightning’. Using the Rectangular Marquee tool (M), select a square box (holding down the Shift key while dragging will create a perfect square). Bring out the Gradient tool (G) when you are done.
Create a gradient by dragging through the center of the selection.



The gradient should look like this, preferably.  

With the selection still active, go to Filter > Render > Difference Clouds (or just press CTRL+F as it is your last applied filter).

NOTE: Difference Clouds rendered differs as it is randomly generated. Press CTRL+I to invert it.



And adjust the level (CTRL+L). 




 Apply the lightning's blend mode to ‘Screen’.

 Next, use the eraser tool to remove any hard edges. Make sure the hardness is set to 0%.

Transform (CTRL+T) the bolt of lightning to which ever way you like.



I’ve tweaked mine a little bit more; adding glows, reducing brightness, and etc. This is my final image.


Monday, August 1, 2011

Creating a plugin for jQuery


Learn how to create a plug-in for jQuery from scratch - the basics, options, compatibility and examples.

The Basics
A plugin is written as a method or function.

Creating a jQuery function

Syntax
The function must return this.each (..) to maintain the ability to "chaining" - so that the function can be used with one or several objects jQuery.

jQuery.fn.myFunction = function () {
this.each return (function () {
/ / Element-specific code here
});
};


Example

jQuery.fn.makeTextRed = function () {
this.each return (function () {
$ (This). Css ('color', 'red');
});
};
/ / Example usage

$ ('# My-div'). MakeTextRed () / / make text in "my-div" red

$ ('P'). MakeTextRed () / / make all red Paragraphs

Creating a jQuery method

Syntax
jQuery.myMethod = function () {
/ / Code here
};
Example
jQuery.sayHelloWorld = function () {

alert ('Hello World');

};
/ / Example usage

$. SayHelloWorld () / / alerts "Hello World"

Options


Let your plugin more flexible and user friendly as possible, using options. The method $. Extend () takes two or more objects as arguments and combines their contents within the first object.

Example
A function that sets the text color (red by default).

jQuery.fn.makeTextColored = function (settings) {
var config = {
'Color': 'red'
};
if (settings) {$. extend (config, settings);}
this.each return (function () {

$ (This). Css ('color', config.color);

});

};

Now we can choose to use this function passing the parameter settings or not.

$ ('# My-div'). MakeTextColored () / / make red text (default)
$ ('# My-div'). MakeTextColored ('blue') / / make text blue

Compatibility

Because the variable $ can be used by other plugins, use a technique "disguised" to make your plugin compatible future.

(Function ($) {
$. Fn.myFunction = function () {
this.each return (function () {
/ / Element-specific code here
});
};
}) (JQuery);

We went to the jQuery function, and we can now use jQuery for any outfit you want. So, instead of the $, you could use any valid JavaScript variable name.

A checklist of the jQuery plugin
  • This is a list of important points to remember when developing a jQuery plugin ( the jQuery.com ).

  • Name your file jquery. [Insert the plugin name]. Js, eg. jquery.debug.js.

  • All new methods are attached to the object jQuery.fn object, and all functions to the jQuery object.

  • For internal methods, this is a reference to the current jQuery object.

  • Any methods or functions that you attach must have a semicolon (;) at the end - otherwise the code will break when compressed.

  • Your method must return the jQuery object, unless it was explicitly noted otherwise.

  • Use this.each to iterate over the current set of matching elements.

  • Always attach the plugin to jQuery instead of $, so that users can use a custom outfit via noConflict ().

Templates jQuery plugin

These are good code templates to begin developing a jQuery plugin.
Template Function

(Function ($) {
$. Fn.myPlugin = function (settings) {
var config = {
'Foo': 'bar'
};
if (settings) {$. extend (config, settings);}
this.each return (function () {

/ / Element-specific code here

});

};

}) (JQuery);

Template Method

(Function ($) {
$. MyPlugin = function (settings) {
var config = {
'Foo': 'bar'
};
if (settings) {$. extend (config, settings);}
/ / Code here
return this;

};

}) (JQuery);

Example: jQuery Slideshow Plugin

I decided to use very simple examples so far that you might be familiar. The following example is a little more complex and can help you find your inspiration.

He uses the setInterval () together with the effects fadeOut () and fadeIn () jQuery to generate the cycle of any images within a HTML element.

Configuration

HTML

<div id="slideshow">
<img src="img/sample-image-1.png" alt="" />
<img src="img/sample-image-2.png" alt="" />
<img src="img/sample-image-3.png" alt="" />
<img src="img/sample-image-4.png" alt="" />
</ Div>

CSS

# Slideshow img {
display: none;
position: absolute;
}

JavaScript

(Function ($) {
$. SimpleSlideShow = function (selector, settings) {
/ / Settings
var config = {
'Delay': 2000,
'FadeSpeed': 500
};
if (settings) {$. extend (config, settings);}
/ / Variables

var obj = $ (selector);

var img = obj.children ('img');

var count = img.length;

var i = 0;
/ / Show first image

img.eq (0). show ();
/ / Run slideshow

setInterval (function () {

img.eq (i). fadeOut (config.fadeSpeed);

i = (i +1 == count)? 0: i +1;

img.eq (i). fadeIn (config.fadeSpeed);

}, Config.delay);
return this;

};

}) (JQuery);


Use

To activate the slideshow div # slideshow, we simply call it using the following JavaScript code:

<script type="text/javascript">
$. SimpleSlideShow ('# slideshow');
</ Script>

How do we allow the configuration to change the behavior of the slideshow, we could leave 5 seconds between the images and set the duration of the "fade" for 200 ms using:

<script type="text/javascript">
$. SimpleSlideShow ('# slideshow', {'delay': 5000, 'fadeSpeed': 200});
</ Script>