[{"data":1,"prerenderedAt":415},["ShallowReactive",2],{"\u002Farticles\u002Fhow-to-convert-a-svg-to-png-using-canvas":3},{"id":4,"title":5,"body":6,"description":406,"extension":407,"meta":408,"navigation":321,"path":410,"published":411,"seo":412,"stem":413,"__hash__":414},"articles\u002Farticles\u002Fhow-to-convert-a-svg-to-png-using-canvas.md","How to convert a SVG to PNG using Canvas",{"type":7,"value":8,"toc":402},"minimark",[9,16,19,29,106,109,114,225,228,233,259,262,271,383,388,391,395,398],[10,11,12],"blockquote",{},[13,14,15],"p",{},"This article was created using ChatGPT and meant as a placeholder",[13,17,18],{},"Converting an SVG to a PNG in JavaScript using a canvas element is a handy technique for web developers who need to manipulate vector graphics for various applications. This process essentially involves rendering an SVG image onto a canvas and then converting the canvas to a PNG format. Here's a step-by-step guide on how to achieve this:",[20,21,22,26],"ol",{},[23,24,25],"li",{},"Prepare the SVG\nEnsure your SVG code is ready. You can use an SVG file or an SVG string embedded directly in your JavaScript code. If you're using an external SVG file, you'll need to load it into your application.",[23,27,28],{},"Create a Canvas Element\nYou need a canvas element to draw your SVG onto. This can be an existing canvas in your HTML or one created dynamically using JavaScript:",[30,31,36],"pre",{"className":32,"code":33,"language":34,"meta":35,"style":35},"language-js shiki shiki-themes github-light","let canvas = document.createElement(\"canvas\");\ncanvas.width = 500; \u002F\u002F Set the canvas width\ncanvas.height = 500; \u002F\u002F Set the canvas height\n","js","",[37,38,39,72,91],"code",{"__ignoreMap":35},[40,41,44,48,52,55,58,62,65,69],"span",{"class":42,"line":43},"line",1,[40,45,47],{"class":46},"sD7c4","let",[40,49,51],{"class":50},"sgsFI"," canvas ",[40,53,54],{"class":46},"=",[40,56,57],{"class":50}," document.",[40,59,61],{"class":60},"s7eDp","createElement",[40,63,64],{"class":50},"(",[40,66,68],{"class":67},"sYBdl","\"canvas\"",[40,70,71],{"class":50},");\n",[40,73,75,78,80,84,87],{"class":42,"line":74},2,[40,76,77],{"class":50},"canvas.width ",[40,79,54],{"class":46},[40,81,83],{"class":82},"sYu0t"," 500",[40,85,86],{"class":50},"; ",[40,88,90],{"class":89},"sAwPA","\u002F\u002F Set the canvas width\n",[40,92,94,97,99,101,103],{"class":42,"line":93},3,[40,95,96],{"class":50},"canvas.height ",[40,98,54],{"class":46},[40,100,83],{"class":82},[40,102,86],{"class":50},[40,104,105],{"class":89},"\u002F\u002F Set the canvas height\n",[13,107,108],{},"Set the width and height of the canvas to match the desired dimensions of your final PNG.",[20,110,111],{"start":93},[23,112,113],{},"Draw the SVG onto the Canvas\nTo draw the SVG onto the canvas, you need to convert the SVG into an image and then draw that image on the canvas. This can be done using the Image object in JavaScript:",[30,115,117],{"className":32,"code":116,"language":34,"meta":35,"style":35},"let img = new Image();\nimg.onload = function () {\n  let ctx = canvas.getContext(\"2d\");\n  ctx.drawImage(img, 0, 0);\n};\nimg.src = \"data:image\u002Fsvg+xml;base64,\" + btoa(svgString);\n",[37,118,119,137,154,177,199,205],{"__ignoreMap":35},[40,120,121,123,126,128,131,134],{"class":42,"line":43},[40,122,47],{"class":46},[40,124,125],{"class":50}," img ",[40,127,54],{"class":46},[40,129,130],{"class":46}," new",[40,132,133],{"class":60}," Image",[40,135,136],{"class":50},"();\n",[40,138,139,142,145,148,151],{"class":42,"line":74},[40,140,141],{"class":50},"img.",[40,143,144],{"class":60},"onload",[40,146,147],{"class":46}," =",[40,149,150],{"class":46}," function",[40,152,153],{"class":50}," () {\n",[40,155,156,159,162,164,167,170,172,175],{"class":42,"line":93},[40,157,158],{"class":46},"  let",[40,160,161],{"class":50}," ctx ",[40,163,54],{"class":46},[40,165,166],{"class":50}," canvas.",[40,168,169],{"class":60},"getContext",[40,171,64],{"class":50},[40,173,174],{"class":67},"\"2d\"",[40,176,71],{"class":50},[40,178,180,183,186,189,192,195,197],{"class":42,"line":179},4,[40,181,182],{"class":50},"  ctx.",[40,184,185],{"class":60},"drawImage",[40,187,188],{"class":50},"(img, ",[40,190,191],{"class":82},"0",[40,193,194],{"class":50},", ",[40,196,191],{"class":82},[40,198,71],{"class":50},[40,200,202],{"class":42,"line":201},5,[40,203,204],{"class":50},"};\n",[40,206,208,211,213,216,219,222],{"class":42,"line":207},6,[40,209,210],{"class":50},"img.src ",[40,212,54],{"class":46},[40,214,215],{"class":67}," \"data:image\u002Fsvg+xml;base64,\"",[40,217,218],{"class":46}," +",[40,220,221],{"class":60}," btoa",[40,223,224],{"class":50},"(svgString);\n",[13,226,227],{},"In this code, replace svgString with your SVG code. If you're using an external SVG file, ensure the file is read and converted into a base64 string.",[20,229,230],{"start":179},[23,231,232],{},"Convert Canvas to PNG\nOnce your SVG is rendered on the canvas, you can convert the canvas to a PNG image:",[30,234,236],{"className":32,"code":235,"language":34,"meta":35,"style":35},"let pngUrl = canvas.toDataURL(\"image\u002Fpng\");\n",[37,237,238],{"__ignoreMap":35},[40,239,240,242,245,247,249,252,254,257],{"class":42,"line":43},[40,241,47],{"class":46},[40,243,244],{"class":50}," pngUrl ",[40,246,54],{"class":46},[40,248,166],{"class":50},[40,250,251],{"class":60},"toDataURL",[40,253,64],{"class":50},[40,255,256],{"class":67},"\"image\u002Fpng\"",[40,258,71],{"class":50},[13,260,261],{},"This pngUrl is a base64 encoded string representing your PNG image.",[20,263,264],{"start":201},[23,265,266,267,270],{},"Use or Save the PNG Image\nNow that you have your PNG in base64 format, you can use it as needed in your application. For example, you can display it in an ",[268,269],"img",{}," element or download it:",[30,272,274],{"className":32,"code":273,"language":34,"meta":35,"style":35},"let imgElement = document.createElement(\"img\");\nimgElement.src = pngUrl;\ndocument.body.appendChild(imgElement);\n\n\u002F\u002F To download the image\nlet downloadLink = document.createElement(\"a\");\ndownloadLink.href = pngUrl;\ndownloadLink.download = \"image.png\";\ndownloadLink.click();\n",[37,275,276,296,306,317,323,328,348,358,372],{"__ignoreMap":35},[40,277,278,280,283,285,287,289,291,294],{"class":42,"line":43},[40,279,47],{"class":46},[40,281,282],{"class":50}," imgElement ",[40,284,54],{"class":46},[40,286,57],{"class":50},[40,288,61],{"class":60},[40,290,64],{"class":50},[40,292,293],{"class":67},"\"img\"",[40,295,71],{"class":50},[40,297,298,301,303],{"class":42,"line":74},[40,299,300],{"class":50},"imgElement.src ",[40,302,54],{"class":46},[40,304,305],{"class":50}," pngUrl;\n",[40,307,308,311,314],{"class":42,"line":93},[40,309,310],{"class":50},"document.body.",[40,312,313],{"class":60},"appendChild",[40,315,316],{"class":50},"(imgElement);\n",[40,318,319],{"class":42,"line":179},[40,320,322],{"emptyLinePlaceholder":321},true,"\n",[40,324,325],{"class":42,"line":201},[40,326,327],{"class":89},"\u002F\u002F To download the image\n",[40,329,330,332,335,337,339,341,343,346],{"class":42,"line":207},[40,331,47],{"class":46},[40,333,334],{"class":50}," downloadLink ",[40,336,54],{"class":46},[40,338,57],{"class":50},[40,340,61],{"class":60},[40,342,64],{"class":50},[40,344,345],{"class":67},"\"a\"",[40,347,71],{"class":50},[40,349,351,354,356],{"class":42,"line":350},7,[40,352,353],{"class":50},"downloadLink.href ",[40,355,54],{"class":46},[40,357,305],{"class":50},[40,359,361,364,366,369],{"class":42,"line":360},8,[40,362,363],{"class":50},"downloadLink.download ",[40,365,54],{"class":46},[40,367,368],{"class":67}," \"image.png\"",[40,370,371],{"class":50},";\n",[40,373,375,378,381],{"class":42,"line":374},9,[40,376,377],{"class":50},"downloadLink.",[40,379,380],{"class":60},"click",[40,382,136],{"class":50},[384,385,387],"h3",{"id":386},"additional-considerations","Additional Considerations",[13,389,390],{},"Cross-Origin Issues: If you're loading an SVG from an external source, you may encounter cross-origin issues. Ensure CORS policies are configured correctly on the server hosting the SVG file.\nSVG Features: Some SVG features may not render correctly on canvas, so test your SVGs thoroughly.\nPerformance: For large SVG files, consider the performance implications of this conversion process.",[384,392,394],{"id":393},"conclusion","Conclusion",[13,396,397],{},"Converting SVG to PNG using JavaScript and canvas is a powerful technique that can be integrated into web applications for dynamic image manipulation. By following these steps, developers can effectively translate the versatility of SVGs into the wide compatibility of PNGs.",[399,400,401],"style",{},"html pre.shiki code .sD7c4, html code.shiki .sD7c4{--shiki-default:#D73A49}html pre.shiki code .sgsFI, html code.shiki .sgsFI{--shiki-default:#24292E}html pre.shiki code .s7eDp, html code.shiki .s7eDp{--shiki-default:#6F42C1}html pre.shiki code .sYBdl, html code.shiki .sYBdl{--shiki-default:#032F62}html pre.shiki code .sYu0t, html code.shiki .sYu0t{--shiki-default:#005CC5}html pre.shiki code .sAwPA, html code.shiki .sAwPA{--shiki-default:#6A737D}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":35,"searchDepth":74,"depth":74,"links":403},[404,405],{"id":386,"depth":93,"text":387},{"id":393,"depth":93,"text":394},"A simple way to convert a SVG to PNG using Canvas","md",{"slug":409},"how-to-convert-a-svg-to-png-using-canvas","\u002Farticles\u002Fhow-to-convert-a-svg-to-png-using-canvas","2023-11-22",{"title":5,"description":406},"articles\u002Fhow-to-convert-a-svg-to-png-using-canvas","6cHcJk_zBXFXaEKELLixM07Lj3CFlu9HHcAiVl7trOc",1780299452030]