Canvas Element Pattern: HTML5

Today lets learn how to create pattern from another canvas element.

canvas-element-pattern-html5

In this video tutorial, we take 2 canvas elements. Draw a small circle on one canvas and use it as pattern and draw a bigger circle on the main canvas.

HTML file
index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< !DOCTYPE HTML>
<html>
<head>
<title>Canvas: HTML5</title>
<meta charset="utf-8"/>
<link href="myStyle.css" rel="stylesheet" />
<script src="myScript.js"></script>
</head>
<body>
 <canvas id="myCanvas" width="400" height="400">
  Upgrade Browser
 </canvas>
 
 <canvas id="pattern" width="50" height="50">
 Please upgrade your browser!
 </canvas>
 
</body>
</html>

Here we have 2 canvas elements with unique ids. First/Main canvas is 400px X 400px width and height, second canvas is 50px X 50px width and height.
Related: Canvas Basics: HTML5

CSS file
myStyle.css

1
2
3
canvas {
border: 2px dotted black;
}

We assign 2px black dotted border to both the canvas present on our HTML page.

JavaScript file: Pattern Code
myScript.js

1
2
3
4
5
6
7
8
9
var pCanvas          = document.getElementById("pattern");
var pContext         = pCanvas.getContext("2d");
 
pContext.lineWidth   = 2;
pContext.arc(25, 25, 10, 0, 2 * Math.PI);
pContext.strokeStyle = "red";
pContext.fillStyle   = "blue";
pContext.stroke();
pContext.fill();

Here we get the pattern id and extract the 2d context APIs. Using pContext object, we draw a small circle of radius 10px with red border and filled with blue color.
Related: Draw Arcs/Circle with Canvas: HTML5

JavaScript file: Main Canvas Code
myScript.js

1
2
3
4
5
context.fillStyle    = context.createPattern(pCanvas, "repeat");
context.strokeStyle  = "pink";
context.arc(200, 200, 100, 0, 2 * Math.PI);
context.stroke();
context.fill();

We assign previously created pattern to fillStyle of our main canvas context. With this pattern as fillStyle, and a pink border, we draw a circle which has a radius of 100px.
Related: Canvas Image Patterns: HTML5

JavaScript file: Full Code
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
window.onload = canvas;
 
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
 
if( myCanvas && myCanvas.getContext("2d") ) 
{
var context            = myCanvas.getContext("2d");
 
var pCanvas            = document.getElementById("pattern");
var pContext           = pCanvas.getContext("2d");
 
pContext.lineWidth     = 2;
pContext.arc(25, 25, 10, 0, 2 * Math.PI);
pContext.strokeStyle   = "red";
pContext.fillStyle     = "blue";
pContext.stroke();
pContext.fill();
 
context.shadowColor    = "black";
//context.shadowOffsetX= - 10;
//context.shadowOffsetY= - 10;
context.shadowBlur     = 10;
 
context.fillStyle      = context.createPattern(pCanvas, "repeat");
context.strokeStyle    = "pink";
context.arc(200, 200, 100, 0, 2 * Math.PI);
context.stroke();
context.fill();
}
}

We’re making the main canvas circle stand out by applying shadow effects.
Related: Shadow Effect on Canvas: HTML5

Canvas Element Pattern: HTML5


[youtube https://www.youtube.com/watch?v=2LuBWIzs_EY]

YouTube Link: https://www.youtube.com/watch?v=2LuBWIzs_EY [Watch the Video In Full Screen.]



Patterns can be used creatively in HTML5 games development and to draw images which look like having a 2d or 3d dimensions.

Canvas Image Patterns: HTML5

Today lets learn how to create patterns on canvas using image.

index.html and myStyle.css files are kept same as illustrated in previous video tutorial: Canvas State: HTML5

Related Topics:
Canvas Basics: HTML5
Draw Rectangle: HTML5 Canvas

Javascript file
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
window.onload = canvas;
 
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
 
if( myCanvas && myCanvas.getContext("2d") ) 
{
var context         = myCanvas.getContext("2d");
 
var myImg           = new Image();
myImg.src           = "technotip.png";
 
myImg.onload        = function() {
  context.fillStyle = context.createPattern(myImg, "no-repeat");
  context.fillRect(0, 0, context.canvas.width, context.canvas.height);
}
 
}
}

Here we create a new image object myImg and set it’s source to a image technotip.png Once the object loads the source image we call an anonymous function. Here we set fillStyle property with the pattern we create out of the image. createPattern takes 2 parameter, first on being the pattern itself and the second one being the image repeat value, it takes repeat, no-repeat, repeat-x or repeat-y as it’s value.

Canvas Image Patterns: HTML5


[youtube https://www.youtube.com/watch?v=AhxDlK5dOHY]

YouTube Link: https://www.youtube.com/watch?v=AhxDlK5dOHY [Watch the Video In Full Screen.]



Note:
We can create pattern out of another canvas element or an image or even a video file.

Shadow Effect on Canvas: HTML5

Today lets see how we can draw shadow on canvas elements like text, circle etc.

index.html and myStyle.css files are kept same as illustrated in previous video tutorial: Canvas State: HTML5

Since we’re showing shadow effect on Text, you need to first know how to draw text on canvas:
Draw Text on Canvas: HTML5
Draw Arcs/Circle with Canvas: HTML5

shadow-canvas-html5

Javascript file: Shadow settings
myScript.js

1
2
3
4
context.shadowColor     = "black";
context.shadowOffsetX   = - 10;
context.shadowOffsetY   = - 10;
context.shadowBlur      = 10;

using shadowColor we can set the color of the shadow. You can use any css color string as its value. By default, the color is transparent black.

shadowOffsetX and shadowOffsetY are used to set horizontal and vertical offsets of the shadow respectively. Both default to 0.

shadowBlur is the blurness of the shadow. It defaults to 0 too. Higher the value of shadowBlur, more blur it looks!

Javascript file: Full Code
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
window.onload = canvas;
 
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
 
if( myCanvas && myCanvas.getContext("2d") ) 
{
var context         = myCanvas.getContext("2d");
 
context.shadowColor     = "black";
context.shadowOffsetX   = - 10;
context.shadowOffsetY   = - 10;
context.shadowBlur      = 10;
 
var myString            = "Microsoft";
context.font            = "40px Verdana strong";
context.textAlign       = "left";
context.strokeStyle     = "cyan";
context.fillStyle       = "blue";
context.textBaseline    = "bottom";
context.fillText(myString, 10, 100, 100);
context.strokeText(myString, 10, 100, 100);
 
 
context.arc(150, 150, 50, 0, 2 * Math.PI);
context.stroke();
 
}
}

Here we’re applying the shadow settings to text as well as a circle.

Draw Shadow on Canvas: HTML5


[youtube https://www.youtube.com/watch?v=Uj1oXQI4xaU]

YouTube Link: https://www.youtube.com/watch?v=Uj1oXQI4xaU [Watch the Video In Full Screen.]



We can apply shadow settings to text, circle, lines, arcs, images etc which is present on the canvas. Remember, you can’t directly apply CSS rules on canvas elements.

Draw Text on Canvas: HTML5

Today lets learn how to draw text / string on a canvas, using HTML5.

It’s similar to drawing any other paths. You can fillStyle, strokeStyle on them. But the text on canvas can not be directly affected by a cascading style sheet.

index.html and myStyle.css files are kept same as illustrated in previous video tutorial: Canvas State: HTML5

Drawing Text: Syntax
context.fillText(myString, x-axis, y-axis, [maximumWidth-of-text]);
context.strokeText(myString, x-axis, y-axis, [maximumWidth-of-text]);

myString is the text we’re drawing on the canvas. x and y axis is the position of the text to be drawn on the canvas. maximumWidth-of-text is the maximum limit the text can stretch on the canvas i.e., it can’t go beyond that maxWidth value. Also note that, maximumWidth-of-text is an optional parameter.

Javascript file: Drawing Text – font settings
myScript.js

1
2
3
4
5
6
7
8
var myString = "Microsoft";
context.font = "30px Verdana strong";
context.textAlign = "left";
context.strokeStyle = "cyan";
context.fillStyle = "blue";
context.textBaseline = "bottom";
context.fillText(myString, 10, 100, 100);
context.strokeText(myString, 10, 100, 100);

We store the string Microsoft in a variable called myString. We set the font values to it, with a text align of left and text base line of bottom. Then by applying strokeStyle of cyan and fillStyle of blue, we fillText and strokeText the string present in myString.

Javascript file: Drawing Text – Measure Text Method
myScript.js

1
2
3
4
5
6
7
var txtW = context.measureText(myString);
context.lineWidth = 4;
context.beginPath();
context.strokeStyle = "red";
context.moveTo(10, 110);
context.lineTo(txtW.width, 110);
context.stroke();

measureText() method returns the dimension metrics of the string using the current font settings. Here we’re extracting its width property and drawing a line below the string Microsoft.

Javascript file: Drawing Text – Full Code
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
window.onload = canvas;
 
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
 
if( myCanvas && myCanvas.getContext("2d") ) 
{
var context         = myCanvas.getContext("2d");
 
var myString = "Microsoft";
context.font = "30px Verdana strong";
context.textAlign = "left";
context.strokeStyle = "cyan";
context.fillStyle = "blue";
context.textBaseline = "bottom";
context.fillText(myString, 10, 100, 100);
context.strokeText(myString, 10, 100, 100);
 
var txtW = context.measureText(myString);
context.lineWidth = 4;
context.beginPath();
context.strokeStyle = "red";
context.moveTo(10, 110);
context.lineTo(txtW.width, 110);
context.stroke();
 
}
}

context.font takes any value that you would normally put into a font css rules: font-family, font-size, font-weight, variant etc.

context.textBaseline takes values like: start(its default value), end, left, right, center.

context.textAlign takes values like: top, bottom, middle, hanging, ideographic, alphabetic(its default value)

Drawing Text on Canvas: HTML5


[youtube https://www.youtube.com/watch?v=4cFZ-CV88zM]

YouTube Link: https://www.youtube.com/watch?v=4cFZ-CV88zM [Watch the Video In Full Screen.]



Note:
1. textAlign renders text based on the positioning of the text on canvas. If you assign a value of center, the text will be centered based on the x and y axis of the text and not based on the canvas width(x-axis) and height(y-axis).
2. By default the font on canvas is 10px in size and has a font-family of sans-serif.

Quadratic Curve In Canvas: HTML5

Today lets learn to draw quadratic curves on Canvas, using HTML5.

index.html and myStyle.css files are kept same as illustrated in previous video tutorial: Canvas State: HTML5

quadratic-curve-canvas-html5

Syntax
quadraticCurveTo(cx, cy, endX, endY);

(cx, cy) are control point.
(endX, endY) are end point of the curve.

You may be wondering about the start point!
We specify it with the help of moveTo() method.

Quadratic curve start at the current pen position using the given control point cx, cy and end at the end points defined by endX, endY.

i.e., Quadratic curves use a starting point, one control point and an end point.

Javascript file: Quadratic Curve
myScript.js

1
2
3
4
5
6
7
8
context.lineWidth= 6;
context.beginPath();
context.moveTo(20, 100);
context.quadraticCurveTo(80, 190, 150, 120);
context.strokeStyle= "red";
context.fillStyle= "yellow";
context.stroke()
context.fill();

With a line width of 6px, we draw a line from 20px x-axis, 100px y-axis and using the control point 80px x-axis, 190px y-axis, we join to the end points 150px x-axis, 120px y-axis.

Javascript file: Control Point
myScript.js

1
2
3
4
5
6
7
context.lineWidth= 1;
context.beginPath();
context.moveTo(20, 100);
context.lineTo(80, 190);
context.lineTo(150, 120);
context.strokeStyle = "pink";
context.stroke();

With the help of the points in Quadratic curve example above, we trace its control points.
moveTo takes the same value.
first lineTo takes control points value.
second lineTo takes end point values of quadratic curves.

Quadratic Curve In Canvas: HTML5


[youtube https://www.youtube.com/watch?v=OMDfbpLBeGc]

YouTube Link: https://www.youtube.com/watch?v=OMDfbpLBeGc [Watch the Video In Full Screen.]



Related:
Draw Arcs/Circle with Canvas: HTML5
Bezier Curve In Canvas: HTML5

Javascript file: Full Code
myScript.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
window.onload = canvas;
 
function canvas()
{
var myCanvas = document.getElementById("myCanvas");
 
if( myCanvas && myCanvas.getContext("2d") ) 
{
var context         = myCanvas.getContext("2d");
 
context.lineWidth= 6;
context.beginPath();
context.moveTo(20, 100);
context.quadraticCurveTo(80, 190, 150, 120);
context.strokeStyle= "red";
context.fillStyle= "yellow";
context.stroke()
context.fill();
 
context.lineWidth= 1;
context.beginPath();
context.moveTo(20, 100);
context.lineTo(80, 190);
context.lineTo(150, 120);
context.strokeStyle = "pink";
context.stroke();
}
}

Color Scheme
Curve border – strokeStyle : red
Curve Fill – fillStyle : yellow
Control Point Path : pink