In the world of web development, adding graphical elements to HTML pages has evolved from static images. Two prominent techniques for creating dynamic graphics are Canvas and SVG (Scalable Vector Graphics). In this article, we delve into the capabilities of both methods and explore how they can be used to enhance the interactivity and visual appeal of web content.
The HTML canvas element serves as a dynamic drawing board that allows developers to dynamically create graphical elements using JavaScript. Below are examples illustrating the creation of simple graphics with Canvas:
Example 1: Drawing a Line
var c = document.getElementById("canvasSquare");
var ctx = c.getContext("2d");
ctx.moveTo(125, 0);
ctx.lineTo(150, 50);
ctx.lineTo(250, 60);
ctx.lineTo(175, 100);
ctx.lineTo(200, 150);
ctx.lineTo(125, 120);
ctx.lineTo(50, 150);
ctx.lineTo(75, 100);
ctx.lineTo(0, 60);
ctx.lineTo(100, 50);
ctx.lineTo(125, 0);
ctx.closePath();
ctx.stroke();
let el = document.getElementById('drawingCanvas');
var ctx = el.getContext('2d');
let isDrawing;
let inputEl = document.getElementById('canvasData');
el.onpointerdown = function (e) {
isDrawing = true;
ctx.moveTo(e.offsetX, e.offsetY);
};
el.onpointermove = function (e) {
if (isDrawing) {
ctx.lineTo(e.offsetX, e.offsetY);
ctx.stroke();
}
};
el.onpointerout = el.onpointerup = function () {
if (isDrawing) {
isDrawing = false;
let data = el.toDataURL();
inputEl.value = data;
}
};
let reset = document.getElementById('drawingCanvasReset');
reset.onclick = () => {
ctx.beginPath();
ctx.clearRect(0, 0, el.width, el.height);
inputEl.value = null;
};
Scalable Vector Graphics (SVG) is an XML-based markup language for displaying two-dimensional graphical elements. SVG seamlessly integrates with other W3C standards, and the elements can be easily animated. Below is an example of creating a simple SVG circle:
Let's compare Canvas and SVG based on key features:
Canvas:Both Canvas and SVG offer powerful front-end techniques suitable for various applications. Canvas excels in dynamic, resolution-dependent graphics, making it an excellent choice for games. On the other hand, the scalability and event support of SVG make it ideal for applications with large display areas. Whether you're creating interactive maps or immersive online games, understanding the strengths of Canvas and SVG enables you to choose the right tool for your web development needs. Stay tuned for more articles exploring advanced techniques and practical examples with Canvas and SVG!
Also interesting
Expand your knowledge with the 4BIS Blog...
Web development, SEO / SEA, Managed Hosting, B2B, Digital Marketing, Business software
Discover 5 common pitfalls in software development and learn how to avoid them for successful and effective projects.
Read moreWeb development, Software Modernisation, Business software, Managed Hosting, Saas
API connections are essential for seamless integration between software systems, but determining the cost of creating one can be complex. In this guide, we break down the main factors that impact API development costs—including data volume, security...
Read moreWeb development, Software Modernisation, Ecommerce / Webshop, Business software, PHP, MySQL, HTML & CSS
Discover 5 essential tips for finding the right software developer, from price-quality ratio to location and experience. Read our helpful guide!
Read more Do you want to know more or are you interested? Please feel free to contact us directly, by phone or via mail.
Or use one of our contact forms so that we can answer you as quickly and appropriately as possible can give. We would love to hear from you!