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: v2 image bugfix #199

Merged
merged 1 commit into from
Sep 4, 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
18 changes: 17 additions & 1 deletion src/skia/graphic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import SkiaPathRebuilder from './SkiaPathRebuilder';
import { ReactElement } from 'react';
import mapStyleToAttrs from 'zrender/lib/svg/mapStyleToAttrs';
import {
Skia,
Path as SkiaPath,
Text as SkiaText,
matchFont,
Expand Down Expand Up @@ -313,17 +314,32 @@ export function brushSVGImage(
y={y}
width={dw}
height={dh}
{...attrs}
>
{effects}
</SkiaImage>
);
}
type CustomImageProps = SkiaProps<ImageProps> & {
href: string;
height?: number;
width?: number;
children?: ReactElement[];
};
function SkiaImage({ href, children, ...attrs }: CustomImageProps) {
const skiaImage = useImage(href);
let skiaImage = useImage(href);
// Base64 image
if (href.indexOf(';base64') > -1) {
const base64Data = href.split(',')[1] || '';
const binaryData = Skia.Data.fromBase64(base64Data);
skiaImage = Skia.Image.MakeImageFromEncoded(binaryData);
}
const imageInfo = skiaImage?.getImageInfo();
if (imageInfo && !attrs.width) {
const { height: oriHeight, width: oriWidth } = imageInfo;
attrs.width = attrs.height ? (attrs.height / oriHeight) * oriWidth : 0;
}
// attrs.fit = 'fill'; // Here you can set 'Image' fit mode
return (
<Image {...attrs} image={skiaImage}>
{children}
Expand Down
39 changes: 38 additions & 1 deletion src/svg/svgChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import React, {
useCallback,
} from 'react';

import { Platform, View } from 'react-native';
import { Platform, View, Image as RNImage } from 'react-native';

import {
setPlatformAPI,
Expand Down Expand Up @@ -80,6 +80,32 @@ const tagMap = {
mask: Mask,
};

const imageSizeMap: any = {};

interface ImageSize {
width: number;
height: number;
}

// 使用 async/await 获取图片尺寸
const getImageSize = async (uri: string): Promise<ImageSize> => {
return new Promise((resolve, reject) => {
RNImage.getSize(
uri,
(width, height) => resolve({ width, height }),
(error) => reject(error)
);
});
};

async function imageInit(url: string) {
const { width, height } = await getImageSize(url);
imageSizeMap[url] = {
width,
height,
};
}

function toCamelCase(str: string) {
var reg = /-(\w)/g;
return str.replace(reg, function (_: any, $1: string) {
Expand Down Expand Up @@ -170,6 +196,17 @@ function SvgEle(props: SVGVEleProps) {
</Tag>
);
}
if (tag === 'image' && !attrs.width) {
if (imageSizeMap[attrs.href]) {
const { width, height } = imageSizeMap[attrs.href];
attrs.width = (attrs.height / height) * width;
if (attrs.height === 0) {
attrs.opacity = 0;
}
} else {
imageInit(attrs.href);
}
}
return (
<Tag key={node.key} {...attrs}>
{children?.map((child) => <SvgEle key={child.key} node={child} />)}
Expand Down
Loading