MCPcopy Create free account
hub / github.com/JKPotato-Computer/SignMaker / saveToPng

Function saveToPng

js/main.js:1101–1161  ·  view source on GitHub ↗
(file,isPreview,isSVG)

Source from the content-addressed store, hash-verified

1099 }
1100
1101 const saveToPng = async function(file,isPreview,isSVG) {
1102 let scaleMultiplier = 5;
1103 //let result = -11.09773 * (Math.log(scaleMultiplier) / Math.log(0.469922)) + 11.4557;
1104 return new Promise((resolve, reject) => {
1105 let svg;
1106 if (fileInfo.panel == -1) {
1107 svg = htmlToImage.toSvg(file);
1108 } else {
1109 svg = htmlToImage.toSvg(file, {
1110 width: file.getBoundingClientRect().width * scaleMultiplier,
1111 height: file.getBoundingClientRect().height * scaleMultiplier,
1112 canvasWidth: file.getBoundingClientRect().width * scaleMultiplier,
1113 canvasHeight: file.getBoundingClientRect().height * scaleMultiplier,
1114 style: {
1115 position: "fixed",
1116 inset: "40%",
1117 scale: scaleMultiplier,
1118 }
1119 });
1120 }
1121 svg.then(function(dataUrl) {
1122 if (isSVG) {
1123 if (isPreview) {
1124 resolve(dataUrl);
1125 }
1126 downloadFile(dataUrl,".svg");
1127 return;
1128 }
1129
1130 let tmpCanvas = document.createElement("canvas");
1131 let ctx = tmpCanvas.getContext("2d");
1132
1133 let tmpImg = new Image();
1134 tmpImg.addEventListener("load", onTempImageLoad);
1135 tmpImg.src = dataUrl;
1136
1137 //console.log(tmpImg.width, tmpImg.height);
1138
1139 tmpCanvas.width = file.getBoundingClientRect().width;
1140
1141
1142 // let targetImg = new Image();
1143
1144 function onTempImageLoad(e) {
1145 tmpCanvas.width = e.target.width;
1146 tmpCanvas.height = e.target.height;
1147
1148 ctx.drawImage(e.target, 0, 0);
1149 if (isPreview) {
1150 resolve(tmpCanvas.toDataURL());
1151 } else {
1152 downloadFile(tmpCanvas.toDataURL(),".png");
1153 resolve(true);
1154 }
1155 };
1156
1157 }).catch(function(error) {
1158 console.error("Error Saving!", error);

Callers 2

downloadSignFunction · 0.85
updatePreviewFunction · 0.85

Calls 1

downloadFileFunction · 0.85

Tested by

no test coverage detected