html5+d3 svg 线条、图形颜色渐变动画
2016-01-05 11:40
645 查看
代码如下:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>线条、图形颜色渐变动画</title> <script src="js/jquery.min.js"></script> <script src="js/d3.js"></script> <script> $(document).ready(function(){ changeCorlor(); }); function changeCorlor(){//控制circle颜色渐变属性 var svg = d3.select("body").select("svg"); //svg.select('#g1').attr("stop-color","yellow"); svg.select('#svg_4').append("animate").attr("attributeName","fill")//因为是填充色,所以用fill属性;如果渐变的是线条的颜色,就改成stroke属性 .attr('attributeType','XML') .attr('from','green') .attr('to','red') .attr('dur','1s') .attr('fill','freeze'); } function changeColor2(){ //利用d3.js获取svg元素 var svg = d3.select("body").select("svg"); //svg.select('#g1').attr("stop-color","yellow"); //svg.select('#svg_4').remove("animate"); var animate = svg.select('#svg_4').append("animate"); animate.attr("attributeName","fill") .attr('attributeType','XML') .attr('from','green') .attr('to','yellow') .attr('dur','5s') //秒数太小时看不出渐变的效果,不知道啥原因???而且渐变就第一次点击有效果,不知道为啥??? .attr('fill','freeze'); } </script> </head> <body> <input type="button" value="changeColor" onClick="changeColor2()"> <svg width="1920" height="5612" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient x1="0%" y1="0%" x2="100%" y2="100%" id="lsvg_1"> <stop id="g1" offset="0%" stop-color="green"> <animate attributeName="stop-color" values="green; yellow;" dur="1s" fill="freeze"/> </stop> <stop id="g1" offset="90%" stop-color="green"> <animate attributeName="stop-color" values="green; yellow;" dur="1s" fill="freeze"/> </stop> <stop id="g2" offset="100%" stop-color="green"> <animate attributeName="stop-color" values="yellow;" dur="1s" fill="freeze"/> </stop> </linearGradient> </defs> <g> <path id="svg_3" d="m100,102c0,0 60,49 80,46c20,-3 69,44 97,43c28,-1 117,-4 117,-4" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="5" stroke="url(#lsvg_1)" fill="none"> </path> <!-- 线条颜色的渐变 --> <animate attributeName="stroke" attributeType="XML" from="purple" to="red" begin="0s" dur="3s" fill="freeze"></animate> </path> </g> <circle id="svg_4" cx="110" cy="320" r="100" stroke=""> <!-- 图形颜色渐变,这个效果同js中的效果是一样的 --> <!-- <animate attributeName="fill" attributeType="XML" from="yellow" to="red" begin="0s" dur="3s" fill="freeze"></animate> --> </circle> <!-- 任意不规则的封闭图形,填充色、外线条颜色都可以渐变 --> <path d="m260,257.87201c71.28699,0.961 168.74301,-7.69301 170.548,0c1.80399,7.69299 30.67999,146.16599 0.90201,146.16599c-29.77802,0 -162.42599,0.96201 -162.42599,0.96201c0,0 -9.02402,-147.12799 -9.02402,-147.12799z" stroke="lightgreen" id="svg_1" stroke-linecap="null" stroke-linejoin="null" stroke-dasharray="null" stroke-width="5" fill="lightgreen"> <animate attributeName="fill" attributeType="XML" from="lightgreen" to="red" begin="0s" dur="3s" fill="freeze"></animate> <animate attributeName="stroke" attributeType="XML" from="lightgreen" to="yellow" begin="0s" dur="3s" fill="freeze"></animate> </path> </svg> </body> </html>
相关文章推荐
- HTML5中在客户端验证文件上传的大小
- Canvas 在高清屏下绘制图片变模糊的解决方法
- Gifski:一个跨平台的高质量 GIF 编码器
- 模仿动画的放大缩小容器
- jQuery 1.9.1源码分析系列(十五)动画处理之缓动动画核心Tween
- 原生js结合html5制作小飞龙的简易跳球
- Android中ViewFlipper的使用及设置动画效果实例详解
- jQuery实现美观的多级动画效果菜单代码
- php判断GIF图片是否为动画的方法
- jQuery实现动画效果circle实例
- 三个不常见的 HTML5 实用新特性简介
- 低版本IE正常运行HTML5+CSS3网站的3种解决方案
- js+HTML5实现canvas多种颜色渐变效果的方法
- 浅析JavaScript动画
- js排序动画模拟-插入排序
- javascript+HTML5的Canvas实现Lab单车动画效果
- javascript+html5实现绘制圆环的方法
- 基于javascript实现漂亮的页面过渡动画效果附源码下载