{"id":1232,"date":"2024-03-13T19:51:48","date_gmt":"2024-03-14T01:51:48","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=1232"},"modified":"2024-03-13T19:51:48","modified_gmt":"2024-03-14T01:51:48","slug":"dom-to-png-directly-in-the-browser","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/dom-to-png-directly-in-the-browser\/","title":{"rendered":"DOM to PNG Directly in the Browser"},"content":{"rendered":"\n<p>You could design something on the web then take a screenshot of it. That is, in a basic sense, converting DOM to PNG. But a screenshot is rather manual and finicky. If you had to do this over and over, or you needed a very exact size (like a social media card), you can actually produce a PNG right from the browser with the click of a button. No backend service needed, like <a href=\"https:\/\/pptr.dev\/\">Puppeteer<\/a> or <a href=\"https:\/\/playwright.dev\/\">Playwright<\/a> which are often thought of for this type of automatic screenshotting job.<\/p>\n\n\n\n<p>The trick is that you can render DOM to a <code>&lt;canvas&gt;<\/code>, thanks to the very fancy <a href=\"https:\/\/github.com\/niklasvh\/html2canvas\">html2canvas<\/a> library. Then you can use <code>.toDataURL()<\/code> to get it into PNG format and ultimately download it. <a href=\"https:\/\/codepen.io\/walpolea\/pen\/wvmELrN\">I always reference Andrew Walpole&#8217;s Pen<\/a> to see it done, which also happens to be a rather elegant <a href=\"https:\/\/github.com\/vuejs\/petite-vue\">Petite Vue<\/a> example, and the <code>contenteditable<\/code> usage is a clever way to make it even more usable.<\/p>\n\n\n\n<!--more-->\n\n\n\n<div class=\"wp-block-cp-codepen-gutenberg-embed-block cp_embed_wrapper\"><iframe id=\"cp_embed_wvmELrN\" src=\"\/\/codepen.io\/anon\/embed\/wvmELrN?height=1100&amp;theme-id=47434&amp;slug-hash=wvmELrN&amp;default-tab=result\" height=\"1100\" scrolling=\"no\" frameborder=\"0\" allowfullscreen allowpaymentrequest name=\"CodePen Embed wvmELrN\" title=\"CodePen Embed wvmELrN\" class=\"cp_embed_iframe\" style=\"width:100%;overflow:hidden\">CodePen Embed Fallback<\/iframe><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You could design something on the web then take a screenshot of it. That is, in a basic sense, converting DOM to PNG. But a screenshot is rather manual and finicky. If you had to do this over and over, or you needed a very exact size (like a social media card), you can actually [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1234,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29],"tags":[131,133,3],"class_list":["post-1232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-the-beat","tag-canvas","tag-images","tag-javascript"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2024\/03\/canvas-export-thumb.jpg?fit=1000%2C500&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=1232"}],"version-history":[{"count":3,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1232\/revisions"}],"predecessor-version":[{"id":1236,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/1232\/revisions\/1236"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/1234"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=1232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=1232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=1232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}