【问题标题】:Is ist possible to get/set the radius of an existing path.circle?是否可以获取/设置现有 path.circle 的半径?
【发布时间】:2015-11-19 15:46:24
【问题描述】:

从一条路径开始 - 或其中许多......

var c=paper.Path.Circle(centerPoint, 30);
c.strokeColor="";

我想让那个圆线性增长它的半径。我可以这样做:

var children = paper.project.activeLayer.children;
paper.view.onFrame = function(event) {
    for (var i = 0; i < children.length; i++) {
        var item = children[i];
        item.scale(1.01);
    }

但这会成倍增加半径!

我可以得到一个圆的半径并改变它吗?还是我必须创建一个新的,删除旧的?
scale() 是怎么做到的?

我也想删除大于给定大小的圆圈。

谢谢, 塞巴斯蒂安

【问题讨论】:

  • 如果我的回答解决了您的问题,我希望您通过给它一个绿色复选标记来接受这个答案。

标签: javascript geometry paperjs


【解决方案1】:

你可以得到一个圆的半径,虽然它是间接的。

var radius = circle.bounds.topCenter.y - circle.bounds.center.y;

var radius = circle.bounds.width / 2

给你一个圆的半径。但是一个圆被存储为 4 个带有进出手柄的线段,而不是圆形对象,因此半径不会存储在任何地方。

为了让它看起来变大,你必须删除旧的并绘制一个更大尺寸的新的。

也可以对其进行缩放,但您希望在不复合缩放的情况下对其进行缩放。因此,如果您希望它增长 1.01,然后增长 1.02 而不是 1.0201,等等。您需要每次调整比例因子。

目前尚不清楚您希望如何扩大圈子,但这里有一些代码对您想要做什么做出了一些假设:

function Scale() {
    this.original = 1.0;
    this.current = 1.0;
}

// target refers to original size in fractional terms, e.g., to
// grow by 1% specify 1.01 or to shrink by 1% specify 0.99. It returns
// the scale factor to apply to the current scale to achieve the
// target. So to increase the scale by 10% of the original size each
// time:
//
//     var s = new Scale();
//
//     for (i = 1.1; i <= 2.05; i += 0.1) {
//         var scaleFactor = s.scale(i);
//     }
//
// note the i <= 2.05 to allow for real number math issues.
//
Scale.prototype.scale = function(target) {
    // get the scaling factor from the original size
    var oFactor = target / this.original;
    // now get the factor to scale the current size by
    var cFactor = oFactor / this.current;
    this.current = oFactor;
    return cFactor;
}

【讨论】:

  • 我最终将半径作为属性添加到圆路径 - 最终甚至使用 .data 属性作为自定义数据的容器对象......然后我可以调用 item.scale((item.data.r+10)/item.data.r)。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 2013-08-30
  • 2014-10-03
  • 2011-04-23
  • 1970-01-01
  • 2011-02-17
相关资源
最近更新 更多