Skip to content

Commit

Permalink
fix: the size of an unloaded Image is 0 (#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
wang1212 authored Nov 13, 2024
1 parent 1de86a8 commit abebed9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
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

0 comments on commit abebed9

Please sign in to comment.