微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在paper.js中将文本插入这些Ball对象?

如何解决如何在paper.js中将文本插入这些Ball对象?

我想在(http://paperjs.org/examples/candy-crash/)项目的每个球中添加一些单字文本。

例如,python写在一个球上,而javascript写在另一个球上。

我不知道有可能这样做吗?以及如何做到这一点。

下面是生成它的书面脚本代码

function Ball(r,p,v) {
    this.radius = r;
    this.point = p;
    this.vector = v;
    this.maxVec = 15;
    this.numSegment = Math.floor(r / 3 + 2);
    this.boundOffset = [];
    this.boundOffsetBuff = [];
    this.sidePoints = [];
    

    this.path = new Path({
        fillColor: {
            hue: Math.random() * 360,saturation: 1,brightness: 1
        },blendMode: 'lighter',});


    for (var i = 0; i < this.numSegment; i++) {
        this.boundOffset.push(this.radius);
        this.boundOffsetBuff.push(this.radius);
        this.path.add(new Point());
        this.sidePoints.push(new Point({
            angle: 360 / this.numSegment * i,length: 1
        }));
    }
}

Ball.prototype = {
    iterate: function() {
        this.checkBorders();
        if (this.vector.length > this.maxVec)
            this.vector.length = this.maxVec;
        this.point += this.vector;
        this.updateShape();
    },checkBorders: function() {
        var size = view.size;
        if (this.point.x < -this.radius)
            this.point.x = size.width + this.radius;
        if (this.point.x > size.width + this.radius)
            this.point.x = -this.radius;
        if (this.point.y < -this.radius)
            this.point.y = size.height + this.radius;
        if (this.point.y > size.height + this.radius)
            this.point.y = -this.radius;
    },updateShape: function() {
        var segments = this.path.segments;
        for (var i = 0; i < this.numSegment; i ++)
            segments[i].point = this.getSidePoint(i);

        this.path.smooth();
        for (var i = 0; i < this.numSegment; i ++) {
            if (this.boundOffset[i] < this.radius / 4)
                this.boundOffset[i] = this.radius / 4;
            var next = (i + 1) % this.numSegment;
            var prev = (i > 0) ? i - 1 : this.numSegment - 1;
            var offset = this.boundOffset[i];
            offset += (this.radius - offset) / 15;
            offset += ((this.boundOffset[next] + this.boundOffset[prev]) / 2 - offset) / 3;
            this.boundOffsetBuff[i] = this.boundOffset[i] = offset;
        }
    },react: function(b) {
        var dist = this.point.getdistance(b.point);
        if (dist < this.radius + b.radius && dist != 0) {
            var overlap = this.radius + b.radius - dist;
            var direc = (this.point - b.point).normalize(overlap * 0.015);
            this.vector += direc;
            b.vector -= direc;

            this.calcBounds(b);
            b.calcBounds(this);
            this.updateBounds();
            b.updateBounds();
        }
    },getBoundOffset: function(b) {
        var diff = this.point - b;
        var angle = (diff.angle + 180) % 360;
        return this.boundOffset[Math.floor(angle / 360 * this.boundOffset.length)];
    },calcBounds: function(b) {
        for (var i = 0; i < this.numSegment; i ++) {
            var tp = this.getSidePoint(i);
            var bLen = b.getBoundOffset(tp);
            var td = tp.getdistance(b.point);
            if (td < bLen) {
                this.boundOffsetBuff[i] -= (bLen  - td) / 2;
            }
        }
    },getSidePoint: function(index) {
        return this.point + this.sidePoints[index] * this.boundOffset[index];
    },updateBounds: function() {
        for (var i = 0; i < this.numSegment; i ++)
            this.boundOffset[i] = this.boundOffsetBuff[i];
    }
};

//--------------------- main ---------------------

var balls = [];
var numBalls = 18;
for (var i = 0; i < numBalls; i++) {
    var position = Point.random() * view.size;
    var vector = new Point({
        angle: 360 * Math.random(),length: Math.random() * 10
    });
    var radius = Math.random() * 60 + 60;
    balls.push(new Ball(radius,position,vector));
}

function onFrame() {
    for (var i = 0; i < balls.length - 1; i++) {
        for (var j = i + 1; j < balls.length; j++) {
            balls[i].react(balls[j]);
        }
    }
    for (var i = 0,l = balls.length; i < l; i++) {
        balls[i].iterate();
    }
}

我是新来的。任何帮助都是可观的。

解决方法

您可以通过在路径上添加一个PointText项目以显示球名称来轻松实现此目的。
这是sketch演示解决方案。
这是更新的代码(添加的行带有注释)。

function Ball(r,p,v,name) {
    this.radius = r;
    this.point = p;
    this.vector = v;
    this.maxVec = 15;
    this.numSegment = Math.floor(r / 3 + 2);
    this.boundOffset = [];
    this.boundOffsetBuff = [];
    this.sidePoints = [];


    this.path = new Path({
        fillColor: {
            hue: Math.random() * 360,saturation: 1,brightness: 1
        },blendMode: 'lighter'
    });

    // Create a text item to display the name.
    this.text = new PointText({
        content: name,justification: 'center'
    });


    for (var i = 0; i < this.numSegment; i++) {
        this.boundOffset.push(this.radius);
        this.boundOffsetBuff.push(this.radius);
        this.path.add(new Point());
        this.sidePoints.push(new Point({
            angle: 360 / this.numSegment * i,length: 1
        }));
    }
}

Ball.prototype = {
    iterate: function() {
        this.checkBorders();
        if (this.vector.length > this.maxVec) {
            this.vector.length = this.maxVec;
        }
        this.point += this.vector;
        this.updateShape();
    },checkBorders: function() {
        var size = view.size;
        if (this.point.x < -this.radius) {
            this.point.x = size.width + this.radius;
        }
        if (this.point.x > size.width + this.radius) {
            this.point.x = -this.radius;
        }
        if (this.point.y < -this.radius) {
            this.point.y = size.height + this.radius;
        }
        if (this.point.y > size.height + this.radius) {
            this.point.y = -this.radius;
        }
    },updateShape: function() {
        var segments = this.path.segments;
        for (var i = 0; i < this.numSegment; i++) {
            segments[i].point = this.getSidePoint(i);
        }

        this.path.smooth();
        for (var i = 0; i < this.numSegment; i++) {
            if (this.boundOffset[i] < this.radius / 4) {
                this.boundOffset[i] = this.radius / 4;
            }
            var next = (i + 1) % this.numSegment;
            var prev = (i > 0) ? i - 1 : this.numSegment - 1;
            var offset = this.boundOffset[i];
            offset += (this.radius - offset) / 15;
            offset += ((this.boundOffset[next] + this.boundOffset[prev]) / 2 - offset) / 3;
            this.boundOffsetBuff[i] = this.boundOffset[i] = offset;
        }

        // Place the text at the center of the path.
        this.text.position = this.path.position;
    },react: function(b) {
        var dist = this.point.getDistance(b.point);
        if (dist < this.radius + b.radius && dist != 0) {
            var overlap = this.radius + b.radius - dist;
            var direc = (this.point - b.point).normalize(overlap * 0.015);
            this.vector += direc;
            b.vector -= direc;

            this.calcBounds(b);
            b.calcBounds(this);
            this.updateBounds();
            b.updateBounds();
        }
    },getBoundOffset: function(b) {
        var diff = this.point - b;
        var angle = (diff.angle + 180) % 360;
        return this.boundOffset[Math.floor(angle / 360 * this.boundOffset.length)];
    },calcBounds: function(b) {
        for (var i = 0; i < this.numSegment; i++) {
            var tp = this.getSidePoint(i);
            var bLen = b.getBoundOffset(tp);
            var td = tp.getDistance(b.point);
            if (td < bLen) {
                this.boundOffsetBuff[i] -= (bLen - td) / 2;
            }
        }
    },getSidePoint: function(index) {
        return this.point + this.sidePoints[index] * this.boundOffset[index];
    },updateBounds: function() {
        for (var i = 0; i < this.numSegment; i++) {
            this.boundOffset[i] = this.boundOffsetBuff[i];
        }
    }
};

//--------------------- main ---------------------

var balls = [];
var numBalls = 18;
for (var i = 0; i < numBalls; i++) {
    var position = Point.random() * view.size;
    var vector = new Point({
        angle: 360 * Math.random(),length: Math.random() * 10
    });
    var radius = Math.random() * 60 + 60;
    balls.push(new Ball(radius,position,vector,'Ball ' + i + 1));
}

function onFrame() {
    for (var i = 0; i < balls.length - 1; i++) {
        for (var j = i + 1; j < balls.length; j++) {
            balls[i].react(balls[j]);
        }
    }
    for (var i = 0,l = balls.length; i < l; i++) {
        balls[i].iterate();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?