html5 canvas webgl,WebGL - Html5 Canvas Overview

WebGL - Html5 Canvas Overview

Advertisements

To create graphical applications on the web, HTML-5 provides a rich set of features such as 2D Canvas, WebGL, SVG, 3D CSS transforms, and SMIL. To write WebGL applications, we use the existing canvas element of HTML-5. This chapter provides an overview of the HTML-5 2D canvas element.

HTML5 Canvas

HTML-5 provides an easy and powerful option to draw graphics using JavaScript. It can be used to draw graphs, make photo compositions, or do simple (and not so simple) animations.

Here is a simple element having only two specific attributes width and height plus all the core HTML-5 attributes like id, name, and class.

Syntax

The syntax of HTML canvas tag is given below. You have to mention the name of the canvas inside double quotations (“ ”).

Canvas Attributes

The canvas tag has three attributes namely, id, width, and height.

Id − Id represents the identifier of the canvas element in the Document Object Model (DOM).

Width − Width represents the width of the canvas.

Height − Height represents the height of the canvas.

These attributes determine the size of the canvas. If a programmer is not specifying them under the canvas tag, then browsers such as Firefox, Chrome, and Web Kit, by default, provide a canvas element of size 300 × 150.

Example - Create a Canvas

The following code shows how to create a canvas. We have used CSS to give a colored border to the canvas.

#mycanvas{border:1px solid red;}

On executing, the above code will produce the following output −

The Rendering Context

The is initially blank. To display something on the canvas element, we have to use a scripting language. This scripting language should access the rendering context and draw on it.

The canvas element has a DOM method called getContext(), which is used to obtain the rendering context and its drawing functions. This method takes one parameter, the type of context 2d.

The following code is to be written to get the required context. You can write this script inside the body tag as shown below.

var canvas = document.getElementById('mycanvas');

var context = canvas.getContext('2d');

context.font = '20pt Calibri';

context.fillStyle = 'green';

context.fillText('Welcome to Tutorialspoint', 70, 70);

On executing, the above code will produce the following output −

For more example on HTML-5 2D Canvas, check out the following link HTML-5 Canvas.

WebGL Context

HTML5 Canvas is also used to write WebGL applications. To create a WebGL rendering context on the canvas element, you should pass the string experimental-webgl, instead of 2d to the canvas.getContext() method. Some browsers support only 'webgl'.

var canvas = document.getElementById('my_canvas');

var gl = canvas.getContext('experimental-webgl');

gl.clearColor(0.9,0.9,0.8,1);

gl.clear(gl.COLOR_BUFFER_BIT);

On executing, the above code will produce the following output −

Advertisements

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值