-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
110 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 21 additions & 21 deletions
42
docs/assets/index-rky34ouY.js → docs/assets/index-BDQF4V-l.js
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { getCanvasSize, getEntities } from '../state.ts'; | ||
import { saveAs } from 'file-saver'; | ||
import { convertEntitiesToSvgString } from './export-entities-to-svg.ts'; | ||
|
||
/** | ||
* Takes an svg string and converts it to a png data uri | ||
* by creating an svg element in the dom and drawing that element on the canvas | ||
* Then taking the canvas data and outputting it as a png data blob | ||
* @param svgString | ||
* @param width | ||
* @param height | ||
* @param margin | ||
*/ | ||
export function convertSvgToPngBlob( | ||
svgString: string, | ||
width: number, | ||
height: number, | ||
margin: number, | ||
): Promise<Blob> { | ||
return new Promise<Blob>((resolve, reject) => { | ||
const canvas = document.createElement('canvas'); | ||
const ctx = canvas.getContext('2d'); | ||
|
||
if (!ctx) { | ||
throw new Error('Could not get canvas context'); | ||
} | ||
|
||
const img = new Image(); | ||
const svg = new Blob([svgString], { type: 'image/svg+xml' }); | ||
const url = URL.createObjectURL(svg); | ||
|
||
img.onload = () => { | ||
canvas.width = width + margin * 2; | ||
canvas.height = height + margin * 2; | ||
|
||
ctx.fillStyle = 'white'; | ||
ctx.fillRect(0, 0, canvas.width, canvas.height); | ||
|
||
ctx.drawImage(img, margin, margin); | ||
|
||
URL.revokeObjectURL(url); | ||
|
||
canvas.toBlob(blob => { | ||
if (blob) { | ||
resolve(blob); | ||
} else { | ||
reject(new Error('Could not convert canvas to blob')); | ||
} | ||
}, 'image/png'); | ||
}; | ||
|
||
img.src = url; | ||
}); | ||
} | ||
|
||
export async function exportEntitiesToPngFile() { | ||
const entities = getEntities(); | ||
const canvasSize = getCanvasSize(); | ||
|
||
const svg = convertEntitiesToSvgString(entities, canvasSize); | ||
const pngDataBlob: Blob = await convertSvgToPngBlob( | ||
svg.svgString, | ||
svg.width, | ||
svg.height, | ||
20, | ||
); | ||
|
||
saveAs(pngDataBlob, 'open-web-cad--drawing.png'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters