Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
#canvas { position: fixed; }
JavaScript
const canvas = document.querySelector("#canvas"); const context = canvas.getContext("2d", { alpha: false }); let vw = canvas.width = window.innerWidth; let vh = canvas.height = window.innerHeight; // let boxSize = Math.min(vw / 6, vh / 4); let boxSize = 80; const maxLevel = 10; const roundTo = 1000; const color1 = { h: 69, s: 75, l: 51 }; const color2 = { h: 288, s: 98, l: 17 }; const colors = getColors(color1, color2, maxLevel + 2); const mouse = { x: vw / 2, y: vh / 2 }; let count = 0; let lean = 0; let scale = 0; let resized = false; let requestId = null; let x = (vw - boxSize) / 2; let y = vh; // // CALC BRANCHES // ================================================================================ const calcBranches = (function() { let memo = {}; let memoize = function(width, scale, lean) { const memoKey = `${width}-${scale}-${lean}`; if (!memo[memoKey]) { const currentH = width * scale; const result = { leftSize: Math.sqrt(currentH ** 2 + (width * (0.5 - lean)) ** 2), rightSize: Math.sqrt(currentH ** 2 + (width * (0.5 + lean)) ** 2), leftAngle: Math.atan(currentH / ((0.5 - lean) * width)), rightAngle: Math.atan(currentH / ((0.5 + lean) * width)) }; memo[memoKey] = result; memoize.count++; } return memo[memoKey]; } memoize.count = 0; memoize.cache = memo; return memoize; })(); window.addEventListener("resize", onResize); window.addEventListener("mousemove", onMouseMove); update(); // // DRAW TREE // ================================================================================ function drawTree(size, scale, lean, level) { if (size < 1) { count += 2; return; } size = Math.round(size * roundTo) / roundTo; const { leftSize, leftAngle, rightSize, rightAngle } = calcBranches(size, scale, lean); context.save(); context.fillRect(0, 0, size, -size); context.fillStyle = colors[level]; context.translate(0, -size); context.rotate(-leftAngle); if (level) { drawTree(leftSize, scale, lean, level - 1); } else { context.fillRect(0, 0, leftSize, -leftSize); } context.translate(leftSize, 0); context.rotate(rightAngle + leftAngle); if (level) { drawTree(rightSize, scale, lean, level - 1); } else { context.fillRect(0, 0, rightSize, -rightSize); } context.restore(); } // // UPDATE // ================================================================================ function update() { if (resized) { vw = canvas.width = window.innerWidth; vh = canvas.height = window.innerHeight; // boxSize = Math.min(vw / 6, vh / 4); x = (vw - boxSize) / 2; y = vh; } scale = Math.round(map(mouse.y, vh, 0, 0, 0.8) * roundTo) / roundTo; lean = Math.round(map(mouse.x, 0, vw, 0.5, -0.5) * roundTo) / roundTo; context.fillStyle = "white"; context.fillRect(0, 0, vw, vh); context.save(); context.fillStyle = colors[maxLevel+1]; context.translate(x, y); drawTree(boxSize, scale, lean, maxLevel, maxLevel); context.restore(); resized = false; requestId = null; } // // GET COLORS // ================================================================================ function getColors(c1, c2, steps) { const colors = []; for (let i = 0; i < steps; i++) { const t = i / (steps - 1); const h = Math.round(lerp(c1.h, c2.h, t)); const s = Math.round(lerp(c1.s, c2.s, t)); const l = Math.round(lerp(c1.l, c2.l, t)); colors.push(`hsl(${h},${s}%,${l}%)`); } return colors; } // // ON MOUSE MOVE // ================================================================================ function onMouseMove(event) { mouse.x = event.clientX; mouse.y = event.clientY; if (!requestId) { requestId = requestAnimationFrame(update); } } // // ON RESIZE // ================================================================================ function onResize() { resized = true; if (!requestId) { requestId = requestAnimationFrame(update); } } function lerp(a, b, t) { return a + (b - a) * t; } function map(x, a, b, c, d) { return c + (d - c) * ((x - a) / (b - a)) || 0; }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>毕达哥拉斯树-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号