Tag Archives: Canvas element

Interesting behavior of lineWidth on Opera

I found an interesting behavior of lineWidth on Opera when setting its value very large:

context.lineWidth = 222;

This is a rectangle whose lineWidth is 222!

On Google Chrome, Firefox or IE – it doesn’t act like Opera!

If you want to test it quickly, open my tool: Canvas Designer

Selectrectangle” from tool-box; set lineWidth=222 (from tool-box) and draw rectangle! (You must do this in Opera!)


Relative Points vs. Absolute Points

Relative Points:

In simple words, points that are relative to each other are called relative points.

 Absolute Points:

Absolute points contain exact values.

I started using the concept of “Relative Points” while within my first tool “Curvature” (I continued using it in “Canvas Designer“!) – where I allowed end-users to get auto-generated code in relative/absolute formats. Relative points helped them move/drag shapes easily by just changing two objects: x and y. (x-y coordinating system!)

My all tools contain this feature and personally I use it in my real experiments and projects, too.

Relative points help you in complex animations like flying bird, scene movement (rolling camera), rocket fire or any other kind of animating experiment!

See an example: http://muaz-khan.github.com/Everything/Canvas/Experiments/Flying-Bird/

The process of “Relative Points”:

I get topmost x-y coordinates and then I extract all other points from those coordinates accordingly.

In case of additional complex shapes like Bezier/quadratic curves, I get topmost four points (one starting point, two control points and one ending point) and I set these values to four variables and use them accordingly. You can see it in action in my tool “Curvature“.


An absolute point specifies a unique point on the screen as measured from the top-left corner of the screen.

The difference between the two types of coordinates is their origin.

Absolute coordinates are always measured from the origin (top-left corner of the screen or a specific object), and relative coordinates are measured from the current position (or a specific position), where this might be.

Following statement draws a line using absolute coordinates:

context.moveTo(20, 20);

context.lineTo(500, 500);

Following statement draws a line using relative coordinates:

var x = 20, y = 20;context.moveTo(x, y);
context.lineTo( x + 480, y + 480);

Relative coordinates are using frequently in drawing closed shapes, be it’s easier to define an endpoint by its distance from the previous one.


Just designed a simplest project: UseMe

UseMe allows you to compare features side-by-side or search any feature as quickly and easily as possible!

Features are relevant to HTML5CSS3 and JavaScript specifications (also WebGL/SVG etc.)

Data has taken from +caniuse.com (awesome website!)

If I misused any feature; please let me know in the case.

I wanna make its design awesome using Canvas 2d API but my exams are about to starts (college exams) that’s why I’m early releasing a simplest version of UseMe.

Some individual features:
❶ http://muaz-khan.github.com/UseMe/#Canvas
❷ http://muaz-khan.github.com/UseMe/#WebGL
❸ http://muaz-khan.github.com/UseMe/#Video

Some features comparatively:
❶ http://muaz-khan.github.com/UseMe/#Canvas/vs/WebGL
❷ http://muaz-khan.github.com/UseMe/#Audio/vs/Video
❸ http://muaz-khan.github.com/UseMe/#requestAnimationFrame/vs/Canvas


Related: https://plus.google.com/100325991024054712503/posts/3A826CiDJzp

Releasing Curvature second time!

Curvature is a designer allows you draw curves. It generates Canvas 2d APIs relevant code in different forms/styles.


What is Curvature?

Today release contains following new features:
➛ Curves Alignment (align any point, to any coordinate of the screen)
➛ You can drag whole curve (as it is!)
➛ You can drag whole drawing (all curves – without any disturbance!)
➛ You can copy whole drawing exactly!
➛ You can get shortened code too!

Dragging/Moving shapes smoothly using Canvas 2d APIs

Did you ever write code to drag or move shapes using Canvas 2d APIs? If you did, then didn’t you ever face a situation (an unexpected behaviour) when you drag or move a shape (e.g. circle, rectangle, image, Bezier cubic/quadratic curve, etc.)?

This post helps you write code to drag or move shapes as smoothly and accurately as possible using HTML5 Canvas 2d APIs.

Read full blog post


Curvature: HTML5 Canvas Curves Generator/Tool

Today I am happy to announce the release of Curvature: a tool lets you design curves using Canvas 2d APIs!

Curvature lets you draw unlimited Bezier curves and also generates exact code for you in two formats:

1) Absolute Coordinates

context.moveTo(200, 300);

context.bezierCurveTo(200, 300, 400, 500, 600, 700);

These are absolutely positioned points/coordinates.

Now let’s make an assumption: you had drawn one hundred (100) curves. Could you easily move all curves (as they are) on any portion of the screen?

The answer is: No, never. Not so easy!

Why it is not easy to move absolutely positioned coordinates on the screen?

You are drawing Bezier curves; not a simple line, arc or rectangle! For Bezier curves you have to understand its two control points and one ending point.

Canvas 2d API for Bezier curve looks like this:

CanvasRenderingContext2D.bezierCurveTo = function(cp1x, cp1y, cp2x, cp2y, x, y);

So what is the solution? The solution is relative positioned coordinates. If you had drawn hundreds of thousands of curves; no matter; Curvaturewill do all job for you!

2) Relative Coordinates

/* First curve where we set default values for x and y ! */

/* All subsequent curves will be extracted from these two objects! */

context.moveTo( x = 359, y = 319 );

context.bezierCurveTo( x + 25, y – 164, x + 191, y – 169, x + 285, y – 57 );

// Second Curve

context.moveTo( x , y  );

context.bezierCurveTo( x + 25, y – 164, x + 157, y + 51, x + 285, y – 57 );

// Third Curve

context.moveTo( x , y  );

context.bezierCurveTo( x + 25, y – 164, x + 234, y – 256, x + 285, y – 57 );

As you saw in the above code; when we call context.moveTo Canvas 2d API first time; we initialize default position for two variables or objects: x and y.

All subsequent values/points are relative to these two objects.

Changing them will affect all others accordingly.

How relative positioned coordinates can helps me?

Just copy auto generated code and you have to change only two points or coordinates or values:

x = e.pageX    // or whatever

y = e.pageY      // or whatever

Curvature auto-generates code for you! – Just copy and you are done!

Just draw more than one Bezier curves copy auto-generated code and you are done! Happy!!!

Try the tool:


Just try! – Don’t hesitate to feedback! – You are most welcome!

You feedback may help me bring something better!