[英]Don't know how to apply raster to every planet in my code
I'm trying to draw planets on my canvas following a basic example on paper.js website, but when I want to use a raster in a for loop to apply "texture" to every one of them, only one planet gets the texture. 我试图按照paper.js网站上的一个基本示例在我的画布上绘制行星,但是当我想在for循环中使用栅格对每个行星应用“纹理”时,只有一个行星可以获取纹理。
I've tried a couple solutions online, but none of them allowed me to make what I'm looking for. 我已经在网上尝试了几种解决方案,但是没有一种解决方案能让我做出自己想要的东西。 You will notice that, in my code, I have both planets (Points) and stars (Stars).
您会注意到,在我的代码中,我同时拥有行星(点)和恒星(星)。 The texture is a basic 100*100 square image.
纹理是基本的100 * 100正方形图像。
var url = '../IMG/planettexture.png';
var raster = new Raster(url);
var count = 150;
var x = window.innerWidth;
var y = window.innerHeight;
for (i=0;i<count;i++){
var xrandom = randomCoordinates(x);
var yrandom = randomCoordinates(y);
var center = new Point(xrandom,yrandom);
var points = 12;
var radius1 = randomCoordinates(1);
var radius2 = randomCoordinates(5);
var path = new Path.Star(center, points, radius1, radius2);
path.fillColor = 'white';
raster.position = new Point(xrandom,yrandom);
path.clipMask = true;
}
var path = new Path.Circle({
center: [0, 0],
radius: 10,
fillColor: 'white',
strokeColor: 'black'
});
var symbol = new Symbol(path);
for (var i = 0; i < count; i++) {
var center = Point.random() * view.size;
var placedSymbol = symbol.place(center);
placedSymbol.scale(i / count);
}
function randomCoordinates(number){
return Math.random()*number;
}
function onFrame(event) {
for (var i = 0; i < 450; i++) {
var item = project.activeLayer.children[i];
item.position.x += item.bounds.width / 20;
if (item.bounds.left > view.size.width) {
item.position.x = -item.bounds.width;
}
}
}
You need Paper.js to run this. 您需要Paper.js来运行它。 With this code, only one Star is "textured", the rest isn't visible on my screen.
使用此代码,只有一个“星形”被“纹理化”,其余的在我的屏幕上不可见。 Do you have any idea where the problem is coming from ?
您知道问题出在哪里吗?
Don't you need to clone your raster inside the for loop ? 您是否不需要在for循环内克隆栅格?
var clonedRaster = raster.clone();
clonedRaster.position = new Point(xrandom,yrandom);
Here is a working example: https://codepen.io/JulienLemaitre/pen/PMmBNJ 这是一个工作示例: https : //codepen.io/JulienLemaitre/pen/PMmBNJ
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.