【问题标题】:Generating SVG polygons with PHP/HTML gives strange results使用 PHP/HTML 生成 SVG 多边形会产生奇怪的结果
【发布时间】:2021-02-20 03:01:10
【问题描述】:

我想渲染一个 SVG 图像,比如手表。 viewBox 是 1000 x 1000px,所以中心/偏移量是 500,500

基本的html是这样的:

    <div class="imageMap">
    <svg xmlns="http://www.w3.org/2000/svg" 
        width="100%" height="100%" viewBox="0 0 1000 1000">
    <defs><style>
        .imageMap polygon { fill: red; opacity:0.5; }
        .imageMap polygon:nth-child(2n) { fill: green; }
        </style></defs>
        <image width="100%" height="100%" 
        xlink:href="clear.gif"></image>
        <?php render() ?>
        </svg>
    </div>

php:

    function render() {
      $parts=4;
      $arc = 360/$parts; $offsetX=500; $offsetY=500;
      for($i=0; $i < 360; $i += $arc) {
        $x0 = $offsetX;
        $y0 = $offsetY;
        $x1 = cos($i) * 500+$offsetX;
        $y1 = sin($i) * 500+$offsetY;
        $x2 = cos($i+$arc) * 500+$offsetX;
        $y2 = sin($i+$arc) * 500+$offsetY;
        $stringArray[] = '<polygon points="'.$x0.','.$y0.','.$x1.','.$y1.','.$x2.','.$y2.'"/>';
      }
      return implode(PHP_EOL, $stringArray);
    }

【问题讨论】:

  • #-) 你是绝对正确的:deg2rad($i) 完成了这项工作!谢谢!

标签: php svg


【解决方案1】:

根据php documentation cos 和 sin 采用弧度值。您提供的值是度数,因此这些值太大了。

您可以使用deg2rad 将度数转换为弧度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2014-12-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2019-04-13
    相关资源
    最近更新 更多