如何填充矩形内的粒子? [英] how to fill particles inside rectangle?

查看:153
本文介绍了如何填充矩形内的粒子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个基本上是文字的矩形,但只是为了清除洞察力我正在使用矩形,矩形内部有一些小颗粒,基本我的想法来自 https://yalantis.com/ 但是在我的尝试中,我被困在这里的实心填充矩形与我为粒子指定的颜色。请帮助我.. :)

I'm looking to draw a rectangle basically text but just for clearing insight I'm working it with rectangle with small particles inside rectangle the basic I idea I got from https://yalantis.com/ but in my attempt I'm stuck here with solid filled rectangle with a color I have specified for particles. Please help me.. :)

谢谢,这是我的代码:

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Off Screen Canvas</title>
<script>
    function createOffscreenCanvas() {
        var offScreenCanvas = document.createElement('canvas');
        offScreenCanvas.width = '1360';
        offScreenCanvas.height = '400';
        var context = offScreenCanvas.getContext("2d");
        var W=200;
        var H=200;
        particleCount = 200;
        particles = []; //this is an array which will hold our particles Object/Class
        function Particle() {
            this.x = Math.random() * W;
            this.y = Math.random() * H;
            this.direction ={"x": -1 + Math.random()*2, "y": -1 + Math.random()*2};
            this.vx = 2 * Math.random() + 4 ;
            this.vy = 2 * Math.random() + 4;
            this.radius = .9 * Math.random() + 1;
            this.move = function(){
                this.x += this.vx * this.direction.x;
                this.y += this.vy * this.direction.y;
            };
            this.changeDirection = function(axis){
                this.direction[axis] *= -1;
            };
            this.draw = function() {
                context.beginPath();
                context.fillStyle = "#0097a7";
                context.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
                context.fill();
            };
            this.boundaryCheck = function(){
                if(this.x >= W){
                    this.x = W;
                    this.changeDirection("x");
                }
                else if(this.x <= 0){
                    this.x = 0;
                    this.changeDirection("x");
                }
                if(this.y >= H){
                    this.y = H;
                    this.changeDirection("y");
                }
                else if(this.y <= 0){
                    this.y = 0;
                    this.changeDirection("y");
                }
            };
        }
        function createParticles(){
            for (var i = particleCount-1; i >= 0; i--) {
                p = new Particle();
                particles.push(p);
            }
        }// end createParticles
        function drawParticles(){
            for (var i = particleCount-1; i >= 0; i--){
                p = particles[i];
                p.draw();
            }
        } //end drawParticles

        function updateParticles(){
            for(var i = particles.length - 1; i >=0; i--){
                p = particles[i];
                p.move();
                p.boundaryCheck();

            }
        }//end updateParticle
        createParticles();
        var part=drawParticles();
        context.fillStyle=part;
        context.fillRect(W-190, H-190, W, H);
        context.fill();
        return offScreenCanvas;
    }
    function copyToOnScreen(offScreenCanvas) {
        var onScreenContext=document.getElementById('onScreen').getContext('2d');
        var offScreenContext = offScreenCanvas.getContext('2d');
        var image=offScreenContext.getImageData(10,10,200,200);
        onScreenContext.putImageData(image,offScreenCanvas.width/2,offScreenCanvas.height/4);
    }

    function main() {
        copyToOnScreen(createOffscreenCanvas());
    }
</script>
<style>
    canvas {
        border: 1px solid red;
    }
</style>
</head>
<body onload="main()"> 
<canvas id="onScreen" width="1360" height="400"></canvas>
 </body>
</html>


推荐答案

在所有像地狱的尝试之后我终于得到了我的感谢moáois回答我的使用粒子滑块发布。这是代码。谢谢大家的帮助:)

well after all the attempts like hell I finally got my answer thanks to moáois answer to my post using particle slider. here is the code. Thanks everyone for your help :)

这篇关于如何填充矩形内的粒子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆