我遇到了this site,并爱上了背景动画,特别是相互连接的点,在3D空间中制作动画。 (有问题的元素是:<canvas id="bg" width="1920" height="995"></canvas>
)我是画布动画的新手,从我到目前为止所做的研究中,我不知道应该从哪条路开始为我的项目实现类似的背景动画。到目前为止,我已经研究过Paper.js和普通的Canvas及其JS API。
以下是我的规格:
<canvas>
,而是图形画布。我不反对任何特定的图形包装。)很高兴:
可以查看类似于我想要实现的内容here。
我知道我要做的所有事情都需要整个部分(JS图形库,如Paper.js,自定义JS类等),但我只想知道其他人有什么成功。
谢谢!
答案 0 :(得分:5)
老实说,我只是随心所欲地做到了这一点。我通常不会试图说“给我这个代码”的问题,但我想知道这需要多长时间。它绝对可以清理,名称间隔等等,它甚至可能都不是你想要的,但无论如何效果都很酷。
注意鼠标事件不包含在此示例中。这个问题在细节方面有点模糊,所以我提供的只是非常简单的方法来开始使用3d和canvas 2d元素。
Theres是一些相当不错的代码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();
答案 1 :(得分:0)
当您在没有任何插件的情况下在浏览器中瞄准高质量的3D图形时,它可能会很快变得复杂。有一些更高级别的库可以使这更容易。其中最着名的是three.js。