简体   繁体   English

不知道如何在我的代码中将栅格应用于每个行星

[英]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.

相关问题 不知道如何编码这个jQuery - Don't know how to code this jQuery 在我的代码中,我试图创建房间,但我不知道为什么每次使用新房间创建旧房间时 - In my code i am trying to create rooms but i don't know why every time it create old rooms with new one 我不知道如何应用异步/等待概念 - I don't know how to apply async/await concept 我的代码在第5行有一个问题,说“Missing&#39;()&#39;调用一个构造函数”,我不知道如何调试 - There is a problem in my code on line 5 stating “Missing '()' invoking a constructor” and i don't know how to debug that 想在我的代码中使用 for 循环,但我不知道怎么可能 - A want to use the for loop in my code, but I don't know how is it possible 不知道如何处理我的项目中的输入 - Don't know how to handle input in my project 我不知道如何解决我的反向数组/字符串 - I don't know how to fix my reverse array/string 我的JavaScript没有在CodePen中运行,我也不知道这是否是我的代码中的错误 - My JavaScript isn't running inside CodePen and I don't know if it's a bug with my code or not 不知道如何在代码中使用 target=&quot;_blank&quot; - Don't know how to use target="_blank" in code 我的作业中出现无限循环错误; 不知道如何修复 - Infinite Loop error in my assignment; don't know how to fix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM