draw2d 目前没有提供直接对Figure 设置渐变效果的API.
但是raphael 有提供, 这样的话基本上在draw2d实现渐变成为可能。
<!--Add by oscar999--> </HTML> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Linear Gradient</title> <script src="http://raphaeljs.com/raphael.js" type="text/javascript" charset="utf-8"></script> </head> <body> <script type="text/javascript" charset="utf-8"> var paper = Raphael(10, 10, 800, 600); var circle = paper.circle(150, 150, 150); circle.attr({ "fill": "90-#f00:5-#00f:95", "fill-opacity": 0.5 }); </script> </body> </html>先看一下 raphael 官方API对于fill 设置渐变色的说明:
Linear gradient format: “‹angle›-‹colour›[-‹colour›[:‹offset›]]*-‹colour›
”, example: “90-#fff-#000
” – 90°gradient from white to black or “0-#fff-#f00:20-#000
” – 0° gradient from white via red (at 20%) to black.
radial gradient: “r[(‹fx›, ‹fy›)]‹colour›[-‹colour›[:‹offset›]]*-‹colour›
”, example: “r#fff-#000
” –gradient from white to black or “r(0.25, 0.75)#fff-#000
” – gradient from white to black with focus pointat 0.25, 0.75. Focus point coordinates are in 0..1 range. Radial gradients can only be applied to circles and ellipses.
"fill": "90-#f00:5-#00f:95"90 设置的是渐变的方向的角度, 90 就是垂直。
<?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"> <defs> <linearGradient id="orange_red" x1="0%" y1="0%" x2="100%" y2="0%"> <stop offset="0%" style="stop-color:rgb(255,255,0); stop-opacity:1"/> <stop offset="100%" style="stop-color:rgb(255,0,0); stop-opacity:1"/> </linearGradient> </defs> <ellipse cx="200" cy="190" rx="85" ry="55" style="fill:url(#orange_red)"/> </svg>
attributes.fill = "90-#fff:5-#000:95";渐变颜色根据需要配置,也可以动态配置