news 2026/5/1 6:46:49

SVG - SVG 引入(SVG 概述、SVG 基本使用、SVG 使用 CSS、SVG 使用 JavaScript、SVG 实例实操)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SVG - SVG 引入(SVG 概述、SVG 基本使用、SVG 使用 CSS、SVG 使用 JavaScript、SVG 实例实操)

一、SVG 概述

  • 官方文档:https://developer.mozilla.org/zh-CN/docs/Web/SVG
  1. SVG 全称 Scalable Vector Graphics,即可缩放矢量图形

  2. SVG 基于 XML 标记语言,用于描述二维的矢量图形

  3. SVG 格式提供的是矢量图,这意味着它的图像能够被无限放大而不失真或降低质量,并且可以方便地修改内容,无需图形编辑器


二、SVG 基本使用

1、准备阶段
  • 准备一个宽 400 像素、高 300 像素的灰色边框的空白 SVG 画布
<svgid="mySvg"width="400"height="300"xmlns="http://www.w3.org/2000/svg">...</svg>
svg{border:1px solid #ccc;}
2、基础图形绘制
  1. 矩形:x, y 是左上角坐标;fill 是填充色;stroke 是描边色;stroke-width 是描边粗细
<rectx="50"y="50"width="100"height="60"fill="steelblue"stroke="darkblue"stroke-width="3"/>
  1. 圆形:cx, cy 是圆心坐标;r 是半径;fill 是填充色;opacity 控制透明度(0 ~ 1)
<circlecx="250"cy="100"r="40"fill="coral"opacity="0.8"/>
  1. 直线:x1, y1 是起点坐标,x2, y2 是终点坐标。stroke 是线条颜色,stroke-width 是线条粗细
<linex1="50"y1="200"x2="150"y2="250"stroke="green"stroke-width="5"/>
  1. 多边形:points 属性定义了一系列用空格分隔的 x,y 坐标对,它会自动连接首尾点形成封闭图形
<polygonpoints="300,200 350,250 250,250"fill="lavender"stroke="purple"/>
  1. 路径:d 属性包含绘制命令,上例中,M(移动到)、L(画线到)、Z(闭合路径)
<pathd="M 100,150 L 200,150 L 150,100 Z"fill="lightyellow"stroke="orange"/>

三、SVG 使用 CSS

  • SVG 的 fill、stroke、opacity 等属性可以使用 CSS控制,这有助于统一风格与交互效果
<svgid="mySvg"width="400"height="300"xmlns="http://www.w3.org/2000/svg"><rectclass="shape"x="50"y="50"width="100"height="60"/><circleclass="shape"cx="250"cy="100"r="40"/></svg>
svg{border:1px solid #ccc;}.shape{stroke-width:2;stroke-opacity:0.7;stroke:blue;fill:gray;transition:fill 0.3s ease;}.shape:hover{fill:gold;}

四、SVG 使用 JavaScript

<svgid="mySvg"width="400"height="300"xmlns="http://www.w3.org/2000/svg"><rectid="myRect"class="shape"x="50"y="50"width="100"height="60"/></svg>
svg{border:1px solid #ccc;}.shape{stroke-width:3;stroke-opacity:0.7;stroke:blue;}
constmySvg=document.getElementById("mySvg");constmyRect=document.getElementById("myRect");// 生成一个随机颜色,改变矩形的填充色myRect.addEventListener("click",function(){constrandomColor="#"+Math.floor(Math.random()*16777215).toString(16);console.log(randomColor);this.setAttribute("fill",randomColor);});// 在 SVG 内部点击时添加圆形mySvg.addEventListener("click",function(event){constpoint=mySvg.createSVGPoint();point.x=event.clientX;point.y=event.clientY;constsvgPoint=point.matrixTransform(mySvg.getScreenCTM().inverse());constnewCircle=document.createElementNS("http://www.w3.org/2000/svg","circle");newCircle.setAttribute("cx",svgPoint.x);newCircle.setAttribute("cy",svgPoint.y);newCircle.setAttribute("r","15");newCircle.setAttribute("fill","lightcoral");newCircle.classList.add("shape");mySvg.appendChild(newCircle);});

五、SVG 实例实操(房屋绘制)

<svgwidth="400"height="400"viewBox="0 0 400 400"><!-- 房屋主体 --><rectclass="house-body"x="100"y="200"width="200"height="150"rx="5"/><!-- 屋顶 --><polygonclass="roof"points="70,200 330,200 200,100"/><!-- 烟囱 --><rectclass="chimney"x="260"y="120"width="30"height="60"/><rectclass="chimney"x="255"y="115"width="40"height="10"/><!-- 烟囱烟 --><pathd="M275,90 Q280,70 290,65 Q300,60 310,55"fill="none"stroke="#aaa"stroke-width="3"stroke-linecap="round"/><pathd="M280,80 Q285,60 295,55 Q305,50 315,45"fill="none"stroke="#aaa"stroke-width="3"stroke-linecap="round"/><!-- 窗户 --><rectclass="window"x="120"y="220"width="40"height="40"rx="3"/><rectclass="window"x="240"y="220"width="40"height="40"rx="3"/><!-- 窗户十字架 --><linex1="140"y1="220"x2="140"y2="260"stroke="#3366cc"stroke-width="1"/><linex1="120"y1="240"x2="160"y2="240"stroke="#3366cc"stroke-width="1"/><linex1="260"y1="220"x2="260"y2="260"stroke="#3366cc"stroke-width="1"/><linex1="240"y1="240"x2="280"y2="240"stroke="#3366cc"stroke-width="1"/><!-- 门 --><rectclass="door"x="180"y="280"width="40"height="70"rx="3"/><!-- 门把手 --><circlecx="210"cy="315"r="3"fill="gold"/><!-- 台阶 --><rectx="170"y="350"width="60"height="10"fill="#888"rx="2"/><!-- 文字 --><textx="200"y="390"text-anchor="middle"font-size="14"fill="#666">SVG 房屋绘制</text></svg><divstyle="text-align:center;margin:20px"><buttononclick="addSun()">添加太阳</button><buttononclick="changeColor()">改变颜色</button><buttononclick="reset()">重置</button></div>
svg{border:1px solid #ddd;background-color:#f8f9fa;display:block;margin:20px auto;}.house-body{fill:#ffcc99;stroke:#cc9966;stroke-width:2;}.roof{fill:#cc3333;stroke:#993333;stroke-width:2;}.window{fill:#99ccff;stroke:#3366cc;stroke-width:1;}.window:hover{fill:#66aaff;}.door{fill:#996633;stroke:#663300;stroke-width:2;}.door:hover{fill:#cc9966;}.chimney{fill:#cccccc;stroke:#999999;stroke-width:2;}
functionaddSun(){constsvg=document.querySelector("svg");constsun=document.createElementNS("http://www.w3.org/2000/svg","circle");sun.setAttribute("cx","350");sun.setAttribute("cy","50");sun.setAttribute("r","30");sun.setAttribute("fill","gold");sun.setAttribute("opacity","0.8");svg.appendChild(sun);// 阳光光线for(leti=0;i<12;i++){constray=document.createElementNS("http://www.w3.org/2000/svg","line");constangle=(i*30*Math.PI)/180;constx1=350+Math.cos(angle)*30;consty1=50+Math.sin(angle)*30;constx2=350+Math.cos(angle)*45;consty2=50+Math.sin(angle)*45;ray.setAttribute("x1",x1);ray.setAttribute("y1",y1);ray.setAttribute("x2",x2);ray.setAttribute("y2",y2);ray.setAttribute("stroke","gold");ray.setAttribute("stroke-width","3");svg.appendChild(ray);}}functionchangeColor(){constcolors=["#ff9999","#99ff99","#9999ff","#ffff99","#ff99ff"];consthouseBody=document.querySelector(".house-body");constrandomColor=colors[Math.floor(Math.random()*colors.length)];houseBody.style.fill=randomColor;}functionreset(){location.reload();}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/26 17:42:49

英文文献在哪里找:实用检索平台与高效获取方法指南

生成式人工智能的浪潮正引发各领域的颠覆性变革&#xff0c;在学术研究这一知识生产的前沿阵地&#xff0c;其影响尤为显著。文献检索作为科研工作的基石&#xff0c;在AI技术的赋能下各大学术数据库已实现智能化升级。小编特别策划"AI科研导航"系列专题&#xff0c;…

作者头像 李华
网站建设 2026/4/26 20:44:38

GPT-SoVITS训练失败常见原因及解决方案

GPT-SoVITS训练失败常见原因及解决方案 在个性化语音合成的浪潮中&#xff0c;GPT-SoVITS 凭借“一分钟克隆音色”的能力迅速走红。它让普通用户也能用极少量语音数据生成高度还原自己声音的语音&#xff0c;在虚拟主播、有声书配音、无障碍辅助等领域展现出巨大潜力。然而&am…

作者头像 李华
网站建设 2026/4/25 15:48:12

智普AutoGLM究竟强在哪?:3大核心技术解析颠覆你的认知

第一章&#xff1a;智普Open-AutoGLM 沉思在人工智能与自动化深度融合的当下&#xff0c;智普推出的 Open-AutoGLM 项目为大语言模型的自主任务执行提供了全新范式。该项目结合了 GLM 大模型的强大语义理解能力与自动化决策框架&#xff0c;使得机器能够在复杂环境中感知、推理…

作者头像 李华
网站建设 2026/5/1 4:42:16

【Open-AutoGLM唤醒全攻略】:5步实现模型高效激活与部署

第一章&#xff1a;Open-AutoGLM唤醒全攻略导论Open-AutoGLM 是一个面向自动化自然语言处理任务的开源框架&#xff0c;旨在通过轻量级接口实现大语言模型的快速部署与调用。该框架支持多种推理模式&#xff0c;包括本地加载、API 调用以及边缘设备适配&#xff0c;适用于从开发…

作者头像 李华
网站建设 2026/4/29 23:50:57

质谱AI分析新纪元开启,Open-AutoGLM私有化部署仅需这7步

第一章&#xff1a;质谱AI分析新纪元的技术背景近年来&#xff0c;质谱技术在生物医学、环境监测和药物研发等领域取得了突破性进展。随着高通量数据的爆发式增长&#xff0c;传统数据分析方法已难以应对复杂、高维的质谱信号处理需求。在此背景下&#xff0c;人工智能&#xf…

作者头像 李华
网站建设 2026/4/30 20:02:22

Open-AutoGLM apk使用全攻略(从安装到实战部署)

第一章&#xff1a;Open-AutoGLM apk使用全攻略概述Open-AutoGLM 是一款基于开源大语言模型的本地化推理应用&#xff0c;通过其 APK 安装包可在 Android 设备上实现离线自然语言处理与代码生成能力。该应用融合了 GLM 架构的高效推理特性&#xff0c;支持多场景下的文本补全、…

作者头像 李华