Html5

HTML5 Web Storage

HTML5 Web Storage

Fadıl

HTML5 comes with two objects for storing data in the client machine.

localstorage – There is no time limit for the data stored
sessionstorage – Data is stored for one session.

Cookies were used for this purpose, but cookies cannot be used if the data amount is large and they are also slow and ineffective as these cookies are passed to server on every request.
In HTML5 data is not passed every time whenever there is a server request. The data is sent only when the particular information is requested by the server. This will help a lot in increasing server performance.

Drag and drop – overview

Drag and drop – overview

Fadıl

The HTML5 – HTML series latest version comes with a drag and drops API. This feature allows the user to drag and drop the data within the website or with other websites.

This drag and drop feature allows the user to click and hold an element using the button in the mouse drag that element and drop into another location. That location may be the same site or another site. When the element is dragged from a location a translucent image follows the path that the mouse moves during this dragging operation.

A complex canvas example

A complex canvas example

Fadıl

In the previous post we saw about canvas using path and also using rectangles in this example we will see how to combine them to form a complex and at the same time an interesting composition.

<html>
  <head>
   <script type="application/javascript">
 function drawBowtie(ctx, fillStyle) {

   ctx.fillStyle = "rgba(200,200,200,0.3)";
   ctx.fillRect(-30, -30, 60, 60);

   ctx.fillStyle = fillStyle;
   ctx.globalAlpha = 1.0;
   ctx.beginPath();
   ctx.moveTo(25, 25);
   ctx.lineTo(-25, -25);
   ctx.lineTo(25, -25);
   ctx.lineTo(-25, 25);
   ctx.closePath();
   ctx.fill();
 }

 function dot(ctx) {
   ctx.save();
   ctx.fillStyle = "black";
   ctx.fillRect(-2, -2, 4, 4);
   ctx.restore();
 }

 function draw() {
   var canvas = document.getElementById("canvas");
   var ctx = canvas.getContext("2d");

   // note that all other translates are relative to this
   // one
   ctx.translate(45, 45);

   ctx.save();
   //ctx.translate(0, 0); // unnecessary
   drawBowtie(ctx, "red");
   dot(ctx);
   ctx.restore();

   ctx.save();
   ctx.translate(85, 0);
   ctx.rotate(45 * Math.PI / 180);
   drawBowtie(ctx, "green");
   dot(ctx);
   ctx.restore();

   ctx.save();
   ctx.translate(0, 85);
   ctx.rotate(135 * Math.PI / 180);
   drawBowtie(ctx, "blue");
   dot(ctx);
   ctx.restore();

   ctx.save();
   ctx.translate(85, 85);
   ctx.rotate(90 * Math.PI / 180);
   drawBowtie(ctx, "yellow");
   dot(ctx);
   ctx.restore();
 }
    </script>
  </head>
  <body onload="draw()">
    <canvas id="canvas" width="300" height="300"></canvas>
  </body>
 </html>

In the above example, we can find two methods drawbowtie and dot both these methods are called 4 times each. And the calls like translate(), rotate() are used to set transformation matrix which in turn positions the dot and the bowtie .dot function is used to place the small black square at (0,0). That dot is moved around by the transformation matrix .drawBowtie will create a very simple bowtie path using the passed-in fill style.
The functions save() and restore() are used to set the original canvas state.This rotation always occur around the current origin.Thus a translate() rotate() translate() sequence will yield different results than a translate() translate() rotate() series of calls.

Applying styles and colors using canvas

Applying styles and colors using canvas

Fadıl

Canvas elements can be used to give colors also in the previous post we discussed various drawing functionalities in a canvas element. In this post, we shall discuss the coloring features in the canvas element.

Colors

There are two properties that canvas element provide us to apply colors to shapes

fillStyle = color
strokeStyle = color

strokeStyle parameter helps in defining the outline color of the shape.
lifestyle parameter helps in coloring inside the shape.
Any CSS color code can be used here.
//these all set the fillStyle to 'orange' ctx.fillStyle = "orange"; ctx.fillStyle = "#FFA500"; ctx.fillStyle = "rgb(255,165,0)"; ctx.fillStyle = "rgba(255,165,0,1)";

Canvas Element transformations

Canvas Element transformations

Fadıl

In the previous posts, we discussed using images and also applying styles and colors. In this post, we shall discuss using canvas transformations.
When we talk about transformations in Canvas element we have to deal with two main concepts they are

  • save()
  • restore()

The above-mentioned methods are used to save and retrieve the canvas states. The canvas states are stored onto a stack. Every time the save method is called, the current drawing state is pushed onto the stack. A drawing state consists of transformations, stroke style, fill style, linewidth shadow blur values. When the restoration method is called the last saved state is taken from the stack and is exhibited.