利用svg实现画线

论坛 期权论坛 脚本     
匿名技术用户   2021-1-4 02:51   38   0

概述:在svg中画一矩形用来当作用户画图区域,利用四个Test元素(选择起始点、选择终止点、画线、清空)的click事件依次实现相应的功能。利用Group元素实现画图节点的填充容器。
(一)svg文件说明
1,画rect代码

None.gif<rect x="0.137" y="-4.646" onclick="DrawSvgCircle(evt)" fill="#ADFF5F" stroke="#000000" stroke-width="0.7078" stroke-linecap="round" stroke-linejoin="round" stroke-miterlimit="500" width="594.566" height="247.729"/>

2,画text代码

None.gif<text transform="matrix(1 0 0 1 61.1379 266.7664)" font-family="'SimSun'" font-size="21" onclick="selectedStart()">选择起始点</text>
None.gif
<text transform="matrix(1 0 0 1 212.1379 267.7664)" font-family="'SimSun'" font-size="21" onclick="selectedEnd()">选择终止点</text>
None.gif
<text transform="matrix(1 0 0 1 353.1375 266.7664)" font-family="'SimSun'" font-size="21" onclick="drawline()">画线</text>
None.gif
<text transform="matrix(1 0 0 1 429.1375 265.7664)" font-family="'SimSun'" font-size="21" onclick="cancle()">清空</text>


3,group节点代码

None.gif<g id="g5">
None.gif
</g>


4,引用外部lib1.js文件
<script language="JavaScript" xlink:href="lib1.js"></script>
(二)脚本文件
1,圆类

ExpandedBlockStart.gifContractedBlock.gif/**//****************************************************************************
InBlock.gif画园类
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif****************************************************************************
*/

None.gif
function SvgCircle(x,y,r,color,width)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
this.x=x;
InBlock.gif
this.y=y;
InBlock.gif
this.r=r;
InBlock.gif
this.color=color;
InBlock.gif
this.width=width;
InBlock.gif
this.DrawCircle=DrawCircle;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
function DrawCircle()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
var circle=document.createElement("circle");
InBlock.gif circle.setAttribute(
"cx",this.x);
InBlock.gif circle.setAttribute(
"cy", this.y);
InBlock.gif circle.setAttribute(
"r",this.r);
InBlock.gif circle.getStyle().setProperty(
"stroke",this.color);
InBlock.gif circle.getStyle().setProperty(
"stroke-width",this.width);
InBlock.gif
return circle;
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**//****************************************************************************
InBlock.gif画园类
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif****************************************************************************
*/

2,线类

ExpandedBlockStart.gifContractedBlock.gif/**//****************************************************************************
InBlock.gif画线类
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif****************************************************************************
*/

None.gif
function SvgLine(x1,y1,x2,y2,linecolor,linewidth)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
this.x1=x1;
InBlock.gif
this.y1=y1;
InBlock.gif
this.x2=x2;
InBlock.gif
this.y2=y2;
InBlock.gif
this.linecolor=linecolor;
InBlock.gif
this.linewidth=linewidth;
InBlock.gif
this.DrawLine=DrawLine;
ExpandedBlockEnd.gif}

None.gif
function DrawLine()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
// var target = get_target(evt);
InBlock.gif//
var svgdoc = target.getOwnerDocument();
InBlock.gif
var line=document.createElement("line");
InBlock.gif line.setAttribute(
"x1",this.x1);
InBlock.gif line.setAttribute(
"y1", this.y1);
InBlock.gif line.setAttribute(
"x2", this.x2);
InBlock.gif line.setAttribute(
"y2", this.y2);
InBlock.gif line.getStyle().setProperty(
"stroke",this.linecolor);
InBlock.gif line.getStyle().setProperty(
"fill",this.linecolor);
InBlock.gif line.getStyle().setProperty(
"stroke-width",this.linewidth);
InBlock.gif
return line;
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**//****************************************************************************
InBlock.gif画线类
InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif****************************************************************************
*/

3,事件

None.gifvar start=false;
None.gif
var end=false;
None.gif
var sx;
None.gif
var sy;
None.gif
var endx;
None.gif
var endy;
ExpandedBlockStart.gifContractedBlock.gif
/**//****************************************************************************
InBlock.gif事件
ExpandedBlockEnd.gif****************************************************************************
*/

None.gif
function selectedStart()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif start
=true;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
function selectedEnd()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif end
=true;
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
function DrawSvgCircle(evt)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
if(start)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
InBlock.gif sx
= evt.getClientX();
InBlock.gif sy
= evt.getClientY();
InBlock.gif
var c1=new SvgCircle(sx,sy,"2","red","2");
InBlock.gif
var circle=c1.DrawCircle();
InBlock.gif
var group=document.getElementById("g5");
InBlock.gif group.appendChild(circle);
InBlock.gif
// document.getDocumentElement().appendChild(group);
InBlock.gif

InBlock.gif
InBlock.gif start
=false;
ExpandedSubBlockEnd.gif }

InBlock.gif
if(end)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif endx
= evt.getClientX();
InBlock.gif endy
= evt.getClientY();
InBlock.gif
var c1=new SvgCircle(endx,endy,"2","red","2");
InBlock.gif
var circle=c1.DrawCircle();
InBlock.gif
var group=document.getElementById("g5");
InBlock.gif group.appendChild(circle);
InBlock.gif
//document.getDocumentElement().appendChild(group);
InBlock.gif
end=false;
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
function drawline()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
var l=new SvgLine(sx,sy,endx,endy,"red","2");
InBlock.gif
var line=l.DrawLine();
InBlock.gif
var group=document.getElementById("g5");
InBlock.gif group.appendChild(line);
InBlock.gif
// document.getDocumentElement().appendChild(group);
InBlock.gif

ExpandedBlockEnd.gif}

None.gif
function cancle()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
var group=document.getElementById("g5");
InBlock.gif
for(var i=0;i<group.childNodes.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif group.removeChild(group.childNodes.item(i));
ExpandedSubBlockEnd.gif }

InBlock.gif
//var lines=group.getElementsByTagName("line");
InBlock.gif//
var circles=group.getElementsByTagName("circle");
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//*for(var i=0;i<lines.length;i++)
InBlock.gif {
InBlock.gif group.removeChild(lines.item(i));
InBlock.gif }
InBlock.gif for(var i=0;i<circles.length;i++)
InBlock.gif {
InBlock.gif group.removeChild(circles.item(i));
ExpandedSubBlockEnd.gif }
*/

ExpandedBlockEnd.gif}

总结:
1,不足:没有实现在text元素上鼠标变形;在刷屏时不能全部清空所画对象,谁能够实现请指教
2,继续学习DOM
全部源码

posted on 2007-04-16 17:26 老狼 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/MuYi/archive/2007/04/16/715610.html

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP