代码之家  ›  专栏  ›  技术社区  ›  c0ldcash

Pixi js使用画布纹理填充形状

  •  2
  • c0ldcash  · 技术社区  · 7 年前

    我在想一个办法,我可以填补一个形状在PIXI。js使用从画布创建的纹理。 这样做的原因是我希望能够在普通html画布上创建渐变,然后他们用它制作纹理,并将其添加到pixi阶段。现在我可以做到了,这是我测试的第一件事,它起作用了。但最终目标是在PIXI中创建形状。js使用图形类,然后用我的渐变填充它们。我不知道如何做到这一点,因为。beginFill()方法只接受颜色。如何用纹理填充形状? 这是我的密码。我知道辅助画布创作有点冗长,但这是以后的问题。

    $(document).ready(function() {
        var stage = new PIXI.Container();
        var renderer = PIXI.autoDetectRenderer(800, 600);
        document.body.appendChild(renderer.view);
    
        //Aliases
        var Sprite = PIXI.Sprite;
        var TextureCache = PIXI.utils.TextureCache;
        var resources = PIXI. loader.resources;
    
        function AuxCanvas(id, w, h, color1, color2) {
            this.id = id;
            this.w = w;
            this.h = h;
            this.color1 = color1;
            this.color2 = color2;
        }
        // create and append the canvas to body
        AuxCanvas.prototype.create = function() {
            $('body').append('<canvas id="'+
                this.id+'"  width="'+
                this.w+'" height="'+
                this.h+'"></canvas>');
        }
        // draw gradient
        AuxCanvas.prototype.drawGradient = function() {
            var canvas = document.getElementById(this.id);
            var ctx = canvas.getContext('2d');
            var gradient = ctx.createLinearGradient(0, 0, 800, 0);
            gradient.addColorStop(0, this.color1);
            gradient.addColorStop(1, this.color2);
            ctx.fillStyle = gradient;
            ctx.fillRect(0, 0, this.w, this.h);
        }
    
        function setup() {
    
            var graphics = new PIXI.Graphics();
            graphics.beginFill(PIXI.Texture.fromCanvas(can1)); //This doesn't work obviously
            graphics.drawCircle(60, 185, 40); 
            graphics.endFill();
            stage.addChild(graphics);
            renderer.render(stage);
        }
    
        var can1 = new AuxCanvas("can1", 800, 600, "green", "yellow");
        can1.create();
        can1.drawGradient();
    
        var can2 = new AuxCanvas("can2", 800, 600, "blue", "red");
        can2.create();
        can2.drawGradient();
    
        setup();
    })
    
    1 回复  |  直到 7 年前
        1
  •  10
  •   c0ldcash    7 年前

    好吧,我想出了一个办法,其实很简单。 只需将图形对象作为从html画布创建的精灵的掩码。

    function setup() {
        var can2 = document.getElementById('can2');
        var sprite = new Sprite(PIXI.Texture.fromCanvas(can2))
        var graphics = new PIXI.Graphics();
        graphics.beginFill();
        graphics.drawCircle(300, 300, 200); 
        graphics.endFill();
        sprite.mask = graphics;
        stage.addChild(sprite);
        renderer.render(stage);
    }
    

    此外,作为精灵的子对象添加图形是最好的方式,只需要确保它们的尺寸相同。完成后,我可以自由移动精灵,它的渐变纹理不会改变,或者更准确地说,它会随着精灵移动。当然,一切都必须在维度上相等。

    var graphics = new PIXI.Graphics();
        graphics.beginFill();
        graphics.drawCircle(100, 100, 100); 
        graphics.endFill();
        sprite.addChild(graphics);
        sprite.mask = graphics;