MOCKSTACKS
EN
Questions And Answers

More Tutorials









HTML Canvas

The HTML <canvas> element is used to draw graphics on a web page.Canvas has several methods for drawing paths, boxes, circles, text, and adding images.


Canvas example


<canvas id="myCanvas" width="200" height="100" style="border: 1px solid lightgrey"> </canvas >

Adding a JavaScript


<script> 
    var c = document.getElementById("drawLine");
    var ctx = c.getContext("2d");
    ctx.moveTo(0, 0);
    ctx.lineTo(200, 100);
    ctx.stroke();
</script >

A circle


<script>
    var c = document.getElementById("circle");
    var ctx = c.getContext("2d");
    ctx.beginPath();
    ctx.arc(95, 50, 40, 0, 2 * Math.PI);
    ctx.stroke();
</script >

A Text


<script>
    var c = document.getElementById("text");
    var ctx = c.getContext("2d");
    ctx.font = "33px Arial";
    ctx.fillText("Canvas Text", 10, 50);
</script >

A Stroke Text


<script >
    var c = document.getElementById("strokeText");
    var ctx = c.getContext("2d");
    ctx.font = "33px Arial";
    ctx.strokeText("Canvas Text", 10, 50);    
</script >

A Linear Gradient


<script > 
    var c = document.getElementById("linearGradient");
    var ctx = c.getContext("2d");
    var grd = ctx.createLinearGradient(0, 0, 200, 0);
    grd.addColorStop(0, "blue");
    grd.addColorStop(1, "white");
    ctx.fillStyle = grd;
    ctx.fillRect(10, 10, 150, 80);
</script >

A Circular Gradient


<script> 
    var c = document.getElementById("RadialGradient");
    var ctx = c.getContext("2d");
    var grd = ctx.createRadialGradient(75, 50, 5, 90, 60, 100);
    grd.addColorStop(0, "blueviolet");
    grd.addColorStop(1, "white");
    ctx.fillStyle = grd;
    ctx.fillRect(10, 10, 150, 80);                            
</script >


Conclusion

In this page (written and validated by ) you learned about HTML Canvas . What's Next? If you are interested in completing HTML tutorial, your next topic will be learning about: HTML SVG.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.