To see the result you can check
jsfiddle.net. That is the place i will publish the code samples. You can create canvas element with 'canvas' tag. Then you should assign an id or class
so as to work with it in Javascript. I just want to grab your attention here, you can select canvas element with jquery but you cannot do anything inside it. because it isn't elements. Therefore, javascript must be your
choice anyway. Speaking of giving id or class to element, it can be done like that.
Select All -> Codes will be copited to clipboard
<canvas id="canvas">
</canvas>
<canvas class="canvas">
</canvas>
Canvas is an HTML element so you have two options to design this HTML element. You can design either with inline style or external. Creating separate .css file and-and set width, height, background color in external .css
file is always best practice. We will style canvas element like below
External Styling
Select All -> Codes will be copited to clipboard
.canvas{
width:500px;
height:500px;
}
Additional note: in default canvas's width is 300px, canvas' height is 150px.
Functionality is important, to be honest. So we need javascript for that. Remember, don't use jquery, go for plain javascript. First of all we need to grab the dom object. Look at below;
Select All -> Codes will be copited to clipboard
var canvas = document.querySelector('#canvas');
/* Remember: We use "#" for id, "." for class.
This principle is same as selecting element in css. */
Right now i am using var keywords. Keep it in your mind for future uses, in ES6, there is const keyword and that is new terminology for defining variable. However ES6 is not widely standardized amongst browser.
With that selection, you have dom api, also you need to have canvas api to use canvas functionality. In this point,
Select All -> Codes will be copited to clipboard
var canvas = document.querySelector("#canvas");
// It is for using dom API.
Select All -> Codes will be copited to clipboard
var ctx = canvas.getContenxt('2d');
// It is for using 2d canvas API.
It is time to draw something with canvas. There are a lot of shapes are predefined in canvas API such as rectangle, circle etc. Lets try
fillRect(). FillRect has four parameters.
- x coordinate
- y coordinate
- width
- height
context.fillRect(0, 0, canvas.width, canvas.height);
Additional note: You can give any number instead of canvas.width or canvas.height also you can set in css part as we mentioned above. However if your canvas.
We can set the colors of canvas element with fillStyle(). Fillstyle method
give you some flexibility while setting colour. You can set colour with string, with rgb values,with rgba values, with hex value, with hsv values or with hsl values.
Select All -> Codes will be copited to clipboard
ctx.fillStyle = "blue";
ctx.fillStyle = "rbg(255, 192, 203)";
ctx.fillStyle = "rgba(255, 192, 203, 1)";
// Fourth parameter is just for setting opacity.
ctx.fillStyle = "#00FFFF";
ctx.fillStyle = 'hsl('+h+', 100%, 50%)';
Warning: You have to be careful about orders. If you call fillStyle after fillRect. fillStyle wont work.
Additional content will be added soon.