javascript - 如何创建包含 3D 空间中动画的 2D 对象的图形 Canvas ?

标签 javascript html animation canvas 3d

我遇到了this site并爱上了背景动画,特别是在 3D 空间中相互连接的点。 (有问题的元素是: <canvas id="bg" width="1920" height="995"></canvas> )我是 Canvas 动画的新手,从迄今为止所做的研究来看,我不知道应该从什么路径开始为我的项目实现类似的背景动画。到目前为止,我已经研究了 Paper.js 和普通 Canvas 及其 JS API。

以下是我的规范:

  • 能够制作“ Canvas ”(不是字面上的 <canvas> ,而是图形 Canvas 。我不反对任何特定的图形包装器)。
  • 能够使“ Canvas ”响应
  • 在 3D 空间中导航的圆点(2D 圆)(能够在轴上旋转对象是一个优点,在 helix 中制作动画是一个额外的优点。)
  • 这些 Round Point 集的可实例化模块
  • 能够在特定事件(点击、悬停等)上为这些模块设置动画

值得拥有:

  • 矢量图形

与我想要实现的类似的东西可以查看here .

我知道我想做的所有事情都需要将各个部分整合为一个整体(JS 图形库,例如 Paper.js、自定义 JS 类等),但我只想知道其他人有什么成功。

谢谢!

最佳答案

老实说,我只是一时兴起才这么做的。我通常尽量不去回答那些“给我这个代码”的问题,但我想看看需要多长时间才能做到这一点。它绝对可以被清理,命名空间等等,它甚至可能根本不是你正在寻找的东西,但无论如何效果都很酷。

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

有一些相当不错的代码 here这是来自 Foundation HTML5 Animation with JavaScript 的示例代码和书籍练习其中有一些关于使用 Javascript 和 canvas 的 3d 示例的精彩介绍。我建议您检查一下!

<强> Live Demo

<强> Full Screen

<强> fiddle

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();

关于javascript - 如何创建包含 3D 空间中动画的 2D 对象的图形 Canvas ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18198590/

相关文章:

javascript - @Url.ACtion() 中不接受变量参数

javascript - 函数未返回预期结果

javascript - 无法在javascript中获取textarea的值

python - 如何在 Django 中将控制台输出显示为 HTML?

Android 动画列表

ios - 根据动画期间的变化干扰一些UI属性

javascript - classList.add() 插入变量而不是字符串

javascript - SSRS 报告在某些浏览器中不显示

animation - 是否有任何 B 树程序或网站可以直观地展示 B 树的工作原理

javascript - 如何知道 <div> 标签的滚动事件结束