Skip to content
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

fix: the size of an unloaded Image is 0 #1831

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/proud-gifts-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@antv/g-plugin-image-loader': patch
---

fix: the size of an unloaded Image is 0
42 changes: 36 additions & 6 deletions __tests__/demos/perf/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,49 @@ export async function image(context: { canvas: Canvas; gui: lil.GUI }) {
console.log(canvas);

const group = new Group();

// let image = new GImage({
// style: {
// x: 0,
// y: 0,
// // width: 100,
// // height: 400,
// // src: 'https://gw.alipayobjects.com/mdn/rms_6ae20b/afts/img/A*N4ZMS7gHsUIAAAAAAAAAAABkARQnAQ',
// // src: 'http://mmtcdp.stable.alipay.net/cto_designhubcore/afts/img/g1a5QYkvbcMAAAAAAAAAAAAADgLVAQBr/original',
// src: 'https://mdn.alipayobjects.com/huamei_fr7vu1/afts/img/A*SqloToP7R9QAAAAAAAAAAAAADkn0AQ/original',
// // src: 'https://freepngimg.com/download/svg/animal/10081.svg',
// },
// });

// group.appendChild(image);

const img = new Image();
img.onload = () => {
console.log('onload', img.complete);
// let image = new GImage({
// style: {
// x: 0,
// y: 0,
// src: img,
// },
// });
// group.appendChild(image);
};

let image = new GImage({
style: {
x: 0,
y: 0,
// width: 100,
// height: 400,
// src: 'https://gw.alipayobjects.com/mdn/rms_6ae20b/afts/img/A*N4ZMS7gHsUIAAAAAAAAAAABkARQnAQ',
// src: 'http://mmtcdp.stable.alipay.net/cto_designhubcore/afts/img/g1a5QYkvbcMAAAAAAAAAAAAADgLVAQBr/original',
src: 'https://mdn.alipayobjects.com/huamei_fr7vu1/afts/img/A*SqloToP7R9QAAAAAAAAAAAAADkn0AQ/original',
src: img,
},
});

group.appendChild(image);

// img.src = 'https://freepngimg.com/download/svg/animal/10081.svg';
img.src =
// 'https://gw.alipayobjects.com/mdn/rms_6ae20b/afts/img/A*N4ZMS7gHsUIAAAAAAAAAAABkARQnAQ';
'https://mdn.alipayobjects.com/huamei_fr7vu1/afts/img/A*SqloToP7R9QAAAAAAAAAAAAADkn0AQ/original';

canvas.appendChild(group);

// ---
Expand Down
6 changes: 6 additions & 0 deletions packages/g-plugin-image-loader/src/ImagePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export class ImagePool {

return new Promise((resolve, reject) => {
imageCache.img.addEventListener('load', () => {
imageCache.size = [
imageCache.img.naturalWidth || imageCache.img.width,
imageCache.img.naturalHeight || imageCache.img.height,
];
imageCache.tileSize = calculateImageTileSize(imageCache.img);
resolve(imageCache);
});
Expand Down Expand Up @@ -223,6 +227,7 @@ export class ImagePool {
async createImageTiles(
src: Image['attributes']['src'],
tiles: [number, number][],
rerender: () => void,
ref: DisplayObject,
): Promise<ImageCache> {
const imageCache = await this.getOrCreateImage(src, ref);
Expand All @@ -241,6 +246,7 @@ export class ImagePool {
imageCache.img,
imageCache.tileSize[0],
imageCache.tileSize[0],
rerender,
),
};

Expand Down
3 changes: 3 additions & 0 deletions packages/g-plugin-image-loader/src/ImageSlicer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export class ImageSlicer {
image: HTMLImageElement,
sliceWidth: number,
sliceHeight: number,
rerender: () => void,
overlap = 0,
api = ImageSlicer.api,
) {
Expand Down Expand Up @@ -123,6 +124,8 @@ export class ImageSlicer {
tileY: row,
data: sliceCanvas,
};

rerender();
});
}
}
Expand Down
Loading