Toggle navigation
在线编辑器
在线代码
文本比较
jQuery下载
前端库
在线手册
登录/注册
下载代码
html
css
js
分享到微信朋友圈
X
html
css
body,html { background:#213; color:#342345; height:100%; overflow:hidden; } #paper { border:#190C26 solid 20px; background:#0D0514; position:absolute; margin:auto; left:0; right:0; top:0; bottom:0; width:800px; height:600px; }
JavaScript
"use strict"; // #### Simulation state configuration and updating #################################################################### var circle = { x: 400, y: 300, radius: 75, radius2: 75*75 }; var points; // Creates a new rope to manipulate. This function is also called to reset after entering a bad state. function createRope() { points = (function( x0, y0, x1, y1, steps ) { var dx = (x1-x0)/steps; var dy = (y1-y0)/steps; var d = Math.sqrt( dx*dx + dy*dy ); var x = x0; var y = y0; var pts = []; for( var i = 0 ; i < steps ; ++i, x+=dx, y+=dy ) pts.push({ x:x, y:y, lx:x, ly:y+10*Math.random(), distFromParent:d }); // The random variation in the value for "last y" gives the illusion of the rope snapping in to existence. return pts; })( 50, 50, 750, 50, 75 ); } createRope(); function motion() { // Perform Verlet integration on each point, adding gravity along the y-axis. for( var i = 0 ; i < points.length ; ++i ) { var newX = points[i].x + (points[i].x - points[i].lx); var newY = points[i].y + (points[i].y - points[i].ly) + 0.4; points[i].lx = points[i].x; points[i].ly = points[i].y; points[i].x = newX; points[i].y = newY; } } function constraint() { // Apply the constraint algorithm several times to improve accuracy. var iterations = 100; while( iterations-- ) for( var i = 0 ; i < points.length ; ++i ) { // The following block constrains pairs of points [i] and [i-1]. if( i >= 1 ) { // Compute distance between points. var distX = points[i].x - points[i-1].x; var distY = points[i].y - points[i-1].y; var distance = Math.sqrt( distX*distX + distY*distY ); // Determine unit components along each axis. var distCompX = distX / distance; var distCompY = distY / distance; // Determine the total deviation from ideal distance and divide it by two to get the deviation of each point. var distDeviation = (distance - points[i].distFromParent) / 2; // Determine the x and y axis correction movement amounts for the points. var correctionX = distDeviation * distCompX; var correctionY = distDeviation * distCompY; // Correct the position of the pair by bringing each one half way to each other. if( !points[i-1].fixed ) { points[i-1].x += correctionX; points[i-1].y += correctionY; } if( !points[i].fixed ) { points[i].x -= correctionX; points[i].y -= correctionY; } } // Keep the points from going inside of the circle var cdx = points[i].x - circle.x; var cdy = points[i].y - circle.y; if( cdx*cdx + cdy*cdy < circle.radius2 ) { var depthRatio = Math.sqrt( circle.radius2 / (cdx*cdx + cdy*cdy) ); cdx *= depthRatio; cdy *= depthRatio; points[i].x = cdx + circle.x; points[i].y = cdy + circle.y; } // Keep the points inside the stage. if( points[i].x < 0 ) points[i].x = 0; if( points[i].y < 0 ) points[i].y = 0; if( points[i].x > 800 ) points[i].x = 800; if( points[i].y > 600 ) points[i].y = 600; } } // Sometimes NaN happens when you jam it in to a corner or something... RESET function failureCheck() { if( isNaN( points[0].x ) ) createRope(); } // #### View rendering ################################################################################################# var ctx = document.getElementById('paper').getContext("2d"); ctx.lineWidth = 10; ctx.lineCap = "round"; ctx.lineJoin = "round"; function render() { ctx.clearRect(0,0,800,600); ctx.strokeStyle = "#258C59"; ctx.beginPath(); ctx.arc( circle.x, circle.y, circle.radius - ctx.lineWidth, 0, Math.PI*2, true ); ctx.closePath(); ctx.stroke(); ctx.strokeStyle = "#724D27"; ctx.beginPath(); ctx.moveTo( points[0].x, points[0].y ) for( var i = 1 ; i < points.length ; ++i ) ctx.lineTo( points[i].x, points[i].y ) ctx.stroke(); } // #### Main loop and user interface ################################################################################### setInterval(function(){ failureCheck(); motion(); constraint(); render(); },25); var paperElement = document.getElementById("paper"); document.onmousemove = function(e) { circle.x = e.pageX - paperElement.offsetLeft - 20; // 20 is #paper's border-width. circle.y = e.pageY - paperElement.offsetTop - 20; }
粒子
时间
文字
hover
canvas
3d
游戏
音乐
火焰
水波
轮播图
鼠标跟随
动画
css
加载动画
导航
菜单
按钮
滑块
tab
弹出层
统计图
svg
×
Close
在线代码下载提示
开通在线代码永久免费下载,需支付20jQ币
开通后,在线代码模块中所有代码可终身免费下!
您已开通在线代码永久免费下载,关闭提示框后,点下载代码可直接下载!
您已经开通过在线代码永久免费下载
对不起,您的jQ币不足!可通过发布资源 或
直接充值获取jQ币
取消
开通下载
<!doctype html> <html> <head> <meta charset="utf-8"> <title>canvas模拟绳子-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号