Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.save() method is not saving rects. #50

Closed
JohannesHoffmann opened this issue Mar 7, 2023 · 4 comments
Closed

.save() method is not saving rects. #50

JohannesHoffmann opened this issue Mar 7, 2023 · 4 comments

Comments

@JohannesHoffmann
Copy link

JohannesHoffmann commented Mar 7, 2023

Hi, i run into an issue. I want to save my packer by using packer.save() but the rect property of my bins are empty.
So i dug into the code and found that the rect prop is not filld here:

/**
* Output current bins to save
*
* @memberof MaxRectsPacker
*/
public save (): IBin[] {
let saveBins: IBin[] = [];
this.bins.forEach((bin => {
let saveBin: IBin = {
width: bin.width,
height: bin.height,
maxWidth: bin.maxWidth,
maxHeight: bin.maxHeight,
freeRects: [],
rects: [],
options: bin.options
};
if (bin.tag) saveBin = { ...saveBin, tag: bin.tag };
bin.freeRects.forEach(r => {
saveBin.freeRects.push({
x: r.x,
y: r.y,
width: r.width,
height: r.height
});
});
saveBins.push(saveBin);
}));
return saveBins;
}

To understand the problem i created a sandbox with the problem:
https://codesandbox.io/s/musing-sound-vedd2b?file=/src/index.ts

version 2.7.3

@soimy
Copy link
Owner

soimy commented Mar 8, 2023

The purpose of save method is to save the freerects of a packer for later use, ie. adding more rects into the packer without overlapping the previous packed rects position.

The final result of the packed rects are stored in every rects position inside packer.bins, example of access them:

packer.bins.forEach(bin => {
    console.log(bin.rects);
});

This repo is just a simple packing algorithm module, so you have to find your own way storing them.

@JohannesHoffmann
Copy link
Author

Thank you for your quick response!
I missread the readme about the purpose of the saving method. Maybe renaming it to saveSettings is more clearly for developers?
Second,i need to send the packed bin as a post reqest. The packers -> bin -> rects holding instances of the rect class. Serialize this to get a plain js object makes the rects look like this: { _x: 0, _y: 0, _width: 100, _height: 100}. This is why i thought the save method will create an correct rect without the underscores.

sure i found my own way already but maybe this are points to improve this simple nice module?

@soimy
Copy link
Owner

soimy commented Mar 8, 2023

The packer.bin.rects are Array of Objects extended from IRectangle interface, take a look at:

export interface IRectangle {
width: number
height: number
x: number
y: number
[propName: string]: any
}

You can simply use JSON.stringify() to serialize as json. For more complicated export, take a look at
https://github.com/soimy/atlasify/blob/master/src/exporter.ts

@soimy
Copy link
Owner

soimy commented Mar 8, 2023

BTW, Let's move this post to discussions

Repository owner locked and limited conversation to collaborators Mar 8, 2023
@soimy soimy converted this issue into discussion #52 Mar 8, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants