Skip to content

Commit

Permalink
Only generate preview image layers when all layer images have been lo…
Browse files Browse the repository at this point in the history
…aded
  • Loading branch information
ReneeIbeji committed May 7, 2024
1 parent f85c021 commit b2dfa56
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions sources/chargen.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ $(document).ready(function() {

function getImage(imgRef) {
if (images[imgRef])
return images[imgRef];
return images[imgRef];
else {
var img = new Image();
img.src = "spritesheets/" + imgRef;
Expand All @@ -612,15 +612,15 @@ $(document).ready(function() {
}
}

function getImage2(imgRef, callback) {
function getImage2(imgRef, callback, layers, prevctx) {
if (images[imgRef]) {
callback(images[imgRef]);
callback(layers, prevctx);
return images[imgRef];
} else {

var img = new Image();
img.src = "spritesheets/" + imgRef;
img.onload = function() { callback(img) };
img.onload = function() { callback(layers, prevctx) };
images[imgRef] = img;
return img;
}
Expand All @@ -647,9 +647,17 @@ $(document).ready(function() {
var prevctx = prev.getContext("2d");
var img = null;
const previewRow = parseInt($(this).data("preview_row"));
var callback = function(img) {
var callback = function(layers,prevctx) {
for(index = 0; index < layers.length; index++){
if(!images[layers[index].link]){
return;
}
}

try {
prevctx.drawImage(img, 0, previewRow * universalFrameSize, universalFrameSize, universalFrameSize, 0, 0, universalFrameSize, universalFrameSize);
layers.forEach((layer) =>{
prevctx.drawImage(images[layer.link], 0, previewRow * universalFrameSize, universalFrameSize, universalFrameSize, 0, 0, universalFrameSize, universalFrameSize);
});
} catch (err) {
console.log(err);
}
Expand All @@ -673,12 +681,13 @@ $(document).ready(function() {
}
}

layers = layers.sort(function(lhs, rhs) {
layers.sort(function(lhs, rhs) {
return parseInt(lhs.zPos) - parseInt(rhs.zPos);
});


layers.forEach((layer) =>{
img = getImage2(layer.link, callback);
img = getImage2(layer.link, callback, layers, prevctx);
});

if (img != null) {
Expand Down

0 comments on commit b2dfa56

Please sign in to comment.