简体   繁体   中英

SVG drawing didn't work

I made small script to drawing SVG polygons, here is example:

http://codepen.io/anon/pen/dGEQwV

var refreshDuration = 10000;
var refreshTimeout;
var numPointsX;
var numPointsY;
var unitWidth;
var unitHeight;
var points;

function onLoad() {
  var svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  svg.setAttribute('width', window.innerWidth);
  svg.setAttribute('height', window.innerHeight);
  document.querySelector('#bg').appendChild(svg);

        var polygon = document.createElementNS(svg.namespaceURI, 'polygon');
            polygon.setAttribute('points', '-5.1 ,-7.1 343.4,-7.1 386.7,194');
        polygon.setAttribute('fill', '#000');
        svg.appendChild(polygon);
      }

But this code didn't work :( What did I do wrong ?

the code for creating the polygon is completely fine, but it will not draw it unless you call "onLoad". if you will be calling it multiple times, you will also need to clear the innerHTML of your div "bg".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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