Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
3D Rotation Matrix
(move your mouse)
css
body { height: 100%; } body { align-items: center; background-color: #121212; color: #fff; display: flex; font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; font-weight: 100; justify-content: center; line-height: 1.5; text-align: center; -webkit-font-smoothing: antialiased; } canvas { height: 100%; left: 0; position: absolute; top: 0; width: 100%; } .page-wrap { pointer-events: none; } .text-container { max-width: 420px; width: 90%; } h1 { font-size: 22px; } p { font-size: 13px; }
JavaScript
// Change the dot count const dotCount = 100 // init global elements const canvas = document.getElementById('canvas') const ctx = canvas.getContext('2d') canvas.width = window.innerWidth canvas.height = window.innerHeight let pointsRange = Math.min(window.innerWidth, window.innerHeight) baseDotRadius = (pointsRange > 300) ? 3 : 1.5 const maxLineDistance = pointsRange / 2 let xcenter = ctx.canvas.width / 2 let ycenter = (ctx.canvas.height / 2) let dots = [] let alpha = 0 let beta = 0 let gamma = 0 let betaScrollAddition = 0 let gammaScrollAddition = 0 let timedAngleAddition = 0 // Helper function const randomRange = (min, max) => Math.random() * (max - min) + min // Dot Class function Dot (x = null, y = null, z = null) { this.radius = baseDotRadius, this.opacity = 1, this.position = { x: x || randomRange(-pointsRange, pointsRange), y: y || randomRange(-pointsRange, pointsRange), z: z || randomRange(-pointsRange, pointsRange), } this.initialPosition = { x: this.position.x, y: this.position.y, z: this.position.z } } Dot.prototype.update = function() { // z position let one = this.initialPosition.x * -Math.sin(beta) let two = this.initialPosition.y * Math.cos(beta) * Math.sin(gamma) let three = this.initialPosition.z * Math.cos(beta) * Math.cos(gamma) this.position.z = one + two + three // "Depth of field" variables based on z-position let zPercentage = this.position.z / pointsRange this.radius = baseDotRadius + ((baseDotRadius / 3) * zPercentage) this.opacity = 0.5 + ((zPercentage + 1) / 4) let depthOfFieldMultiplier = ((zPercentage + 1) / 2) + 0.5 // x position one = this.initialPosition.x * Math.cos(alpha) * Math.cos(beta) two = this.initialPosition.y * ((Math.cos(alpha) * Math.sin(beta) * Math.sin(gamma)) - (Math.sin(alpha) * Math.cos(gamma))) three = this.initialPosition.z * ((Math.cos(alpha) * Math.sin(beta) * Math.cos(gamma)) + (Math.sin(alpha) * Math.sin(gamma))) this.position.x = (one + two + three) * depthOfFieldMultiplier // y position one = this.initialPosition.x * Math.sin(alpha) * Math.cos(beta) two = this.initialPosition.y * ((Math.sin(alpha) * Math.sin(beta) * Math.sin(gamma)) + (Math.cos(alpha) * Math.cos(gamma))) three = this.initialPosition.z * ((Math.sin(alpha) * Math.sin(beta) * Math.cos(gamma)) - (Math.cos(alpha) * Math.sin(gamma))) this.position.y = (one + two + three) * depthOfFieldMultiplier } const render = () => { timedAngleAddition += 0.2 * Math.PI / 180 beta = timedAngleAddition + betaScrollAddition gamma = timedAngleAddition + gammaScrollAddition ctx.clearRect(0, 0, canvas.width, canvas.height) // draw dots dots.forEach((dot, index) => { // update dot position and radius dot.update() ctx.translate(xcenter + dot.position.x, ycenter + dot.position.y) // draw lines for (let i = index; i < dots.length; i++) { // distance formula to get distance to points let distance = Math.sqrt(Math.pow(dots[i].position.x - dot.position.x, 2) + Math.pow(dots[i].position.y - dot.position.y, 2) + Math.pow(dots[i].position.z - dot.position.z, 2)) if (distance < maxLineDistance) { // use the distance to effect the opacity ctx.lineWidth = 1 ctx.strokeStyle = 'rgba(57, 136, 100, ' + ((1 - (distance / maxLineDistance)) / 2) + ')' ctx.beginPath() ctx.moveTo(0, 0) ctx.lineTo(dots[i].position.x - dot.position.x, dots[i].position.y - dot.position.y) ctx.stroke() } } // draw dots ctx.fillStyle = 'rgba(27, 136, 252, ' + dot.opacity + ')' ctx.beginPath() ctx.arc(0, 0, dot.radius, 0, 2 * Math.PI) ctx.fill() // reset transform matrix ctx.setTransform(1, 0, 0, 1, 0, 0) }) // loop to draw indefinitely window.requestAnimationFrame(render) } // mouse move listener document.body.addEventListener('mousemove', (e) => { let rect = e.target.getBoundingClientRect() let percentageX = (e.clientX - rect.left) / document.body.clientWidth let percentageY = (e.clientY - rect.top) / document.body.clientHeight betaScrollAddition = (percentageX * 2 * Math.PI) - Math.PI gammaScrollAddition = -(percentageY * 2 * Math.PI) - Math.PI }) // resize listenter window.addEventListener('resize', () => { canvas.width = window.innerWidth canvas.height = window.innerHeight pointsRange = Math.min(window.innerWidth, window.innerHeight) }) // create the starting dots for (let i = 0; i < dotCount; i++) { dots.push(new Dot()) } // start animation render()
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>3D旋转矩阵-jq22.com</title> <script src="https://www.jq22.com/jquery/jquery-1.10.2.js"></script> <style>
</style> </head> <body>
<script>
</script>
</body> </html>
2012-2021 jQuery插件库版权所有
jquery插件
|
jq22工具库
|
网页技术
|
广告合作
|
在线反馈
|
版权声明
沪ICP备13043785号-1
浙公网安备 33041102000314号