2021-11-28

Svg doesn't update after changes made with javascript [duplicate]

I have made a page in which I've made an svg element and draw circles in random positions using javascript. There is a problem that I can't see it, like it doesn't update after a change made with appendChild method, because when I open dev tools, cut the svg element using edit as HTML and then paste it in the same place after script I can see a correctly generated svg. Here is the code:

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>

<body>
  <script type="text/javascript">
    var valuesi = new Int32Array(256);
    var values = new Float32Array(valuesi.length);
    crypto.getRandomValues(valuesi);
    var svg = document.createElement("svg");
    svg = document.body.appendChild(svg);
    var width = 500,
      height = 500;
    svg.setAttribute("width", width);
    svg.setAttribute("height", height);
    var circle, radius = 5;
    for (var i = valuesi.length - 1; i > 0; --i) {
      values[i] = valuesi[i] / 2147483648;
    }
    for (var i = values.length - 1; i > 0; --i) {
      circle = document.createElement("circle");
      circle = svg.appendChild(circle);
      circle.setAttribute("cx", width / 2 + values[i] * width / 2);
      --i;
      circle.setAttribute("cy", height / 2 + values[i] * height / 2);
      circle.setAttribute("r", radius);
      circle.setAttribute("fill", "black");
    }
  </script>
</body>
<html>


from Recent Questions - Stack Overflow https://ift.tt/3lh182R
https://ift.tt/eA8V8J

No comments:

Post a Comment