源代码,可以copy直接运行
JavaScript部分
首页 web前端 js教程 基于SVG的web页面图形绘制API介绍及编程演示_javascript技巧

基于SVG的web页面图形绘制API介绍及编程演示_javascript技巧

May 16, 2016 pm 05:30 PM
svg 图形绘制

一:什么是SVG
SVG是1999由W3C发布的2D图形描述语言,纯基于XML格式的标记语言,SVG的
全称是可扩展的矢量图形跟传统的Raster方式的图形(JPG, PNG, GIF等)有很大的差
别。SVG是2D图形开发平台,包括两个部分,一个是基于XML语言的数据描述,另
外一部分是可编程的API,其关键特性支持图形,文本,梯度填充,画笔风格,图形
特效滤镜如高斯模糊,会在稍后的代码中演示。同时还支持各种鼠标事件与DOM部
分API。几乎所有的主流浏览器都支持SVG图形格式的现实与绘制,IE9+以上也开始
支持SVG,在低版本的IE中需要插件支持。
更多了解SVG访问这里:http://www.w3.org/Graphics/SVG/About.html

二:JavaScript中SVG API编程演示
创建与获取SVG对象
复制代码 代码如下:

// create svg object
var mySvg = document.createElementNS("http://www.w3.org/2000/svg","svg");
mySvg.setAttribute("version","1.2");// IE9+ support SVG 1.1 version
mySvg.setAttribute("baseProfile","tiny");
container.appendChild(mySvg);

在SVG中创建一个矩形图形:
复制代码 代码如下:

var c1 = document.createElementNS("http://www.w3.org/2000/svg","rect");
c1.setAttribute("x","20");
c1.setAttribute("y","20");
c1.setAttribute("width","150");
c1.setAttribute("height","150");
c1.setAttribute("fill","rgb(0,0,255)");
c1.setAttribute("stroke","rgb(0,0,0)");
c1.setAttribute("stroke-width","4");
mySvg.appendChild(c1);

在SVG中实现文本绘制:
复制代码 代码如下:

// SVG draw text
var stext = document.createElementNS("http://www.w3.org/2000/svg","text");
stext.setAttribute("x","700");
stext.setAttribute("y","100");
stext.setAttribute("font-size","18px");
stext.setAttribute("fill","#FF0000");
var textString = document.createTextNode("Hello SVG");
stext.appendChild(textString);
mySvg.appendChild(stext);

在SVG对象上实现鼠标点击事件处理与MouseUp事件处理:
复制代码 代码如下:

// mouse event handling
c1.addEventListener("click",changeColor,false);
c2.addEventListener("mouseup", changeColor,false);

通过SVG 图形滤镜实现高斯模糊:
复制代码 代码如下:


Original image




运行效果:
基于SVG的web页面图形绘制API介绍及编程演示_javascript技巧 
源代码,可以copy直接运行
JavaScript部分
复制代码 代码如下:

window.onload = function() {
// get DIV
var container = document.getElementById("svgContainer");
// create svg object
var mySvg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
mySvg.setAttribute("version", "1.2");// IE9+ support SVG 1.1 version
mySvg.setAttribute("baseProfile", "tiny");
container.appendChild(mySvg);

// create svg shape - rectangle
var c1 = document.createElementNS("http://www.w3.org/2000/svg", "rect");
c1.setAttribute("x", "20");
c1.setAttribute("y", "20");
c1.setAttribute("width", "150");
c1.setAttribute("height", "150");
c1.setAttribute("fill", "rgb(0,0,255)");
c1.setAttribute("stroke", "rgb(0,0,0)");
c1.setAttribute("stroke-width", "4");
mySvg.appendChild(c1);

// create svg shape - circle
var c2 = document.createElementNS("http://www.w3.org/2000/svg", "circle");
c2.setAttribute("cx", "250");
c2.setAttribute("cy", "100");
c2.setAttribute("r", "60");
c2.setAttribute("fill", "#996699");
c2.setAttribute("stroke", "#AA99FF");
c2.setAttribute("stroke-width", "7");
mySvg.appendChild(c2);

// create svg shape - ellipse
var c3 = document.createElementNS("http://www.w3.org/2000/svg", "ellipse");
c3.setAttribute("cx", "450");
c3.setAttribute("cy", "100");
c3.setAttribute("rx", "100");
c3.setAttribute("ry", "50");
c3.setAttribute("fill", "#FF0000");
c3.setAttribute("stroke", "purple");
c3.setAttribute("stroke-width", "3");
mySvg.appendChild(c3);

// create svg shape - draw lines
for(var i=0; i{
var sline = document.createElementNS("http://www.w3.org/2000/svg", "line");
var x1 = 580 + i*10;
console.log(x1);

sline.setAttribute("x1", x1.toString());
sline.setAttribute("y1", "10");
sline.setAttribute("x2", x1.toString());
sline.setAttribute("y2", "180");
sline.setAttribute("stroke", "rgb(0,255,0)");
sline.setAttribute("stroke-width", "2");
mySvg.appendChild(sline);
}

// SVG draw text
var stext = document.createElementNS("http://www.w3.org/2000/svg", "text");
stext.setAttribute("x", "700");
stext.setAttribute("y", "100");
stext.setAttribute("font-size", "18px");
stext.setAttribute("fill", "#FF0000");
var textString = document.createTextNode("Hello SVG");
stext.appendChild(textString);
mySvg.appendChild(stext);

// mouse event handling
c1.addEventListener("click", changeColor, false);
c2.addEventListener("mouseup", changeColor, false);
};
function changeColor(evt) {
var target = evt.target;
target.setAttributeNS(null, "fill", "green");
}

HTML部分:
复制代码 代码如下:



Gloomyfish SVG Demo





Original image





本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

聊聊如何利用 SVG 实现图片马赛克效果 聊聊如何利用 SVG 实现图片马赛克效果 Sep 01, 2022 am 11:05 AM

不借助 Javascript,如何利用 SVG 实现图片马赛克效果?下面本篇文章就来带大家详细了解一下,希望对大家有所帮助!

svg怎么转jpg格式 svg怎么转jpg格式 Nov 24, 2023 am 09:50 AM

svg可以通过使用图像处理软件、使用在线转换工具和使用Python图像处理库的方法来转jpg格式。详细介绍:1、图像处理软件包括Adobe Illustrator、Inkscape和GIMP;2、在线转换工具包括CloudConvert、Zamzar、Online Convert等;3、Python图像处理库等等。

深入浅析vue3 vite中怎么使用svg图标 深入浅析vue3 vite中怎么使用svg图标 Apr 28, 2022 am 10:48 AM

svg图片在项目中使用的非常广泛,下面本篇文章带大家介绍一下如何在vue3 vite 中使用svg图标,希望对大家有所帮助!

VUE3入门教程:使用Vue.js插件玩转SVG VUE3入门教程:使用Vue.js插件玩转SVG Jun 16, 2023 am 09:48 AM

随着现代Web前端开发的不断发展,越来越多的技术被广泛应用于实际开发中。其中,Vue.js是目前最为流行的JavaScript框架之一,它基于MVVM模式,提供了丰富的API和组件库,使得开发响应式、可复用、高效的Web应用变得更加容易。而目前最新的Vue.js3版本相较于旧版,又有着更好的性能和更丰富的特性,引起了广泛的关注和研究。本文将会为大家介绍一种

图形绘制利器——matplotlib安装教程 图形绘制利器——matplotlib安装教程 Jan 09, 2024 pm 05:22 PM

图形绘制利器——matplotlib安装教程一、简介matplotlib是一个功能强大的Python绘图库,用于生成各种类型的图形,包括折线图、散点图、柱状图、饼图等。它的安装非常简单方便,本文将介绍如何安装matplotlib并给出具体的代码示例。二、安装matplotlib安装Python首先,确保你的电脑已经安装了Python。可以在Python官网(

详解用SVG给 favicon 添加标识 详解用SVG给 favicon 添加标识 Sep 07, 2022 am 10:30 AM

怎么使用SVG给 favicon 添加标识?下面本篇文章给大家介绍一下使用 SVG 生成带标识的 favicon的方法,希望对大家有所帮助!

vue3 vite2中怎么使用svg方法 vue3 vite2中怎么使用svg方法 May 11, 2023 pm 05:55 PM

一、安装vite-plugin-svg-icons此处还需要安装下fast-glob相关依赖,不然vite运行npmrundev时会报Cannotfindmodule'fast-glob’的错误npmifast-glob@3.x-Dnpmivite-plugin-svg-icons@2.x-D二、在src/components/svgIcon下新建组件index.vueimport{computed}from'vue';cons

vue3 vue-cli4中怎么使用svg vue3 vue-cli4中怎么使用svg May 11, 2023 pm 05:58 PM

一、安装svg-sprite-loadernpminstallsvg-sprite-loader--save-dev二、在src/components/svgIcon下新建组件index.vueimport{computed}from"@vue/reactivity";exportdefault{name:"baseSvgIcon",props:{iconClass:{type:String},className:{type:String},},setup

See all articles