HTML Canvas | HTML | IndianTechnoEra - IndianTechnoEra
Latest update Android YouTube

HTML Canvas | HTML | IndianTechnoEra

HTML Canvas | HTML | IndianTechnoEra
Admin
The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript.

The <canvas> element is only a container for graphics. You must use JavaScript to actually draw the graphics.


Canvas has several methods for drawing paths, boxes, circles, text, and adding images.

Always specify an id attribute (to be referred to in a script), and a width and height attribute to define the size of the canvas. To add a border, use the style attribute.

Example :

    <canvas id="myCanvas" width="200" height="100" style="border:2px solid #000000;"> </canvas>
    <script>
        var cnv = document.getElementById("myCanvas");
        var cnctx = cnv.getContext("2d");
                //required operations
    </script>

 

Circle with Canva:

Definition and Usage for Circle

  • The arc() method creates an arc/curve (used to create circles, or parts of circles).
  • Tip: To create a circle with arc(): Set start angle to 0 and end angle to 2*Math.PI.
  • Tip: Use the stroke() or the fill() method to actually draw the arc on the canvas.

An arc

  • Centerarc(100,75,50,0*Math.PI,1.5*Math.PI)
  • Start anglearc(100,75,50,0,1.5*Math.PI)
  • End anglearc(100,75,50,0*Math.PI,1.5*Math.PI)

JavaScript syntax

context.arc(x,y,r,sAngle,eAngle,counterclockwise);

Parameters Values
x The x-coordinate of the center of the circle
y The y-coordinate of the center of the circle
r The radius of the circle
sAngle The starting angle, in radians (0 is at the 3 o'clock position of the arc's circle)
eAngle The ending angle, in radians

counterclockwise  Optional. Specifies whether the drawing should be counterclockwise or clockwise. False is default, and indicates clockwise, while true indicates counter-clockwise.

 

HTML Canvas Reference

  • The HTML <canvas> tag is used to draw graphics, on the fly, via scripting (usually JavaScript).
  • To learn more about <canvas>, please read our HTML Canvas tutorial.

setTransform() Resets the current transform to the identity matrix. Then runs transform() 7.
 
1. Colors, Styles, and Shadows
Parameters Values
fillStyle Sets or returns the color, gradient, or pattern used to fill the drawing
strokeStyle Sets or returns the color, gradient, or pattern used for strokes
shadowColor Sets or returns the color to use for shadows
shadowBlur Sets or returns the blur level for shadows
shadowOffsetX Sets or returns the horizontal distance of the shadow from the shape
shadowOffsetY Sets or returns the vertical distance of the shadow from the shape
Method Description
createLinearGradient() Creates a linear gradient (to use on canvas content)
createPattern() Repeats a specified element in the specified direction
createRadialGradient() Creates a radial/circular gradient (to use on canvas content)
addColorStop() Specifies the colors and stop positions in a gradient object
   
2. Line Styles
Parameters Description
lineCap Sets or returns the style of the end caps for a line
lineJoin Sets or returns the type of corner created, when two lines meet
lineWidth Sets or returns the current line width
miterLimit Sets or returns the maximum miter length
   
3. Rectangles
Method Description
rect() Creates a rectangle
fillRect() Draws a "filled" rectangle
strokeRect() Draws a rectangle (no fill)
clearRect() Clears the specified pixels within a given rectangle
   
4. Paths
Method Description
fill() Fills the current drawing (path)
stroke() Actually draws the path you have defined
beginPath() Begins a path, or resets the current path
moveTo() Moves the path to the specified point in the canvas, without creating a line
closePath() Creates a path from the current point back to the starting point
lineTo() Adds a new point and creates a line to that point from the last specified point in the canvas
clip() Clips a region of any shape and size from the original canvas
quadraticCurveTo() Creates a quadratic Bézier curve
bezierCurveTo() Creates a cubic Bézier curve
arc() Creates an arc/curve (used to create circles, or parts of circles)
arcTo() Creates an arc/curve between two tangents
isPointInPath() Returns true if the specified point is in the current path, otherwise false
   
5. Transformations
Method Description
scale() Scales the current drawing bigger or smaller
rotate() Rotates the current drawing
translate() Remaps the (0,0) position on the canvas
transform() Replaces the current transformation matrix for the drawing
   
6. Text
Property Description
font Sets or returns the current font properties for text content
textAlign Sets or returns the current alignment for text content
textBaseline Sets or returns the current text baseline used when drawing text
   
Method Description
fillText() Draws "filled" text on the canvas
strokeText() Draws text on the canvas (no fill)
measureText() Returns an object that contains the width of the specified text
   
7. Image Drawing
Method Description
drawImage() Draws an image, canvas, or video onto the canvas
   
8. Pixel Manipulation
   
Property Description
width Returns the width of an ImageData object
height Returns the height of an ImageData object
data Returns an object that contains image data of a specified ImageData object
   
Method Description
createImageData() Creates a new, blank ImageData object
getImageData() Returns an ImageData object that copies the pixel data for the specified rectangle on a canvas
putImageData() Puts the image data (from a specified ImageData object) back onto the canvas
   
9. Compositing
Property Description
globalAlpha Sets or returns the current alpha or transparency value of the drawing
globalCompositeOperation Sets or returns how a new image is drawn onto an existing image
   
10. Other
Method Description save() Saves the state of the current context restore() Returns previously saved path state and attributes createEvent() getContext() toDataURL


Working

    <p>1. Only canvas</p>
    <canvas id="myCanvas1" width="200" height="100" style="border:2px solid #000000;"> </canvas>
    <br>
    <p>2. With JavaScript for a digonal (SingleDignonalLine)</p>
    <canvas id="myCanvas2" width="200" height="100" style="border:2px solid #000000;"> </canvas>
    <script>
        var cnv = document.getElementById("myCanvas2");
        var cnctx = cnv.getContext("2d");
        // left up to right down
        cnctx.moveTo(0, 0);
        cnctx.lineTo(200, 100);
        cnctx.stroke();
    </script>
    <br>
 
    <p>3. With JavaScript for a digonal (DoubleDignonalLine)</p>
    <canvas id="myCanvas3" width="200" height="100" style="border:2px solid #000000;"> </canvas>
    <script>
        var cnv = document.getElementById("myCanvas3");
        var cnctx = cnv.getContext("2d");
        // left up to right down
        cnctx.moveTo(0, 0);
        cnctx.lineTo(200, 100);
 
        // right up to rightdown
        cnctx.moveTo(200, 0);
        cnctx.lineTo(0, 100);
        cnctx.stroke();
    </script>
    <br>
 
    <p>4. With JavaScript for a circle</p>
    <canvas id="myCanvas4" width="200" height="100" style="border: 2px solid #000000;"></canvas>
 
    <script>
        var c = document.getElementById("myCanvas4");
        var ctx = c.getContext("2d");
        ctx.beginPath();
        ctx.arc(100, 50, 45, 0, 2 * Math.PI);
        ctx.stroke();
    </script>
    <br>
    <p>5. With JavaScript for a triangle</p>
    <canvas id="myCanvas5" width="200" height="100" style="border:2px solid #000000;"> </canvas>
    <script>
        var cnv = document.getElementById("myCanvas5");
        var cnctx = cnv.getContext("2d");
        // Left side
        cnctx.moveTo(100, 10);
        cnctx.lineTo(40, 80);
 
        // Right side
        cnctx.moveTo(100, 10);
        cnctx.lineTo(160, 80);
 
        // Base
        cnctx.moveTo(40, 80);
        cnctx.lineTo(160, 80);
        cnctx.stroke();
    </script>
    <br>
 
    <p>6. Canvas for Strike Text draw in a rectange</p>
    <canvas id="myCanvas6" width="200" height="100" style="border: 2px solid #000000;"></canvas>
    <script>
        var cnv = document.getElementById("myCanvas6");
        var cnvctx = cnv.getContext("2d");
        cnvctx.font = "25px Arial";
        cnvctx.stroke.Text("IndianTechnoEra", 5, 60);
    </script>
    <br>
 
    <p>7. Canvas for Rectangular Gradient fill</p>
    <canvas id="myCanvas7" width="200" height="100" style="border: 2px solid #000000;"></canvas>
    <script>
        var c = document.getElementById("myCanvas7");
        var ctx = c.getContext("2d");
 
        // Create gradient
        var grd = ctx.createLinearGradient(0, 0, 200, 100); //Color lining rote
        grd.addColorStop(0, "orange");
        grd.addColorStop(0.4, "white");
        grd.addColorStop(1, "green");
 
        // Fill with gradient (up-x;up-y)
        ctx.fillStyle = grd;
        ctx.fillRect(10, 10, 180, 80); //Fill gradient margine
    </script>
    <br>
 
    <p>8. Canvas for Rectangular Gradient fill</p>
    <canvas id="myCanvas8" width="200" height="100" style="border: 2px solid #000000;"></canvas>
    <script>
        var cnv = document.getElementById("myCanvas8");
        var cnvctx = cnv.getContext("2d");
 
        // Create gradient
        var grd = cnvctx.createRadialGradient(100, 50, 5, 80, 60, 100);
        grd.addColorStop(0, "red");
        grd.addColorStop(1, "white");
 
        // Fill with gradient
        cnvctx.fillStyle = grd;
        cnvctx.fillRect(10, 10, 180, 80);
    </script>
    <br>
 
    <p>9. Canvas for filling the image in box</p>
    <img src="http://shorturl.at/uIJR6" alt="IndianTechnoEra" id="myCnvImg" width="1148" height="250">
 
    <p>Above image fill in  the box </p>
 
    <canvas id="myCanvas9" width="1200" height="300" style="border: 2px solid #000000;"></canvas>
    <p><button onclick="myCanvas9()">Fill the box</button></p>
    <script>
        function myCanvas9(){
            var cnv = document.getElementById("myCanvas9");
            var cnvctx = cnv.getContext("2d");
            var img = document.getElementById("myCnvImg");
            cnvctx.drawImage(img,26,25)
        }
    </script>
    </div>


working done

Live Example Output:

1. Only canvas


2. With JavaScript for a diagonal (SingleDignonalLine)


3. With JavaScript for a diagonal (DoubleDignonalLine)


4. With JavaScript for a circle


5. With JavaScript for a triangle


6. Canvas for Strike Text draw in a rectangle


7. Canvas for Rectangular Gradient fill


8. Canvas for Rectangular Gradient fill


9. Canvas for filling the image in box

IndianTechnoEra

Above image fill in the box

Post a Comment

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.