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...
Software Modernisation, Knowledge Base, Automate, Web development
Nowadays, many terms are thrown around interchangeably. You sometimes hear about Artificial Intelligence, but also about Machine Learning and perhaps even Deep Learning. This can make it quite unclear what exactly the term is that you are looking for...
Read moreSoftware Modernisation, Web development, Business software, Knowledge Base, Automate
Integrations are extremely important to ensure smooth collaboration between all your tools. Unfortunately, it still often happens that companies make some activities run less smoothly by not properly linking their tools. This is of course a shame because...
Read moreError messages when looking up a website are common. Although they are often easy to solve, they still often go wrong. This could be due to a small error, but this can immediately prevent a connection error. Resolving these errors requires some steps....
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!