Difference between SVG and HTML 5 Canvas - GeeksforGeeks (2024)

Last Updated : 30 Jan, 2024

Summarize

Comments

Improve

Suggest changes

Like Article

Like

Save

Report

SVG (Scalable Vector Graphics) is an XML-based vector image format, suitable for creating graphics and interactive elements. HTML5 Canvas is more like a blank canvas where you can draw whatever you want using pixels. SVG is great for logos and icons, while Canvas is good for dynamic animations and games.

SVG:

Scalable Vector Graphics (SVG) is an XML-based image format used to define two-dimensional vector-based graphics for the web. Unlike raster images (Ex .jpg, .gif, .png, etc.), a vector image can be scaled up or down to any extent without losing the image quality. An SVG image is drawn out using a series of statements that follow the XML schema — that means SVG images can be created and edited with any text editor, such as Notepad. There are several other advantages of using SVG over other image formats like JPEG, GIF, PNG, etc.

Example: In this example, we will see the code for drawing a circle using SVG elements.

html

<!DOCTYPE html>

<html>

<head>

<title>SVG</title>

<style>

#svgelem {

position: relative;

left: 50%;

-webkit-transform: translateX(-20%);

-ms-transform: translateX(-20%);

transform: translateX(-20%);

}

</style>

<title>HTML5 SVG</title>

</head>

<body>

<h2 align="center">

SVG Circle(Geeks For Geeks)

</h2>

<svg id="svgelem" height="200">

<circle id="greencircle" cx="60"

cy="60" r="50" fill="green" />

</svg>

</body>

</html>

Output:

Difference between SVG and HTML 5 Canvas - GeeksforGeeks (2)

Canvas:

The HTML element is used to draw graphics on the fly, via scripting (usually JavaScript). The element is only a container for graphics. You must use a script to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.

Example: In this example, we will see the code for drawing a square using Canvas elements.

HTML

<!DOCTYPE html>

<html>

<head>

<title>HTML5 Canvas Tag</title>

</head>

<body>

<h2>Canvas Square(Geeks For Geeks)</h2>

<canvas id="newCanvas" width="100"

height="100" style="border:1px solid #000000;">

</canvas>

<script>

var c = document.getElementById('newCanvas');

var ctx = c.getContext('2d');

ctx.fillStyle = '#7cce2b';

ctx.fillRect(0, 0, 100, 100);

</script>

</body>

</html>

Output:

Difference between SVG and HTML 5 Canvas - GeeksforGeeks (3)

Difference between SVG and HTML5 Canvas:

SVGCanvas

SVG uses geometric shapes to render graphics

Canvas uses pixels

Vector based (composed of shapes)Raster based (composed of pixel)
SVG has better scalability. So it can be printed with high quality at any resolution.Canvas has poor scalability. Hence it is not suitable for printing on higher resolution.
SVG gives better performance with smaller number of objects or larger surface.Canvas gives better performance with smaller surface or larger number of objects.
SVG can be modified through script and CSS.Canvas can be modified through script only.
Multiple graphical elements, which become the part of the page’s DOM tree.Single element similar to <img> in behavior. Canvas diagram can be saved to PNG or JPG format.


Please Login to comment...

Difference between SVG and HTML 5 Canvas - GeeksforGeeks (2024)
Top Articles
Boarding a Cruise Ship: What First-Time Cruisers Can Expect
A Guide to the Art of Flirting (Over Text)
Katie Nickolaou Leaving
Joe Taylor, K1JT – “WSJT-X FT8 and Beyond”
Cappacuolo Pronunciation
Thor Majestic 23A Floor Plan
Free Atm For Emerald Card Near Me
Coffman Memorial Union | U of M Bookstores
Yi Asian Chinese Union
Celsius Energy Drink Wo Kaufen
Globe Position Fault Litter Robot
Aces Fmc Charting
Gmail Psu
Pekin Soccer Tournament
R Personalfinance
PowerXL Smokeless Grill- Elektrische Grill - Rookloos & geurloos grillplezier - met... | bol
Tu Pulga Online Utah
Rochester Ny Missed Connections
The Many Faces of the Craigslist Killer
Ecampus Scps Login
Regina Perrow
EVO Entertainment | Cinema. Bowling. Games.
Angel Haynes Dropbox
Mjc Financial Aid Phone Number
Meowiarty Puzzle
Bridgestone Tire Dealer Near Me
Lincoln Financial Field, section 110, row 4, home of Philadelphia Eagles, Temple Owls, page 1
Publix Daily Soup Menu
Acuity Eye Group - La Quinta Photos
Craigslist Free Puppy
140000 Kilometers To Miles
Kvoa Tv Schedule
42 Manufacturing jobs in Grayling
Orion Nebula: Facts about Earth’s nearest stellar nursery
PruittHealth hiring Certified Nursing Assistant - Third Shift in Augusta, GA | LinkedIn
Vocabulary Workshop Level B Unit 13 Choosing The Right Word
Bartow Qpublic
Henry Ford’s Greatest Achievements and Inventions - World History Edu
Bob And Jeff's Monticello Fl
Trivago Sf
Isabella Duan Ahn Stanford
Emily Browning Fansite
Collision Masters Fairbanks
10 Types of Funeral Services, Ceremonies, and Events » US Urns Online
Kenwood M-918DAB-H Heim-Audio-Mikrosystem DAB, DAB+, FM 10 W Bluetooth von expert Technomarkt
Sam's Club Gas Price Sioux City
Advance Auto.parts Near Me
antelope valley for sale "lancaster ca" - craigslist
18 Seriously Good Camping Meals (healthy, easy, minimal prep! )
View From My Seat Madison Square Garden
Mawal Gameroom Download
Latest Posts
Article information

Author: Velia Krajcik

Last Updated:

Views: 6192

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Velia Krajcik

Birthday: 1996-07-27

Address: 520 Balistreri Mount, South Armand, OR 60528

Phone: +466880739437

Job: Future Retail Associate

Hobby: Polo, Scouting, Worldbuilding, Cosplaying, Photography, Rowing, Nordic skating

Introduction: My name is Velia Krajcik, I am a handsome, clean, lucky, gleaming, magnificent, proud, glorious person who loves writing and wants to share my knowledge and understanding with you.