How to draw transparent shape using html5 canvas

How to draw HTML5Canvas TransparentShape

In this html5 Canvas tutorial we will discuss how to draw transparent shape we set the opacity of a shape using the HTML5 canvas API.For controlling opacity we use globalAlpha property.
code for setting globalAlpha  property is.


context.globalAlpha =[value]


The globalAlpha poperty accept any real number between 0 to 1.
value of 1 represent shape fully opaque.For fully transparent shape value is set 0.

Steps for drawing htm5canvas transparentshape

To draw transparent shape in html5 canvas,we wil take the following steps.

Step 1 for drawing html5 canvas transparent shape: Define a 2D Canvas



var canvas =document.getElementById("myCanvas");
var context =canvas.getContext("2d");

Step 2. Draw a rectangle using html5 Canvas



// draw a rectangle
context.beginPath();
context.rect(240,30,130,130);
context.fillStyle ="blue"
context.fill();

Step 3: Set the globalAlpha property and draw a circle using html5 Canvas



//draw circle
context.globalAlpha =0.5; // set global alpha
context.beginPath();
context.arc(359,150,80,2* Math.PI, false);
context.fillStyle =#ff0000";
context.fill();

Step 4: Embed canvas tag in body of web document



canvas width="600" height="400" style ="border: 1px solid #0000ff">

——————————————————————-

HTML5 Canvas Transparent Shape Complete code example

———————————————————————


  
    <html>
        <head>
            <title>The type of lineJoin.</title>
        </head>
        <body>
            <canvas id="myCanvas" width="600" height="200"
            style="border: 1px solid black">

            </canvas>
            <script>
 
             var canvas  = document.getElementById("myCanvas");
             var context = canvas.getContext("2d");
            // draw a rectangle 
            context.beginPath();
            context.rect(240,30,130,130);
            context.fillStyle ="blue"
            context.fill();
            //draw circle
            context.globalAlpha =0.5; // set global alpha
            context.beginPath();
            context.arc(359,150,80,2* Math.PI, false);
            context.fillStyle ="#ff0000";
            context.fill();

                


            

            </script>
        </body>
  </html>
  

=============================================

HTML5 Canvas drawing Transparent Shape tutorial

=============================================

tranparentshape see on code pen
HTML5 Canvas Drawing Transparent shape

——————————————–

live demo drawing HTML5 Canvas Transparent Shap on codepen

——————————————-
[codepen_embed height=500 theme_id=1 slug_hash=’myMqpP’ user=’aslamwaqar’ default_tab=’html’

animations=’run’]

[/codepen_embed]

Next Lecture How to Create Shadows

All the lectures related to the html5 canvas are covered in this course offered by theiteducation.com. This is the plattform which provides free and step by step audios and videos tutorials for the beginners as well as all those who are working in the iteducation field.

Author: Habibullah Qamar

Its me Habib Ullah Qamar working as a Lecturer (Computer Sciences) in Pakistan. I have an MS(M.Phil) degree in computer sciences with specialization in software engineering from Virtual University of Pakistan Lahore. I have an experience of more than 15 years in the filed of Computer Science as a teacher. Blog Writing is my passion. I have many blogs, This one is special made with the aim of providing 100% Free online coaching and training to the students of under-graduate and postgraduate classes. Most of the students enrolled in computer sciences, information technology, software engineering and related disciplines find it difficult to understand core concepts of programming and office automation. They find difficult in understanding and solving their assignments.

Leave a Reply

Your email address will not be published. Required fields are marked *