首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >帆布纺丝立方体

帆布纺丝立方体
EN

Stack Overflow用户
提问于 2014-10-27 21:08:03
回答 1查看 361关注 0票数 0

我有一个HTML5帆布旋转立方体在这个codePen:http://codepen.io/celli/pen/xwvnb

有人能帮忙演示如何去除黑背景吗?它看起来像是ctx.fillStyle="#000000";JS中的属性是必需的(尝试在CodePen中更改或删除它),但是我希望有一个透明的背景,并且似乎找不到实现这一目标的方法。

代码语言:javascript
运行
复制
 window.onload = startDemo;

    function Point3D(x,y,z) {
        this.x = x;
        this.y = y;
        this.z = z;

        this.rotateX = function(angle) {
            var rad, cosa, sina, y, z
            rad = angle * Math.PI / 180
            cosa = Math.cos(rad)
            sina = Math.sin(rad)
            y = this.y * cosa - this.z * sina
            z = this.y * sina + this.z * cosa
            return new Point3D(this.x, y, z)
        }

        this.rotateY = function(angle) {
            var rad, cosa, sina, x, z
            rad = angle * Math.PI / 180
            cosa = Math.cos(rad)
            sina = Math.sin(rad)
            z = this.z * cosa - this.x * sina
            x = this.z * sina + this.x * cosa
            return new Point3D(x,this.y, z)
        }

        this.rotateZ = function(angle) {
            var rad, cosa, sina, x, y
            rad = angle * Math.PI / 180
            cosa = Math.cos(rad)
            sina = Math.sin(rad)
            x = this.x * cosa - this.y * sina
            y = this.x * sina + this.y * cosa
            return new Point3D(x, y, this.z)
        }

        this.project = function(viewWidth, viewHeight, fov, viewDistance) {
            var factor, x, y
            factor = fov / (viewDistance + this.z)
            x = this.x * factor + viewWidth / 2
            y = this.y * factor + viewHeight / 2
            return new Point3D(x, y, this.z)
        }
    }

    var vertices = [
        new Point3D(-1,1,-1),
        new Point3D(1,1,-1),
        new Point3D(1,-1,-1),
        new Point3D(-1,-1,-1),
        new Point3D(-1,1,1),
        new Point3D(1,1,1),
        new Point3D(1,-1,1),
        new Point3D(-1,-1,1)
    ];

    // Define the vertices that compose each of the 6 faces. These numbers are
    // indices to the vertex list defined above.
    var faces = [[0,1,2,3],[1,5,6,2],[5,4,7,6],[4,0,3,7],[0,4,5,1],[3,2,6,7]]

    var angle = 0;

    function startDemo() {
        canvas = document.getElementById("cubeSpin");
        if( canvas && canvas.getContext ) {
            ctx = canvas.getContext("2d");
            setInterval(loop,33);
        }
    }

    function loop() {
        var t = new Array();

        ctx.fillStyle="#000000";
        ctx.fillRect(0,0,320,200);

        for( var i = 0; i < vertices.length; i++ ) {
            var v = vertices[i];
            var r = v.rotateX(angle).rotateY(angle).rotateZ(angle);
            var p = r.project(320,200,128,3.5);
            t.push(p)
        }

        ctx.strokeStyle = "rgb(255,255,255)"

        for( var i = 0; i < faces.length; i++ ) {
            var f = faces[i]
            ctx.beginPath()
            ctx.moveTo(t[f[0]].x,t[f[0]].y)
            ctx.lineTo(t[f[1]].x,t[f[1]].y)
            ctx.lineTo(t[f[2]].x,t[f[2]].y)
            ctx.lineTo(t[f[3]].x,t[f[3]].y)
            ctx.closePath()
            ctx.stroke()
        }
        angle += 2

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-27 21:38:10

变更:

代码语言:javascript
运行
复制
ctx.fillStyle="#FF0000";
ctx.fillRect(0,0,320,200);

To:

代码语言:javascript
运行
复制
ctx.clearRect(0,0,320,200);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26596866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档