简体   繁体   English

使用Javascript将SVG路径点串在一起

[英]Stringing together SVG path points using Javascript

I'm able to use the following JavaScript to create an SVG hexagon using some points generated from Inkscape: 我可以使用以下JavaScript通过Inkscape生成的一些点来创建SVG六角形:

var createHex = function(fill) {
    var NS = 'http://www.w3.org/2000/svg';
    var hexagon = document.createElementNS(NS, 'path');
    hexagon.setAttribute('d', 'm 311.5022,270.79644 62.8676,108.88986 -62.86759,108.88987 -125.73518,1e-5 -62.8676,-108.88986 62.86759,-108.88987 z');
    hexagon.setAttribute('fill', fill);
    hexagon.setAttribute('style', 'opacity:0.125');
    return hexagon;
}

Yet while attempting to have some Javascript math and string concatenation handle the point generation in the same fashion, the function fails to return anything: 但是,当尝试以某种方式用一些Javascript数学和字符串连接处理点生成时,该函数无法返回任何内容:

var test = function(fill) {
    var NS = 'http://www.w3.org/2000/svg';
    var test = document.createElementNS(NS, 'path');
    var pt1x = 1 * 300;
    var pt1y = 0 * 300;
    var pt2x = (1/2) * 300;
    var pt2y = (sqrt(3)/2) * 300;
    var pt3x = (-1/2) * 300;
    var pt3y = (sqrt(3)/2) * 300;
    var pt4x = -1 * 300;
    var pt4y = 0 * 300;
    var pt5x = (-1/2) * 300;
    var pt5y = (-sqrt(3)/2) * 300;
    var pt6x = (1/2) * 300;
    var pt6y = (-sqrt(3)/2) * 300;
    test.setAttribute('d', 'm ' + pt1x + ',' + pt1y + ' ' + pt2x + ',' + pt2y + ' ' + pt3x + ',' + pt3y + ' ' + pt4x + ',' + pt4y + ' ' + pt5x + ',' + pt5y + ' ' + pt6x + ',' + pt6y + ' z');
    test.setAttribute('fill', fill);
    test.setAttribute('style', 'opacity:0.125');
    return test;
}

I'm new to both JavaScript and SVG; 我是JavaScript和SVG的新手。 anybody able to tell me what I'm missing? 有人能够告诉我我所缺少的吗?

Check your console log for errors. 检查控制台日志中是否有错误。 sqrt() is not a global JavaScript function. sqrt()不是全局JavaScript函数。 You want Math.sqrt() . 您需要Math.sqrt()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM