How to add image to Path in PaperJS

泄露秘密 提交于 2019-12-06 14:34:09

If you want to clip a raster with a path, you need to create a group that contains both objects, then set group.clipped property to true. Passing a raster as the argument for a path constructor will not work.

It's easiest to do this in the "main" loop.

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

var balls = [];

// ADD AN ARRAY TO HOLD SOME RASTERS

var rasters = [];
var raster = new Raster("http://upload.wikimedia.org/wikipedia/commons/1/15/Red_Apple.jpg");
raster.scale(.05);
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));

    // ADD RASTERS AND CLIP TO EACH "BALL"

    rasters.push(raster.clone().scale(radius/50));
    var group = new Group(balls[i].path, rasters[i]);
    group.clipped = true;
}
raster.remove();

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();
        rasters[i].position = balls[i].path.position;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!