skinswitcher/src/skin.js

92 lines
2.8 KiB
JavaScript
Raw Normal View History

/* eslint-disable no-undef */
var button = {};
$("button").each((i, e) => {
button[e.id] = e;
});
var view = {
flat: document.querySelectorAll("div.column > img.skin#flat")[0],
mesh: document.querySelectorAll("div.column > div#mesh")[0]
};
var skin = new skinview3d.SkinViewer({
domElement: view.mesh,
width: 300,
height: 300
});
var control = skinview3d.createOrbitControls(skin);
control.enableRotate = true;
control.enableZoom = false;
control.enablePan = false;
function setView(src, controls = true) {
button.switch.style.display = "initial";
view.flat.src = src;
skin.skinUrl = src;
control.enableRotate = controls;
}
button.switch.addEventListener("click", () => {
if (view.flat.style.display == "inline") {
view.flat.style.display = "none";
view.mesh.style.display = "inline";
button.switch.innerHTML = "Switch to 2D";
} else {
view.flat.style.display = "inline";
view.mesh.style.display = "none";
button.switch.innerHTML = "Switch to 3D";
}
});
if (button.download && button.save) {
button.download.addEventListener("click", () => {
ipcRenderer.send("download", {
url: view.flat.src,
properties: {
saveAs: true
}
});
2020-05-12 19:02:20 +02:00
ipcRenderer.on("downloadResult", () => {
notify("success", "Download successful.");
});
});
button.save.addEventListener("click", () => {
const src = view.flat.src;
const r = new RegExp(/^data:image\/png;base64,/);
if (r.test(src)) {
notify("primary", "Decoding Base64...");
var data = src.replace(/^data:image\/png;base64,/, "");
fs.writeFile(path.join(__dirname + `/../assets/skins/download/${Date.now()}.png`), data, "base64", function(err) {
if (err) {
notify("danger", "Skin has not been saved: " + err);
} else {
notify("success", "Skin has been saved");
}
});
} else {
notify("primary", "Getting skin...");
axios({
method: "get",
url: src,
responseType: "stream",
adapter: require("axios/lib/adapters/http")
}).then((res) => {
var p = res.request.path.split("/")[2];
if (p.split(".").length < 2) {
p = p + ".png";
}
res.data.pipe(fs.createWriteStream(path.join(__dirname + `/../assets/skins/download/${p}`)));
notify("success", "Save successful");
});
}
});
}
fs.readFile(path.join(__dirname + "/../assets/skins/dummy.png"), "base64", (err, data) => {
if (err) throw err;
setView("data:image/png;base64," + data);
});