I am playing around with the nice code from http://paperjs.org/examples/candy-crash/. I want to replace the colors with an image so that instead of showing say a red circle, it should show a circle with an image inside. Here is the snippet I think I need to modify:
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: 'screen'
});
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
}));
}
}
I am reading up on raster as well. But I don't see how to get this code to work with the image. Thanks for any help.
If I simply do this.path = new Path(raster)
it does not work. It only shows one static image as opposed to all the circles moving around.
UPDATE
Here is a raster to use
var imgUrl ="http://upload.wikimedia.org/wikipedia/commons/1/15/Red_Apple.jpg";
var raster = new Raster();
raster.scale(0.2)
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;
}
}
来源:https://stackoverflow.com/questions/27428726/how-to-add-image-to-path-in-paperjs