简体   繁体   English

未显示三个js CSS3d Sprites

[英]Three js CSS3d Sprites not showing up

I've been at this for hours, i'm trying to get sprites to show up using the CSS3d renderer in three js, but to no avail. 我已经花了几个小时了,我试图在3个js中使用CSS3d渲染器来显示精灵,但无济于事。

Here is the code which creates the sprites, allegedly... 这是创建精灵的代码,据称...

        var sprite = document.createElement( 'img' );
        this.mapCanvas = document.createElement( 'canvas' );
        this.mapCanvas.width = this.hotspotWidth;
        this.mapCanvas.height = this.hotspotWidth;
        var ctx = this.mapCanvas.getContext( '2d' );
        console.log( "canvas: ", ctx );
        ctx.rotate( Math.PI / 2 );


        this.object = new THREE.CSS3DSprite( this.mapCanvas );
        console.log( "CSSObject", this.object );
        sprite.src = this.imageSource;
        sprite.addEventListener( "load", function( event ) { 
            console.log( "Sprite Loaded: ", event );
            ctx.drawImage( sprite, 0, 0 ) }, false );
        scene.add( this.object );
        console.log( "HS Object: ", this.object );

It's inside an object, when i log the context 'ctx' and 'this.object' i see what i expect, but they just aren't there! 它在一个对象内部,当我记录上下文“ ctx”和“ this.object”时,我看到了我期望的结果,但是它们根本不在那儿!

Any help would be greatly appreciated. 任何帮助将不胜感激。

Thanks. 谢谢。

Looks like your Sprites are rotated off canvas at "ctx.rotate( Math.PI / 2 );". 看起来您的Sprites已从“ ctx.rotate(Math.PI / 2);”处的画布上旋转了。 By default this is rotating it around the origin (0, 0) so the img disappears off canvas.. 默认情况下,它绕原点(0,0)旋转,因此img从画布上消失。

Try this: 尝试这个:

        sprite.addEventListener( "load", function( event ) { 
            ctx.translate( 20, 20 );
            ctx.rotate( 180*Math.PI/180 );
            ctx.translate( -20, -20 );
            ctx.drawImage( sprite, 0, 0, 40, 40 );
            ctx.restore();
            }, false );

That should give you what you're after. 那应该给你你所追求的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM