Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Javascript更改svg多边形属性_Javascript_Loops_Svg_Setattribute - Fatal编程技术网

通过Javascript更改svg多边形属性

通过Javascript更改svg多边形属性,javascript,loops,svg,setattribute,Javascript,Loops,Svg,Setattribute,我正在尝试缩放和转换svg多边形元素,具体取决于浏览器窗口的大小。 计算我要缩放和平移多少并不是问题,但更改多边形对我来说很重要 我希望你能帮我 我已将问题分解,以便计算多边形的数量。这没问题: function countnumberofPolygons() { numberofPolygons = document.getElementsByTagName("polygon").length; return numberofPolygons; } 以及一个函数,该函数创建一

我正在尝试缩放和转换svg多边形元素,具体取决于浏览器窗口的大小。 计算我要缩放和平移多少并不是问题,但更改多边形对我来说很重要

我希望你能帮我

我已将问题分解,以便计算多边形的数量。这没问题:

function countnumberofPolygons() {
    numberofPolygons = document.getElementsByTagName("polygon").length;
    return numberofPolygons;
}
以及一个函数,该函数创建一个字符串,用于确定多边形的transform=属性-transform属性可以是-transform=translate800,00 scale1.2

function createsvgtransformattribute(){
    transformattribute = '"translate('+ translateAmount +',0) scale(' + scaleAmount + ')"';
}
但是,在它们之间循环并设置它们的属性似乎不起作用。我已经把它全部分解并重新构建了起来——但最终以这种方式结束——这是错误的,可能以某种简单的方式

function changeattributes(numberofPolygons, transformattribute){
    for (var q=0;q< numberofPolygons;q++){
        document.getElementsByTagname("polygon")[q].setAttribute("transform", transformattribute);
    }
}

但是,即使手动插入字符串transformattribute的值,它也不起作用。您能帮忙吗?

当您使用setAttribute设置变换属性时,您不会将其放在双引号中。所以你需要的是

transformattribute = 'translate('+ translateAmount +',0) scale(' + scaleAmount + ')';

在html中,我使用documentreadjquery函数和onresize调用svgscale。 脚本中有一些奇怪的地方——比如左侧轻推——但希望它能对其他人起作用

我已经将图像标签中的图像转换为一个div,该div将图像作为背景图像。然后将图像映射拉入svg。然后,该脚本使用svg的transform属性相应地缩放和平移图像地图的多边形

var winWidth;
var winHeight;

var MainImageHeight;
var MainImageWidth;

var HeightRatio;                
var imageWidth;

var leftoffset;
var ImgVsOriginal;

var offsetnudge;
var offsetnudgescaled;
var los;
var translateAmount;
var scaleAmount;

var numberofNodes;
var numberofPolygons;
var polygonArray;
var transformattribute;


function setVariables(){

                                //Browser window widths and heights
                winWidth = window.innerWidth;
                winHeight = window.innerHeight;

                                                //Widths and heights of the element with the MainImage id
                MainImageHeight =   document.getElementById('MainImage').offsetHeight;
                MainImageWidth =    document.getElementById('MainImage').offsetWidth;

                                                //Establishes the ratio between the height of the element and the background image it displays,
                                                //which has a height of 920px
                                                //The MainImage resizes the background image so the ratio is needed to scale the svg/imagemap
                HeightRatio = MainImageHeight/920;

                                                //This establishes the width of the background image as presented - the background image file is 1400px
                imageWidth = HeightRatio*1400;

                                                //The Background image is centered and covers the screen. So there is space either side of the background image
                                                //This space is worked out here, and halved to work out the left-hand side portion of the space
                leftoffset = (winWidth-imageWidth)/2;

                                                //The original imagemap was created based on an image displayed as 960px by 653px. This calculates the ratio between them.
                ImgVsOriginal = MainImageHeight/653;

                                                //The original image was based on images with a small border each side. This is a hard-adjustment for this.
                offsetnudge = 30;

                                                //Scales the offset for this border based on the background image size vs the original
                offsetnudgescaled = offsetnudge*ImgVsOriginal;

                                                //Creates an easy to type variable based on 
                //los = leftoffset + offsetnudgescaled;
                translateAmount = leftoffset + offsetnudge;
                scaleAmount = ImgVsOriginal;
                                                //Creates variable for idname
                 var idname;      
                }






function createsvgtransformattribute(){
                transformattribute = 'translate('+ translateAmount +',0) scale(' + scaleAmount + ')';
                return transformattribute;
}


function countchildNodes(){
                numberofNodes = document.getElementById("svgstuff").childNodes.length;

                }

function printnumberofnodes(){
                document.write('<span>Number of Nodes:' + numberofNodes + '</span>');
}

function countnumberofPolygons(){
                numberofPolygons = document.getElementsByTagName("polygon").length;
                return numberofPolygons;
                }

function getpolygonArray(){
                polygonArray = document.getElementsByTagName("polygon");
}

function printnumberofPolygons(){
                document.write('<span>Number of Polygons:' + numberofPolygons + '</span>');
}







function changeattributes(){
                document.getElementById('test1').innerHTML='changed';
        for(q=0; q<polygonArray.length; q++){
        //document.getElementsByTagName('polygon')[q].setAttribute("class", "blue");
        document.getElementsByTagName('polygon')[q].setAttribute("transform", transformattribute);
        }
}




function svgscale(){
                setVariables();
                getpolygonArray();
                createsvgtransformattribute(translateAmount, scaleAmount);
                changeattributes();
        } 

非常感谢你!我会试试的!非常感谢你。我以前确实试过这个,但遗憾的是它不起作用,所以我认为我还做了别的错事。我需要在这里使用“this”关键字吗?您好,我最终得到了它-您是对的,它不需要双引号。这是我的全部剧本。。。。
<div id="MainImage">
    <svg onresize="svgscale()" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
    style="position:absolute;"
    width="2000" height="2000"
    pointer-events="visible">       
        <a xlink:href="1.htm" xlink:title="1">');   
            <polygon id="p1" class="" points="736,378 680,363 680,233 736,209 736,378" transform="" fill="" >
            </polygon>
        </a>
        <a xlink:href="2.htm" xlink:title="2">
            <polygon id="p2"class="area" points="839,161,742,204,739,513,831,587,839,161" transform="" fill="">

            </polygon>
        </a>
        <a xlink:href="3.htm" xlink:title="3">');   
            <polygon id="p3" class="area" points="521,286,521,296,557,297,555,287,521,286" transform="" fill="" >

            </polygon>
        </a>
        <a xlink:href="4.htm" xlink:title="4">');   
            <polygon id="p4" class="area" points="562,218,562,240,657,242,657,219,562,218" transform="" fill="" >

            </polygon>
        </a>
        <a xlink:href="5.htm" xlink:title="5">');   
            <polygon id="p5" class="area" points="952,273,909,275,905,276,902,347,858,344,846,351,845,356,855,361,845,542,849,546,849,572,846,573,845,575,841,652,954,652,952,273" transform="" fill="" >
            </polygon>
        </a>                    

    </svg>

</div>
#MainImage {background-image: url('pix/file.jpg'); background-size: auto 100%;}