SVG:Claude Code 实战指南
了解svg:Claude Code 实战. 包含实用技巧和代码示例。
SVGの操作と优化を通过 Claude Code效率化
SVGは图标、イラスト、グラフなど幅広い用途で使われるベクター形式です。借助 Claude Code,SVGの生成、操作、优化を快速实现可以。
SVG图标システムの构建
> 再利用可能なSVG图标组件创建。
> サイズ、色、アクセシビリティに支持して。
// components/Icon.tsx
interface IconProps {
name: string;
size?: number;
color?: string;
className?: string;
label?: string;
}
const icons: Record<string, string> = {
search: 'M21 21l-5.197-5.197m0 0A7.5 7.5 0 105.196 5.196a7.5 7.5 0 0010.607 10.607z',
menu: 'M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5',
close: 'M6 18L18 6M6 6l12 12',
arrow: 'M8.25 4.5l7.5 7.5-7.5 7.5',
check: 'M4.5 12.75l6 6 9-13.5',
};
export function Icon({ name, size = 24, color = 'currentColor', className, label }: IconProps) {
const path = icons[name];
if (!path) return null;
return (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
stroke={color}
strokeWidth={2}
strokeLinecap="round"
strokeLinejoin="round"
className={className}
role={label ? 'img' : 'presentation'}
aria-label={label}
aria-hidden={!label}
>
<path d={path} />
</svg>
);
}
SVGスプライトの实现
// components/SvgSprite.tsx
export function SvgSprite() {
return (
<svg xmlns="http://www.w3.org/2000/svg" style={{ display: 'none' }}>
<symbol id="icon-home" viewBox="0 0 24 24">
<path d="M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z" />
</symbol>
<symbol id="icon-mail" viewBox="0 0 24 24">
<path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z" />
<polyline points="22,6 12,13 2,6" />
</symbol>
</svg>
);
}
// 使用方法
function NavIcon({ icon, label }: { icon: string; label: string }) {
return (
<svg width="24" height="24" aria-label={label}>
<use href={`#icon-${icon}`} />
</svg>
);
}
SVG动画
<!-- ローディングスピナー -->
<svg width="48" height="48" viewBox="0 0 48 48">
<circle cx="24" cy="24" r="20" fill="none" stroke="#e5e7eb" stroke-width="4" />
<circle cx="24" cy="24" r="20" fill="none" stroke="#3b82f6" stroke-width="4"
stroke-dasharray="80 45"
stroke-linecap="round">
<animateTransform
attributeName="transform"
type="rotate"
from="0 24 24"
to="360 24 24"
dur="1s"
repeatCount="indefinite" />
</circle>
</svg>
/* CSSによるSVGアニメーション */
.svg-draw path {
stroke-dasharray: 1000;
stroke-dashoffset: 1000;
animation: draw 2s ease forwards;
}
@keyframes draw {
to { stroke-dashoffset: 0; }
}
/* ホバーでアイコン変化 */
.icon-hover {
transition: transform 0.2s ease, color 0.2s ease;
}
.icon-hover:hover {
transform: scale(1.1);
color: var(--color-accent);
}
JavaScript通过SVG生成
// 動的にSVGチャートを生成
function createBarChart(data: { label: string; value: number }[]): string {
const width = 400;
const height = 200;
const barWidth = width / data.length - 10;
const maxValue = Math.max(...data.map(d => d.value));
const bars = data.map((d, i) => {
const barHeight = (d.value / maxValue) * (height - 40);
const x = i * (barWidth + 10) + 5;
const y = height - barHeight - 20;
return `
<rect x="${x}" y="${y}" width="${barWidth}" height="${barHeight}"
fill="#3b82f6" rx="4" />
<text x="${x + barWidth / 2}" y="${height - 5}"
text-anchor="middle" font-size="11">${d.label}</text>
`;
}).join('');
return `<svg viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">
${bars}
</svg>`;
}
SVGの优化
> SVG文件を优化するスクリプト创建。
> 不要な属性やメタ数据を删除して、文件サイズを削減して。
// scripts/optimize-svg.ts
import { optimize } from 'svgo';
import { readFileSync, writeFileSync } from 'fs';
import { globSync } from 'glob';
const svgFiles = globSync('src/assets/icons/**/*.svg');
for (const file of svgFiles) {
const svg = readFileSync(file, 'utf-8');
const result = optimize(svg, {
plugins: [
'preset-default',
'removeDimensions',
{ name: 'removeAttrs', params: { attrs: ['data-name', 'class'] } },
{ name: 'addAttributesToSVGElement', params: {
attributes: [{ 'aria-hidden': 'true' }]
}},
],
});
writeFileSync(file, result.data);
const savings = ((1 - result.data.length / svg.length) * 100).toFixed(1);
console.log(`${file}: ${savings}% 削減`);
}
アクセシビリティ
// 装飾的SVG
<svg aria-hidden="true" focusable="false">...</svg>
// 意味のあるSVG
<svg role="img" aria-label="検索アイコン">
<title>検索</title>
...
</svg>
// リンク内SVG
<a href="/search" aria-label="検索ページへ">
<svg aria-hidden="true" focusable="false">...</svg>
</a>
总结
SVGの操作と优化は、性能と用户体験の両面で很重要。借助 Claude Code,图标システムの构建からSVGO优化スクリプトまで、一貫した实现を高效地進められます。CSS动画と組み合わせることで、リッチなビジュアル表現も実現可以。SVG的规范请参阅W3C SVG Specification。
#Claude Code
#SVG
#アイコン
#animation
#optimization
Related Posts
Tips & Tricks
Tips & Tricks
10 个技巧让你的 Claude Code 生产力翻三倍
分享 10 个实用的 Claude Code 使用技巧。从提示词策略到工作流优化,这些方法让你今天就能提升效率。
Tips & Tricks
Tips & Tricks
Canvas/WebGL Optimization:Claude Code 实战指南
了解canvas/webgl optimization:Claude Code 实战. 包含实用技巧和代码示例。
Tips & Tricks
Tips & Tricks
Markdown Implementation:Claude Code 实战指南
了解markdown implementation:Claude Code 实战. 包含实用技巧和代码示例。