{"id":539,"date":"2018-08-03T19:18:04","date_gmt":"2018-08-04T02:18:04","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=3213"},"modified":"2018-08-03T19:18:04","modified_gmt":"2018-08-04T02:18:04","slug":"svg-to-canvas-good-trick","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=539","title":{"rendered":"SVG to Canvas (good trick)"},"content":{"rendered":"<p>Awhile back, I wrote some collision detection code that blitted existing interactive SVG to Canvas and then used the pixel data to figure out various aspects of the relationships between arbitrary SVG nodeTypes. A really simple trick I used can be seen in this pen:<\/p>\n<p><iframe height='300' scrolling='no' title='pJZdav' src='\/\/codepen.io\/ZevanRosser\/embed\/pJZdav\/?height=300&#038;theme-id=5318&#038;default-tab=js,result&#038;embed-version=2' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen <a href='https:\/\/codepen.io\/ZevanRosser\/pen\/pJZdav\/'>pJZdav<\/a> by Zevan Rosser (<a href='https:\/\/codepen.io\/ZevanRosser'>@ZevanRosser<\/a>) on <a href='https:\/\/codepen.io'>CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<p>The trick is to load the svg data into an image as a datauri. There are other tricks like this &#8211; one of which is using an svg `foreignObject` to blit html to canvas:<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Canvas_API\/Drawing_DOM_objects_into_a_canvas\" target=\"blank\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Canvas_API\/Drawing_DOM_objects_into_a_canvas<\/a><\/p>\n<p>There were some browser issues at the time with this. The main one being IE 10\/11 didn&#8217;t really work (tainted canvas if I recall correctly). The `foreignObject` trick didn&#8217;t work with image xlink:hrefs in safari at the time&#8230; (weirdly if you opened the dev tools it would start to work) anyway&#8230;<\/p>\n<p>I ended up forking <a href=\"https:\/\/github.com\/canvg\/canvg\" target=\"blank\">canvg<\/a> for various cases. <a href=\"https:\/\/github.com\/canvg\/canvg\" target=\"blank\">canvg<\/a> is really cool&#8230; just a note, a coworker of mine went in at some point and optimized it like crazy and improved the perf a good deal by &#8220;drying things up&#8221;. Maybe I&#8217;ll suggest that he submit his optimizations at some point.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Awhile back, I wrote some collision detection code that blitted existing interactive SVG to Canvas and then used the pixel data to figure out various aspects of the relationships between arbitrary SVG nodeTypes. A really simple trick I used can be seen in this pen: See the Pen pJZdav by Zevan Rosser (@ZevanRosser) on CodePen. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33,161,160,94,25,180],"tags":[181,162,182,183,134,184,185],"class_list":["post-539","post","type-post","status-publish","format-standard","hentry","category-graphics","category-html5","category-javascript","category-misc","category-pixel-manipulation","category-svg","tag-blitting","tag-canvas","tag-canvg","tag-foreignobject","tag-javascript","tag-svg","tag-trick"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/539","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=539"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/539\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}