
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to draw an SVG file on an HTML5 canvas?
To draw SVG onto canvas, you need to use SVG image. Firstly, use the <foreignObject> element which contains the HTML. After that, you need to draw the SVG image into the canvas.
Example
You can try the following code to draw an SVG file on an HTML canvas
<!DOCTYPE html> <html> <head> <title>SVG file on HTML Canvas </title> </head> <body> <canvas id="myCanvas" style="border:2px solid green;" width="300" height="300"></canvas> <script> var canvas = document.getElementById('myCanvas'); var ctx = canvas.getContext('2d'); var data = '<svg xmlns="http://www.w3.org/2000/svg" width="300" height="200">' + '<foreignObject width="100%" height="100%">' + '<div xmlns="http://www.w3.org/1999/xhtml" style="font-size:50px">' + 'Simply Easy ' + '<span style="color:blue;">' + 'Learning</span>' + '</div>' + '</foreignObject>' + '</svg>'; var DOMURL = window.URL || window.webkitURL || window; var img1 = new Image(); var svg = new Blob([data], {type: 'image/svg+xml'}); var url = DOMURL.createObjectURL(svg); img1.onload = function() { ctx.drawImage(img1, 25, 70); DOMURL.revokeObjectURL(url); } img1.src = url; </script> </body> </html>
Output
- Related Articles
- Drawing an SVG file on an HTML5 canvas
- How to draw grid using HTML5 and canvas or SVG?
- How to draw an oval in HTML5 canvas?
- World Map on HTML5 Canvas or SVG
- How to draw large font on HTML5 Canvas?
- How to draw a rectangle on HTML5 Canvas?
- How to draw SVG Logo in HTML5?
- Draw part of an image inside HTML5 canvas
- How to draw a quadratic curve on HTML5 Canvas?
- How to draw sine waves with HTML5 SVG?
- How to draw a rectangle in HTML5 SVG?
- How to draw a polygon in HTML5 SVG?
- How to draw a polyline in HTML5 SVG?
- How to draw a star in HTML5 SVG?
- How to draw shapes using SVG in HTML5?

Advertisements