{"id":1382,"date":"2021-01-26T18:24:43","date_gmt":"2021-01-27T01:24:43","guid":{"rendered":"http:\/\/www.keganv.com\/?p=1382"},"modified":"2021-01-26T18:26:34","modified_gmt":"2021-01-27T01:26:34","slug":"canvas-to-png-download-function-javascript","status":"publish","type":"post","link":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/","title":{"rendered":"Canvas To PNG Download Function JavaScript"},"content":{"rendered":"<p>Here is a little code snippet to hopefully help you out and keep in your toolbox whenever you may need it. The following code was written in Angular (Typescript). The <code>this.canvas<\/code> is a template variable.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">&lt;canvas #canvas&gt;&lt;\/canvas&gt;<\/pre>\n<p>The component utilized the <a href=\"https:\/\/angular.io\/api\/core\/ViewChild\" rel=\"noopener\" target=\"_blank\">@ViewChild decorator<\/a>.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">@ViewChild('canvas') protected canvas: ElementRef;<\/pre>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\ndownload() {\r\n  this.canvas.nativeElement.toBlob((blob: Blob) =&gt; {\r\n    const url = URL.createObjectURL(blob); \/\/ Create the URL from the blob object\r\n    const a = document.createElement('a'); \/\/ Create the anchor tag\r\n    a.href = url; \/\/ Assign the URL string to the anchor tag's href attribute\r\n    a.download = 'chart.png'; \/\/ Name the image by assigning the anchor tag's download attribute\r\n    document.body.appendChild(a); \/\/ Append the anchor tag to the DOM\r\n    a.click(); \/\/ Click the anchor tag to trigger the download\r\n    document.body.removeChild(a); \/\/ Remove the anchor tag from the DOM\r\n    URL.revokeObjectURL(url); \/\/ Release the reference to the ObjectURL\r\n  }, 'image\/png', .92);\r\n}\r\n<\/pre>\n<p>You can see how this function can easily be modified to suit your needs, whether that is in a functional component in React, or my personal favorite, vanilla JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"Here is a little code snippet to hopefully help you out and keep in your toolbox whenever you may need it. The following code was written in Angular (Typescript). The this.canvas is a template variable. &lt;canvas #canvas&gt;&lt;\/canvas&gt; The component utilized the @ViewChild decorator. @ViewChild(&#8216;canvas&#8217;) protected canvas: ElementRef; download() { this.canvas.nativeElement.toBlob((blob: Blob) =&gt; { const url &#8230; <br\/> <a class=\"view-article btn btn-primary flip pull-left\" href=\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/\"><span>View Article<\/span><\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1382","post","type-post","status-publish","format-standard","hentry","category-coding-programming"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Canvas To PNG Download Function JavaScript | KeganV<\/title>\n<meta name=\"description\" content=\"Here is a JavaScript code snippet for converting a canvas to a PNG image then automatically downloading the image.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Canvas To PNG Download Function JavaScript | KeganV\" \/>\n<meta property=\"og:description\" content=\"Here is a JavaScript code snippet for converting a canvas to a PNG image then automatically downloading the image.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"KeganV\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-27T01:24:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-27T01:26:34+00:00\" \/>\n<meta name=\"author\" content=\"Kegan V.\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kegan V.\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/\"},\"author\":{\"name\":\"Kegan V.\",\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"headline\":\"Canvas To PNG Download Function JavaScript\",\"datePublished\":\"2021-01-27T01:24:43+00:00\",\"dateModified\":\"2021-01-27T01:26:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/\"},\"wordCount\":198,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"articleSection\":[\"Coding &amp; Programming\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/\",\"url\":\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/\",\"name\":\"Canvas To PNG Download Function JavaScript | KeganV\",\"isPartOf\":{\"@id\":\"https:\/\/www.keganv.com\/#website\"},\"datePublished\":\"2021-01-27T01:24:43+00:00\",\"dateModified\":\"2021-01-27T01:26:34+00:00\",\"description\":\"Here is a JavaScript code snippet for converting a canvas to a PNG image then automatically downloading the image.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.keganv.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Canvas To PNG Download Function JavaScript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.keganv.com\/#website\",\"url\":\"https:\/\/www.keganv.com\/\",\"name\":\"KeganV\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.keganv.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276\",\"name\":\"Kegan V.\",\"logo\":{\"@id\":\"https:\/\/www.keganv.com\/#\/schema\/person\/image\/\"},\"sameAs\":[\"http:\/\/www.keganv.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Canvas To PNG Download Function JavaScript | KeganV","description":"Here is a JavaScript code snippet for converting a canvas to a PNG image then automatically downloading the image.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/","og_locale":"en_US","og_type":"article","og_title":"Canvas To PNG Download Function JavaScript | KeganV","og_description":"Here is a JavaScript code snippet for converting a canvas to a PNG image then automatically downloading the image.","og_url":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/","og_site_name":"KeganV","article_published_time":"2021-01-27T01:24:43+00:00","article_modified_time":"2021-01-27T01:26:34+00:00","author":"Kegan V.","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kegan V.","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/#article","isPartOf":{"@id":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/"},"author":{"name":"Kegan V.","@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"headline":"Canvas To PNG Download Function JavaScript","datePublished":"2021-01-27T01:24:43+00:00","dateModified":"2021-01-27T01:26:34+00:00","mainEntityOfPage":{"@id":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/"},"wordCount":198,"commentCount":0,"publisher":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"articleSection":["Coding &amp; Programming"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/","url":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/","name":"Canvas To PNG Download Function JavaScript | KeganV","isPartOf":{"@id":"https:\/\/www.keganv.com\/#website"},"datePublished":"2021-01-27T01:24:43+00:00","dateModified":"2021-01-27T01:26:34+00:00","description":"Here is a JavaScript code snippet for converting a canvas to a PNG image then automatically downloading the image.","breadcrumb":{"@id":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.keganv.com\/canvas-to-png-download-function-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.keganv.com\/"},{"@type":"ListItem","position":2,"name":"Canvas To PNG Download Function JavaScript"}]},{"@type":"WebSite","@id":"https:\/\/www.keganv.com\/#website","url":"https:\/\/www.keganv.com\/","name":"KeganV","description":"","publisher":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.keganv.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/412e7755f594475dc403b6d774b80276","name":"Kegan V.","logo":{"@id":"https:\/\/www.keganv.com\/#\/schema\/person\/image\/"},"sameAs":["http:\/\/www.keganv.com"]}]}},"_links":{"self":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/1382"}],"collection":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/comments?post=1382"}],"version-history":[{"count":23,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/1382\/revisions"}],"predecessor-version":[{"id":1405,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/posts\/1382\/revisions\/1405"}],"wp:attachment":[{"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/media?parent=1382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/categories?post=1382"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.keganv.com\/api\/wp\/v2\/tags?post=1382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}