Home > Tags > function
Page 1

What Can You Do With Paper.js?

There are many JavaScript frameworks that leverage HTML5.

Paper.js is one of these frameworks that uses Document Object Model (DOM) to structure objects in an easy-to-understand manner.  It offers creative and neat ways of doing lots of stuff on a Web browser that supports the <canvas> tag. It also offers a new and interesting approach to drawing vector graphics.

The basic setup is shown below:

<script src="js/paper.js" type="text/javascript"></script> <script src="js/main.js" type="text/paperscript"></script> <canvas id="draw" width="100%" height="100%" resize="true"></canvas>

As you can see, the paper.js is included after which you add your code file under “type=”text/….” After passing the canvas element id that needs to be drawn and ensuring that your code file includes all the classes and functionality to be used with paper.js, then the rest is a matter of being creative while you leave the rest to paper.js.

Working with Predefined Shapes

Paper.js allows you to use predefined shapes of varying dimensions and create path items and segments. For example, the code below produces circle-shaped paths from the “Circle” constructor:

var myCircle = new Path.Circle(new Point(300, 70), 50); myCircle.fillColor = 'black';

This piece of code creates a black circle with a radius of 50pts and at the x position of 300 and a y position of y.

To create a rectangle, you pass the “Rectangle” constructor the same way as a circle as shown below:

var rectangle = new Rectangle(new Point(50, 50), new Point(150, 100)); var path = new Path.Rectangle(rectangle); path.fillColor = '#e9e9ff'; path.selected = true;

Creating Interaction

Paper.js also supports drag n drop functionality as well as keyboard strokes. An empty path is created on execution where segments can also be added as shown below:

// Create a new path once, when the script is executed: var myPath = new Path(); myPath.strokeColor = 'black'; // This function is called whenever the user // clicks the mouse in the view: function onMouseDown(event) { // If the path is empty, we need to add two segments // to the path. The first one will stay put, // and the second...
more →
David Gitonga says: Hi Rokcy. Here is the demo link. http://paperjs.org/examples/ The link contains different animations created using Paperjs.

PHP Arrays: Defining, Looping and Sorting Simple Arrays

Unlike scalar variables, which assign only a single value to a variable, an array variable can hold multiple values.

Arrays are useful for holding values from database queries or web form entries, where each field (also called a “key”) holds a specific value.

Let’s take a look at how we define some of the arrays we use in PHP.Numbered Arrays.

If the programmer does not specify a key for each value in the array, PHP automatically numbers the keys, starting from zero.

This code defines an array $arrMonths[], with each month of the year as an element in the array.

<?php $arrMonths[] = ‘January’; $arrMonths[] = ‘February’; $arrMonths[] = ‘March’; $arrMonths[] = ‘April’; ?>

The PHP interpreter automatically defines each key in the array with a number, starting from zero.

<?php $arrMonths[0] = ‘January’; $arrMonths[1] = ‘February’; $arrMonths[2] = ‘March’; $arrMonths[3] = ‘April’; ?>

Array Function

Another method of defining an array is to use the array function:

<?php $arrMonths= array(‘January’, ‘February’, ‘March’, ‘April’); ?>

This function creates a numbered array in the same way as the enumerated elements in the example above.

Associative Arrays

In some instances, the programmer does not want each value associated to a number, but to a more descriptive key.  Each of these descriptive keys needs to be associated with a value, hence the term “associative” array.

Just as with a numerical array, code authors can create an associative array one element at a time:

<?php $arrBooks[‘Comic Books’] = ‘Superman’; $arrBooks[‘Science Fiction’] = ‘Dune’; $arrBooks[‘Fantasy’] = ‘The Hobbit’; $arrBooks[‘Horror’] = ‘Carrie’; ?>

The array function is also useful for creating associative arrays. The “=>” symbol ties the key phrase to the value.

<?php $arrBooks = array( ‘Comic’ => ‘Superman’, ‘Science Fiction’ => ‘Dune’, ‘Fantasy’ => ‘The Hobbit’, ‘Horror’ => ‘Carrie’); ?>

Is It An Array?

If you’re not certain if a variable has the structure of an array, the is_array function can test the variable to see if it is an array.

<?php $baseballTeams = array(‘Cardinals’,...
more →
says: Sorry about that everyone. Something went wrong and yes, the article did get cut off. One of those things where you check, double...

Common C# Build-Time Errors Part II: Inheritance and Interfaces

In our last lesson, we saw many of the most basic build-time errors in C#.

In this session, we will look at some of the errors related to:

classes subclasses inheritance

Once we address some of the more common errors we will take a look at how you can fix them.

#1 Hidden Method Name Creates Overload

This conflict arises when a base class and its subclass have a function of the same name

public class MyBaseClass { public void Function() { // function code goes here } { public class MySubClass : MyBaseClass { public void Function() { // function code goes here } } public class YourClass { public void YourFunction() { SubClass ysb = new MySubClass; ysb.Function(); } }

The function YourFunction() cannot access the MyBaseClass.Function() from MySubClass because MySubClass.Function() hides it.

If the MySubClass.Function() is supposed to be hidden, the line that creates MySubClass.Function() should contain the keyword “new” to differentiate it from the MyBaseClass.Function():

public class MySubClass : MyBaseClass { new public void Function() { // function code goes here } }

If the MySubClass.Function() is supposed to be inherited polymorphically from MyBaseClass, the line that creates MySubClass.Function() should contain the keyword “override” and the MyBaseClass.Function() line should contain the keyword “virtual” to allow for the override:

public class MyBaseClass { public virtual void Function() { // function code goes here } } public class MySubClass : MyBaseClass { public override void Function() { // function code goes here } }

#2 Cannot Inherit from Sealed Class, Method or Variable

Programmers typically seal classes to protect them from modification from inherited classes, so they do not define their classes that are expected to pass methods and variables through inheritance as sealed.

public class Xray { protected virtual void Function() { Console.WriteLine("Xray.Function"); } protected virtual void Function2() { Console.WriteLine("Xray.Function2"); } } // end of class Xray public class Yankee : Xray { sealed protected override void Function()...
more →