Drawing SVG content to Canvas's HTML5 application

Source: Internet
Author: User

SVG and canvas are two completely different techniques for drawing graphical applications on HTML5, both have advantages and disadvantages, but they are not incompatible with each other, especially SVG content can be directly drawn on the canvas function, so that the two can be perfectly fused together, Let canvas enjoy the existing rich SVG footage without sacrificing the SVG vector stepless scaling feature.

"HTML5-based drag and drop generation picture Base64 information" Although the display is drag and drop ordinary raster image effect, but you can also directly drag and drop the SVG format image to display, but the normal picture format data for the Data:image/png type, In the SVG format, the data type is data:image/svg+xml and dragged into the SVG format picture for the HT for Web topology graph:

Here's a small example of how to load an SVG image and divide it into seven basic scale-drawing effects, and you can see that canvas drawing svg retains its vector-not-distorted properties

function Draw () {    var img = new Image ();    IMG.SRC = ' chart.svg ';    Document.body.appendChild (IMG);    Img.onload = function () {        var canvas = document.getElementById (' canvas ');        var g = Canvas.getcontext (' 2d ');         var width = img.clientwidth * 1.5;        var height = img.clientheight * 1.5;                        var x = 2;        var y = 2;        for (var i=0; i<7; i++) {            g.drawimage (img, x, y, width, height);            X + = width + 2;            Width/= 2;            Height/= 2;}}                ;}

Referring to the fusion of canvas and SVG, we will use the HT for web vector function to show a mobile phone battery charging Progress instance, the entire cell phone battery static part of the implementation by loading a simple SVG footage, and the charge dynamic change part, we use a progressive color of the HT rectangle element to describe , the length of the rectangle is converted into length information according to the percentage of the charging progress by the dynamic binding function of the HT vector data, and finally the dynamic charging effect is achieved by the timer simulation data change:


Ht. Default.setimage (' battery ', {width:64,height:64,comps: [   {type: ' rect ', rect: {func:function (data) {return [5, 25, 50*DATA.A (' percent '), 16]}},background: ' Red ', gradient: ' spread.vertical '},                        {type: ' image ', Name: ' Battery.svg ', Relative:true,rect: [0, 0, 1, 1]});               var node = new ht. Node (); node.setposition, node.setimage (' battery '); NODE.S (' Image.stretch ', ' uniform '); Node.a (' percent ', 0) ;d atamodel.add (node);   Graphview.seteditable (True); SetInterval (function () {percent = Node.a (' percent ') + 0.02;if (Percent > 1) {percent = 0;} Node.a (' percent ', percent);}, 16);

There is also a special application scenario for SVG drawing to canvas, which is to describe HTML elements in SVG through the Foreignobject feature of SVG, and then canvas to draw svg. You can draw the HTML content of the Foreignobject description to the Canvas, see Https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Drawing_DOM The _objects_into_a_canvas instance, where the blob is used to set the IMG src as the URL is a more bizarre technical point, but from the above mentioned we can actually convert the entire SVG content into data:image/svg+xml; The base64 content can be passed as src URL, so I modified the example, using Btoa (data) to convert the SVG content into Base64 way to set IMG.SRC, this way easier to understand, example code and the effect is as follows:/HTTP V.youku.com/v_show/id_xodg0mtu4njey.html

function Draw () {var canvas = document.getElementById (' canvas '); var ctx = Canvas.getcontext (' 2d '); var data = ...; var img = New Image (); img.onload = function () {ctx.drawimage (img, 0, 0);};            IMG.SRC = ' data:image/svg+xml;base64, ' + btoa (data);}


Drawing SVG content to Canvas's HTML5 application

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.