
Boost Your Business with a Custom App
Boost your business with a custom mobile app from 4BIS Innovations. Increase sales, engage customers, and future-proof your success.
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...
Boost your business with a custom mobile app from 4BIS Innovations. Increase sales, engage customers, and future-proof your success.
Do you want to start a successful webshop? Find out all about strategy, technology, marketing and security. 4BIS Innovations helps you with custom solutions!
Discover high-quality, custom web development solutions at 4BIS Innovations in Maastricht. We specialize in building fast, secure, and user-friendly websites and web applications...
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!