html5 canvas 效果,HTML5 Canvas 光影变幻特效

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

$(document).ready(function() {

// rAF

(function() {

var lastTime = 0;

var vendors = ['webkit', 'moz'];

for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {

window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];

window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];

}

if (!window.requestAnimationFrame) {

window.requestAnimationFrame = function(callback, element) {

var currTime = new Date().getTime();

var timeToCall = Math.max(0, 16 - (currTime - lastTime));

var id = window.setTimeout(function() {

callback(currTime + timeToCall);

}, timeToCall);

lastTime = currTime + timeToCall;

return id;

};

}

if (!window.cancelAnimationFrame) {

window.cancelAnimationFrame = function(id) {

clearTimeout(id);

};

}

});

// fast absolute value

var fastAbs = function(value) {

return (value ^ (value >> 31)) - (value >> 31);

};

// capture dimesions

var H = $(window).height();

var W = $(window).width();

// setup

var nodes = (W < 960) ? 120 : 200;

var dxy = new Array();

var x = 0,

y = 0;

var s = 2; // speed

var t = 140; // threshold

var ax, ay;

var d = 0.1;

for (var i = 0; i < nodes; i++) {

dxy[i] = [

Math.random() * 360, // random direction vector

(W < 600) ? (W * Math.random()) : (W / 2), (W < 600) ? (H * Math.random()) : (H / 2)

];

}

// canvas setup

var c = document.getElementById('canvas');

var ctx = c.getContext('2d');

c.width = W;

c.height = H;

function loop() {

for (var i = 0; i < nodes; i++) {

x = Math.sin(dxy[i][0]) * s + dxy[i][1];

y = Math.cos(dxy[i][0]) * s + dxy[i][2];

x = (x > W + t) ? -t : (x < -t) ? W + t : x;

y = (y > H + t) ? -t : (y < -t) ? H + t : y;

dxy[i][0] = dxy[i][0] + ((Math.random() * 2 - 1) * d);

for (var j = 0; j < nodes; j++) {

ax = fastAbs(x - dxy[j][1]);

ay = fastAbs(y - dxy[j][2]);

if ((ax < t) && (ay < t)) {

ctx.beginPath();

ctx.moveTo(x, y);

ctx.lineTo(dxy[j][1], dxy[j][2]);

if (x < dxy[i][1]) {

ctx.strokeStyle = 'black';

} else {

ctx.strokeStyle = 'white';

}

ctx.globalAlpha = 0.1;

ctx.stroke();

ctx.globalAlpha = 1;

}

}

dxy[i][1] = x;

dxy[i][2] = y;

};

}

// frame requester

function draw() {

requestAnimationFrame(draw);

loop();

}

// pull trigger

draw();

// resize reset

$(window).resize(function() {

H = $(window).height();

W = $(window).width();

c.width = W;

c.height = H;

});

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现骇客帝国中的文字效果,可以借助HTML5 canvas和一些JavaScript代码来实现。下面是一个示例代码: ```html <!DOCTYPE html> <html> <head> <style> canvas { background-color: #000; } </style> </head> <body> <canvas id="canvas"></canvas> <script> // 获取canvas元素 const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 设置canvas宽高 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 定义文字样式 const fontSize = 16; ctx.font = `${fontSize}px Courier`; ctx.fillStyle = '#0F0'; // 定义文本内容 const text = 'Welcome to the Matrix'; // 定义文本的起始位置 let x = (canvas.width - ctx.measureText(text).width) / 2; let y = canvas.height / 2; // 定义绘制文本的间隔 const interval = 100; // 定义控制参数 let currentCharIndex = 0; let lastTime = performance.now(); function draw() { // 计算时间间隔 const currentTime = performance.now(); const deltaTime = currentTime - lastTime; // 控制文字逐个显示的速度 if (deltaTime > interval) { lastTime = currentTime; // 清除画布 ctx.clearRect(0, 0, canvas.width, canvas.height); // 绘制文字 for (let i = 0; i < currentCharIndex; i++) { const char = text.charAt(i); ctx.fillText(char, x, y + (i * fontSize)); } // 更新当前字符索引 currentCharIndex++; // 检测是否显示完所有字符 if (currentCharIndex > text.length) { currentCharIndex = 0; } } // 递归调用draw函数 requestAnimationFrame(draw); } // 启动动画 draw(); </script> </body> </html> ``` 这段代码会在页面上创建一个全屏的canvas元素,并通过逐个显示字符的方式实现骇客帝国中的文字效果。你可以根据需要修改文本内容、样式和动画参数来实现自己想要的效果。希望对你有所帮助!如果还有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值