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);
}
}
}
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.
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.
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
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
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
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
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.
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.
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.
With a lineWidth of 6px, we draw a curve with 30px x-axis, 40px y-axis as first control point and 90px x-axis, 50px y-axis as second control point; Also 180px x-axis, 190px y-axis as end points of the curve. Not to forget the starting point, moveTo, which has 10px x-axis and 150px y-axis.
With the help of the points in Bezier curve example above, we trace its control points. moveTo takes the same value. first lineTo takes first control points value. second lineTo takes second control point value. third lineTo takes end point values.