svg中viewbox图解分析

SVG视区(viewbox)属性详解及示例
本文介绍了SVG中的viewbox属性,通过示例解释了如何使用该属性在SVG画布上截取并放大指定区域。首先创建了一个300*300的SVG,并绘制了一个矩形。接着设置了viewbox属性,值为10 55 560 60,分别代表左上角坐标和截取的视区尺寸。通过改变viewbox,可以实现不同比例的缩放效果,帮助读者深入理解viewbox的工作原理。

svg中有一个viewbox属性,中文翻译为视区,就是在svg上截取一小块,放大到整个svg显示。

这个属性初学的话有点难以理解,这边做一个简单的示例,一看就明白了。

**1)**先来一个svg,宽高各位300,设置一下边框:

<svg width="300" height="300" style="border: 1px solid steelblue">
</svg>

运行效果:

这边其实就是一个300*300的矩形。

**2)**svg里面画一个矩形:

<svg width="300" height="300" style="border: 1px solid steelblue">
	<rect x="10" y="10" width="200" height="100" fill="skyblue"></rect>
</svg>

运行效果:

**3)**设置svg的viewbox属性:

<svg width="300" height="300" style="border: 1px solid steelblue" 
	viewbox="105 55 60 60">
<rect x="10" y="10" width="200" height="100" fill="skyblue"></rect>
</svg>

先分析一下原理,再来看效果,上面设置的viewbox的值为:105 55 60 60,每个参数的含义如下:

105 表示相对于svg左上角的横坐标。
55 表示相对于svg左上角的纵坐标。
60 表示截取的视区的宽度。
60 表示截取的视区的高度。	

图示原理:

4) 非等比例的viewbox

上面的viewbox宽高和svg等比例,如果等比例,那么就是其中一边放到最大。

<svg width="300" height="300" style="border: 1px solid steelblue" 
	viewbox="105 55 60 60">
<rect x="10" y="10" width="200" height="100" fill="skyblue"></rect>
</svg>

图示原理:

还有不理解的可以给我留言。

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 600"> <!-- 背景和标题 --> <rect width="1000" height="600" fill="#f5f5f5"/> <text x="500" y="30" font-family="Arial" font-size="24" text-anchor="middle" font-weight="bold">原型模式 - 对象克隆对比图</text> <!-- 原始对象 --> <rect x="100" y="100" width="250" height="200" rx="15" fill="#e6f7ff" stroke="#1890ff" stroke-width="3"/> <text x="225" y="130" font-family="Arial" font-size="18" text-anchor="middle" font-weight="bold">原始对象(Prototype)</text> <text x="130" y="170" font-family="Arial" font-size="14">基本属性1: 值1</text> <text x="130" y="200" font-family="Arial" font-size="14">基本属性2: 值2</text> <text x="130" y="230" font-family="Arial" font-size="14">引用属性: → 对象A(蓝色圆圈)</text> <!-- 引用对象A(原始) --> <circle cx="180" cy="350" r="40" fill="#1890ff" stroke="#0050b3" stroke-width="2"/> <text x="180" y="355" font-family="Arial" font-size="12" fill="white" text-anchor="middle">对象A(原始引用)</text> <!-- 浅克隆对象 --> <rect x="400" y="100" width="250" height="200" rx="15" fill="#fff2e8" stroke="#fa8c16" stroke-width="3"/> <text x="525" y="130" font-family="Arial" font-size="18" text-anchor="middle" font-weight="bold">浅克隆对象</text> <text x="430" y="170" font-family="Arial" font-size="14">基本属性1: 值1(独立复制)</text> <text x="430" y="200" font-family="Arial" font-size="14">基本属性2: 值2(独立复制)</text> <text x="430" y="230" font-family="Arial" font-size="14">引用属性: → 对象A(共享原始引用)</text> <!-- 深克隆对象 --> <rect x="700" y="300" width="250" height="200" rx="15" fill="#f6ffed" stroke="#52c41a" stroke-width="3"/> <text x="825" y="330" font-family="Arial" font-size="18" text-anchor="middle" font-weight="bold">深克隆对象</text> <text x="730" y="370" font-family="Arial" font-size="14">基本属性1: 值1(独立复制)</text> <text x="730" y="400" font-family="Arial" font-size="14">基本属性2: 值2(独立复制)</text> <text x="730" y="430" font-family="Arial" font-size="14">引用属性: → 对象B(全新克隆对象)</text> <!-- 引用对象B(深克隆后) --> <circle cx="850" cy="550" r="40" fill="#52c41a" stroke="#389e0d" stroke-width="2"/> <text x="850" y="555" font-family="Arial" font-size="12" fill="white" text-anchor="middle">对象B(深克隆新引用)</text> <!-- 浅克隆箭头(共享引用) --> <path d="M250 250 Q 350 250 400 100" stroke="#fa8c16" stroke-width="4" stroke-dasharray="10 5" fill="none"/> <polygon points="390 110 400 100 410 110" fill="#fa8c16"/> <text x="325" y="180" font-family="Arial" font-size="14" fill="#fa8c16" font-weight="bold">浅克隆(共享引用)</text> <!-- 深克隆箭头(递归克隆引用) --> <path d="M250 280 Q 500 400 700 300" stroke="#52c41a" stroke-width="4" stroke-dasharray="10 5" fill="none"/> <polygon points="690 310 700 300 710 310" fill="#52c41a"/> <text x="550" y="380" font-family="Arial" font-size="14" fill="#52c41a" font-weight="bold">深克隆(复制所有引用)</text> <!-- 引用关系连线(原始→对象A) --> <line x1="250" y1="260" x2="180" y2="350" stroke="#1890ff" stroke-width="2" stroke-dasharray="5 3"/> <polygon points="190 345 180 350 190 355" fill="#1890ff"/> <!-- 浅克隆引用连线(浅克隆对象→对象A) --> <line x1="650" y1="260" x2="180" y2="350" stroke="#fa8c16" stroke-width="2" stroke-dasharray="5 3"/> <polygon points="190 345 180 350 190 355" fill="#fa8c16"/> <!-- 深克隆引用连线(深克隆对象→对象B) --> <line x1="900" y1="460" x2="850" y2="550" stroke="#52c41a" stroke-width="2" stroke-dasharray="5 3"/> <polygon points="860 545 850 550 860 555" fill="#52c41a"/> <!-- 图例 --> <rect x="50" y="500" width="20" height="20" fill="#e6f7ff" stroke="#1890ff" stroke-width="2"/> <text x="80" y="520" font-family="Arial" font-size="12">原始对象</text> <rect x="150" y="500" width="20" height="20" fill="#fff2e8" stroke="#fa8c16" stroke-width="2"/> <text x="180" y="520" font-family="Arial" font-size="12">浅克隆对象(共享引用)</text> <rect x="280" y="500" width="20" height="20" fill="#f6ffed" stroke="#52c41a" stroke-width="2"/> <text x="310" y="520" font-family="Arial" font-size="12">深克隆对象(独立引用)</text> <path d="M50 550 Q 70 550 80 560" stroke="#fa8c16" stroke-width="4" stroke-dasharray="10 5" fill="none"/> <polygon points="79 555 80 560 81 555" fill="#fa8c16"/> <text x="100" y="565" font-family="Arial" font-size="12">浅克隆:共享引用</text> <path d="M200 550 Q 220 550 230 560" stroke="#52c41a" stroke-width="4" stroke-dasharray="10 5" fill="none"/> <polygon points="229 555 230 560 231 555" fill="#52c41a"/> <text x="250" y="565" font-family="Arial" font-size="12">深克隆:复制引用</text> </svg>
06-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晴空闲雲

感谢家人们的投喂

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值