繁体   English   中英

如何使用在3D空间中设置动画的2D对象创建图形画布?

[英]How can I create a graphic canvas with 2D objects animated in 3D space?

我偶然发现了这个网站,并且爱上了背景动画,尤其是在3D空间中进行动画连接的各个点。 (有问题的元素是: <canvas id="bg" width="1920" height="995"></canvas> )我是画布动画的新手,根据到目前为止的研究,我知道不知道我应该从哪条路开始,以便为我的项目实现类似的背景动画。 到目前为止,我已经研究了Paper.js和普通的Canvas及其JS API。

这是我的规格:

  • 能够制作“画布”(不是字面上的<canvas> ,而是图形画布。我并不反对任何特定的图形包装器)。
  • 使“画布”具有响应能力
  • 在3D空间中导航的圆点(2D圆)(在轴上旋转对象的能力是加号,在螺旋中进行动画效果是额外的加号。)
  • 这些圆点集的可实例化模块
  • 能够对特定事件(单击,悬停等)进行动画处理

很高兴拥有:

  • 矢量图形

可以在此处查看与我要实现的目标类似的东西。

我知道我要尝试做的所有事情都需要整体(JS图形库,例如Paper.js,自定义JS类等),但是我只想知道其他人成功了。

谢谢!

老实说,我只是一时兴起。 我通常尽量不回答说“给我这个代码”的问题,但是我想知道这样做需要多长时间。 它肯定可以清理,命名等,它甚至根本不是您要找的东西,但是效果却很酷。

注意此示例中不包括鼠标事件。 这个问题在细节上有点含糊,所以我提供的只是一种非常简单的方法来开始使用3d和canvas 2d元素。

这里有一些相当不错的代码它是带有JavaScript的Foundation HTML5 Animation的示例代码和书籍练习,其中包含一些使用Javascript和canvas的3d示例的精彩介绍。 我建议检查一下!

现场演示

全屏

小提琴

var canvas = document.getElementById("canvas"),
    ctx = canvas.getContext("2d");

canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.lineWidth = 4;

// Color stuff not real important just fluff
var cycle = 0,
    colors = {
        r: 0,
        g: 170,
        b: 0
    };

function colorCycle(inc) {
    cycle += inc;
    if (cycle > 100) {
        cycle = 0;
    }
    colors.r = ~~ (Math.sin(.3 * cycle + 0) * 127 + 128);
    colors.g = ~~ (Math.sin(.3 * cycle + 2) * 127 + 128);
    colors.b = ~~ (Math.sin(.3 * cycle + 4) * 127 + 128);
}

// Sections and points

function Point(x, y, z, size) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.xpos = 0;
    this.ypos = 0;
    this.size = 5;

    rotateY(this,angle+=0.1);
}

function Section(x, y, z, width) {
    this.x = x;
    this.y = y;
    this.z = z;
    this.width = width;
    this.points = [];
    this.points.push(new Point(this.x - this.width / 2, this.y, this.z, 20));
    this.points.push(new Point(this.x + this.width / 2, this.y, this.z, 20));

    this.render = function (angleX, angleY) {
        ctx.beginPath();
        for (var p = 0; p < this.points.length; p++) {
            var point = this.points[p];

            rotateX(point, angleX);
            rotateY(point, angleY);
            doPerspective(point);

            ctx.arc(point.xpos, point.ypos, point.size, 0, Math.PI * 2, true);

            if (p == 0) {
                ctx.moveTo(this.points[0].xpos, this.points[0].ypos);
            } else {
                ctx.lineTo(point.xpos, point.ypos);
            }
        }

        ctx.closePath();
        ctx.stroke();
        ctx.fill();
    }
}

// 3d Functions for rotation and perspective
function rotateX(point, angleX) {
    var cosX = Math.cos(angleX),
        sinX = Math.sin(angleX),
        y1 = point.y * cosX - point.z * sinX,
        z1 = point.z * cosX + point.y * sinX;
    point.y = y1;
    point.z = z1;
}

function rotateY(point, angleY) {
    var cosY = Math.cos(angleY),
        sinY = Math.sin(angleY),
        x1 = point.x * cosY - point.z * sinY,
        z1 = point.z * cosY + point.x * sinY;
    point.x = x1;
    point.z = z1;
}

function doPerspective(point) {
    if (point.z > -fl) {
        var scale = fl / (fl + point.z);
        point.size = scale * 5;
        point.xpos = vpX + point.x * scale;
        point.ypos = vpY + point.y * scale;
    }
}

// Init code
var sections = [],
    numSections = 50,
    fl = 250,
    vpX,
    vpY,
    angle = 0;

vpX = canvas.width / 2;
vpY = canvas.height / 2;

// make some sections
for (var i = 0; i < numSections; i++) {
    sections.push(new Section(0, -250 + (i * 10), 0, 50));
}

function render() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    colorCycle(0.1);
    ctx.fillStyle = "rgb(" + colors.r + "," + colors.g + "," + colors.b + ")";
    ctx.strokeStyle = "rgb(" + colors.r + "," + colors.g + "," + colors.b + ")";
    for (var i = 0; i < sections.length; i++) {
        sections[i].render(0, 0.03);
    }
    requestAnimationFrame(render);
}
render();

如果您的目标是在浏览器中使用高质量的3d图形而没有任何插件,则它很快就会变得复杂。 有一些更高级别的库使此操作更容易。 最著名的之一是three.js

暂无
暂无

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

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