diff --git a/dist/canvas-nest.js b/dist/canvas-nest.js index 16f956f..8fcfc65 100644 --- a/dist/canvas-nest.js +++ b/dist/canvas-nest.js @@ -1 +1 @@ -!function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function t(e,t){return e(t={exports:{}},t.exports),t.exports}var n=t(function(e,t){Object.defineProperty(t,"__esModule",{value:!0});var n=1;t.default=function(){return""+n++},e.exports=t.default});e(n);var o=t(function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:30,n=null;return function(){for(var o=this,i=arguments.length,r=Array(i),a=0;an||r.x<0?-1:1,r.ya*=r.y>o||r.y<0?-1:1,t.fillStyle="rgba("+e.c.pointColor+")",t.fillRect(r.x-.5,r.y-.5,1,1),u=f+1;u=s.max/2&&(r.x-=.03*l,r.y-=.03*d),c=(s.max-v)/s.max,t.beginPath(),t.lineWidth=c/2,t.strokeStyle="rgba("+e.c.color+","+(c+.2)+")",t.moveTo(r.x,r.y),t.lineTo(s.x,s.y),t.stroke()))}),this.requestFrame(this.drawCanvas)}},{key:"destroy",value:function(){l(this.el),window.onmousemove=this.onmousemove,window.onmouseout=this.onmouseout,f(this.tid),this.canvas.parentNode.removeChild(this.canvas)}}]),e}();y.version="2.0.4";var w,b;new y(document.body,(w=document.getElementsByTagName("script"),{zIndex:(b=w[w.length-1]).getAttribute("zIndex"),opacity:b.getAttribute("opacity"),color:b.getAttribute("color"),pointColor:b.getAttribute("pointColor"),count:Number(b.getAttribute("count"))||99}))}(); +!function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function t(e,t){return e(t={exports:{}},t.exports),t.exports}var n=t(function(e,t){Object.defineProperty(t,"__esModule",{value:!0});var n=1;t.default=function(){return""+n++},e.exports=t.default});e(n);var o=t(function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:60,n=null;return function(){for(var o=this,i=arguments.length,r=Array(i),a=0;aMath.abs(c)&&Math.abs(m)*Math.abs(c)>0?-1:1)*v/s.max*2*r.xa,r.yd=(Math.abs(h)>Math.abs(d)&&Math.abs(h)*Math.abs(d)>0?-1:1)*v/s.max*2*r.ya):(r.xd=0,r.yd=0)),vn||r.x<0?-1:1,r.ya*=r.y>o||r.y<0?-1:1,t.fillRect(r.x-.5,r.y-.5,1,1)}),this.requestFrame(this.drawCanvas)}},{key:"destroy",value:function(){l(this.el),window.onmousemove=this.onmousemove,window.onmouseout=this.onmouseout,v(this.tid),this.canvas.parentNode.removeChild(this.canvas)}}]),e}();y.version="2.0.4";var p,w;new y(document.body,(p=document.getElementsByTagName("script"),{zIndex:(w=p[p.length-1]).getAttribute("zIndex"),opacity:w.getAttribute("opacity"),color:w.getAttribute("color"),pointColor:w.getAttribute("pointColor"),count:Number(w.getAttribute("count"))||99}))}(); diff --git a/dist/canvas-nest.umd.js b/dist/canvas-nest.umd.js index 858c06d..4f7d867 100644 --- a/dist/canvas-nest.umd.js +++ b/dist/canvas-nest.umd.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.CanvasNest=t()}(this,function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function t(e,t){return e(t={exports:{}},t.exports),t.exports}var n=t(function(e,t){Object.defineProperty(t,"__esModule",{value:!0});var n=1;t.default=function(){return""+n++},e.exports=t.default});e(n);var o=t(function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:30,n=null;return function(){for(var o=this,i=arguments.length,r=Array(i),a=0;an||r.x<0?-1:1,r.ya*=r.y>o||r.y<0?-1:1,t.fillStyle="rgba("+e.c.pointColor+")",t.fillRect(r.x-.5,r.y-.5,1,1),u=v+1;u=s.max/2&&(r.x-=.03*l,r.y-=.03*d),c=(s.max-f)/s.max,t.beginPath(),t.lineWidth=c/2,t.strokeStyle="rgba("+e.c.color+","+(c+.2)+")",t.moveTo(r.x,r.y),t.lineTo(s.x,s.y),t.stroke()))}),this.requestFrame(this.drawCanvas)}},{key:"destroy",value:function(){l(this.el),window.onmousemove=this.onmousemove,window.onmouseout=this.onmouseout,v(this.tid),this.canvas.parentNode.removeChild(this.canvas)}}]),e}();return y.version="2.0.4",y}); +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.CanvasNest=t()}(this,function(){"use strict";function e(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}function t(e,t){return e(t={exports:{}},t.exports),t.exports}var n=t(function(e,t){Object.defineProperty(t,"__esModule",{value:!0});var n=1;t.default=function(){return""+n++},e.exports=t.default});e(n);var o=t(function(e,t){Object.defineProperty(t,"__esModule",{value:!0}),t.default=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:60,n=null;return function(){for(var o=this,i=arguments.length,r=Array(i),a=0;aMath.abs(c)&&Math.abs(f)*Math.abs(c)>0?-1:1)*v/s.max*2*r.xa,r.yd=(Math.abs(h)>Math.abs(d)&&Math.abs(h)*Math.abs(d)>0?-1:1)*v/s.max*2*r.ya):(r.xd=0,r.yd=0)),vn||r.x<0?-1:1,r.ya*=r.y>o||r.y<0?-1:1,t.fillRect(r.x-.5,r.y-.5,1,1)}),this.requestFrame(this.drawCanvas)}},{key:"destroy",value:function(){l(this.el),window.onmousemove=this.onmousemove,window.onmouseout=this.onmouseout,v(this.tid),this.canvas.parentNode.removeChild(this.canvas)}}]),e}();return y.version="2.0.4",y}); diff --git a/lib/CanvasNest.js b/lib/CanvasNest.js index 37bff66..79454ae 100644 --- a/lib/CanvasNest.js +++ b/lib/CanvasNest.js @@ -30,6 +30,8 @@ var CanvasNest = function () { y: Math.random() * _this.canvas.height, xa: 2 * Math.random() - 1, // 随机运动返现 ya: 2 * Math.random() - 1, + xd: 0, + yd: 0, max: 6000 // 沾附距离 }; }); @@ -127,15 +129,13 @@ var CanvasNest = function () { d = void 0, x_dist = void 0, y_dist = void 0, - dist = void 0; // 临时节点 + dist = void 0, + prex = void 0, + prey = void 0; // 临时节点 // 遍历处理每一个点 points.forEach(function (r, idx) { - r.x += r.xa; - r.y += r.ya; // 移动 - r.xa *= r.x > width || r.x < 0 ? -1 : 1; - r.ya *= r.y > height || r.y < 0 ? -1 : 1; // 碰到边界,反向反弹 context.fillStyle = 'rgba(' + _this4.c.pointColor + ')'; - context.fillRect(r.x - 0.5, r.y - 0.5, 1, 1); // 绘制一个宽高为1的点 + // 从下一个点开始 for (i = idx + 1; i < all.length; i++) { e = all[i]; @@ -143,12 +143,28 @@ var CanvasNest = function () { if (null !== e.x && null !== e.y) { x_dist = r.x - e.x; // x轴距离 l y_dist = r.y - e.y; // y轴距离 n + prex = r.x + r.xa - e.x; // 预算下一个x,y的位置,以判断是加速还是减速 + prey = r.y + r.ya - e.y; dist = x_dist * x_dist + y_dist * y_dist; // 总距离, m - dist < e.max && (e === current && dist >= e.max / 2 && (r.x -= 0.03 * x_dist, r.y -= 0.03 * y_dist), // 靠近的时候加速 - d = (e.max - dist) / e.max, context.beginPath(), context.lineWidth = d / 2, context.strokeStyle = 'rgba(' + _this4.c.color + ',' + (d + 0.2) + ')', context.moveTo(r.x, r.y), context.lineTo(e.x, e.y), context.stroke()); + if (e === current) { + if (dist < e.max) { + // 加速度大小 + r.xd = (Math.abs(prex) > Math.abs(x_dist) && Math.abs(prex) * Math.abs(x_dist) > 0 ? -1 : 1) * dist / e.max * 2 * r.xa; + r.yd = (Math.abs(prey) > Math.abs(y_dist) && Math.abs(prey) * Math.abs(y_dist) > 0 ? -1 : 1) * dist / e.max * 2 * r.ya; + } else { + r.xd = 0; + r.yd = 0; + } + } + dist < e.max && (d = (e.max - dist) / e.max, context.beginPath(), context.lineWidth = d / 2, context.strokeStyle = 'rgba(' + _this4.c.color + ',' + (d + 0.2) + ')', context.moveTo(r.x, r.y), context.lineTo(e.x, e.y), context.stroke()); } } + r.x += r.xa + r.xd; + r.y += r.ya + r.yd; // 移动 + r.xa *= r.x > width || r.x < 0 ? -1 : 1; + r.ya *= r.y > height || r.y < 0 ? -1 : 1; // 碰到边界,反向反弹 + context.fillRect(r.x - 0.5, r.y - 0.5, 1, 1); // 绘制一个宽高为1的点 }); this.requestFrame(this.drawCanvas); } diff --git a/src/CanvasNest.js b/src/CanvasNest.js index 7b086fc..12c7916 100644 --- a/src/CanvasNest.js +++ b/src/CanvasNest.js @@ -65,6 +65,8 @@ export default class CanvasNest { y: Math.random() * this.canvas.height, xa: 2 * Math.random() - 1, // 随机运动返现 ya: 2 * Math.random() - 1, + xd: 0, + yd: 0, max: 6000 // 沾附距离 })); }; @@ -97,15 +99,11 @@ export default class CanvasNest { context.clearRect(0, 0, width, height); // 随机的线条和当前位置联合数组 - let e, i, d, x_dist, y_dist, dist; // 临时节点 + let e, i, d, x_dist, y_dist, dist, prex, prey; // 临时节点 // 遍历处理每一个点 points.forEach((r, idx) => { - r.x += r.xa; - r.y += r.ya; // 移动 - r.xa *= r.x > width || r.x < 0 ? -1 : 1; - r.ya *= r.y > height || r.y < 0 ? -1 : 1; // 碰到边界,反向反弹 - context.fillStyle = `rgba(${this.c.pointColor})`; - context.fillRect(r.x - 0.5, r.y - 0.5, 1, 1); // 绘制一个宽高为1的点 + context.fillStyle = `rgba(${this.c.pointColor})`; + // 从下一个点开始 for (i = idx + 1; i < all.length; i ++) { e = all[i]; @@ -113,9 +111,20 @@ export default class CanvasNest { if (null !== e.x && null !== e.y) { x_dist = r.x - e.x; // x轴距离 l y_dist = r.y - e.y; // y轴距离 n + prex = r.x + r.xa - e.x; // 预算下一个x,y的位置,以判断是加速还是减速 + prey = r.y + r.ya - e.y; dist = x_dist * x_dist + y_dist * y_dist; // 总距离, m - dist < e.max && (e === current && dist >= e.max / 2 && (r.x -= 0.03 * x_dist, r.y -= 0.03 * y_dist), // 靠近的时候加速 + if (e === current) { + if (dist < e.max) { // 加速度大小 + r.xd = (Math.abs(prex) > Math.abs(x_dist) && Math.abs(prex) * Math.abs(x_dist) > 0 ? -1 : 1) * dist / e.max * 2 * r.xa; + r.yd = (Math.abs(prey) > Math.abs(y_dist) && Math.abs(prey) * Math.abs(y_dist) > 0 ? -1 : 1) * dist / e.max * 2 * r.ya; + } else { + r.xd = 0; + r.yd = 0; + } + } + dist < e.max && ( d = (e.max - dist) / e.max, context.beginPath(), context.lineWidth = d / 2, @@ -125,6 +134,11 @@ export default class CanvasNest { context.stroke()); } } + r.x += r.xa + r.xd; + r.y += r.ya + r.yd; // 移动 + r.xa *= r.x > width || r.x < 0 ? -1 : 1; + r.ya *= r.y > height || r.y < 0 ? -1 : 1; // 碰到边界,反向反弹 + context.fillRect(r.x - 0.5, r.y - 0.5, 1, 1); // 绘制一个宽高为1的点 }); this.requestFrame(this.drawCanvas); }