Last active
February 21, 2024 13:46
-
-
Save yikuansun/c0f1a602b4e9d4e344a41c4f49ded3bf to your computer and use it in GitHub Desktop.
Useful algorithms for Photopea plugin development
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
// uses Photopea.js | |
var addImageAndWait = async function(contentWindow, imgURI) { | |
return new Promise(async function(resolve) { | |
var layerCountOld = "done"; | |
while (layerCountOld == "done") layerCountOld = (await Photopea.runScript(contentWindow, `app.echoToOE(app.activeDocument.layers.length)`))[0]; | |
var layerCountNew = layerCountOld; | |
await Photopea.runScript(contentWindow, `app.open("${imgURI}", null, true);`); | |
var layerCheckInterval = async function () { | |
layerCountNew = (await Photopea.runScript(contentWindow, `app.echoToOE(app.activeDocument.layers.length)`))[0]; | |
if (layerCountNew == layerCountOld + 1) { | |
resolve(); | |
return; | |
} | |
else setTimeout(layerCheckInterval, 50); | |
}; | |
layerCheckInterval(); | |
}); | |
}; |
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
// uses Photopea.js | |
var getDocumentAsImage = async function(contentWindow) { | |
// returns Image object containing image data from Photopea document | |
return new Promise(async function(resolve) { | |
Photopea.runScript(contentWindow, "app.activeDocument.saveToOE('png')").then(function(data) { | |
var buffer = data[0]; | |
var fR = new FileReader(); | |
fR.addEventListener("load", function(e) { | |
let img = new Image(); | |
img.src = e.target.result; | |
resolve(img); | |
}); | |
fR.readAsDataURL(new Blob([buffer], { type: "image/png" })); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment