js之:漂浮线
- (function initJParticle( $ ){
- "use strict";
- var createParticlesSandbox, Utils;
- Utils = {};
- /*
- * Create jParticle animation.
- * @param {Object} options Few jParticle options.
- * @return {Object} jQuery object for chaining.
- */
- $.fn.jParticle = function jParticle( options ){
- this.each(function( _, el ){
- if ( typeof el.sandbox === 'object' ) {
- $( el ).removeJParticle();
- }
- el.sandbox = createParticlesSandbox( el, options );
- });
- return this;
- };
- /*
- * Remove jParticle canvas.
- * @return {Object} jQuery object for chaining.
- */
- $.fn.removeJParticle = function removeJParticle(){
- this.each(function( _, el ){
- if ( el.sandbox ) {
- el.sandbox.remove();
- delete el.sandbox;
- }
- });
- return this;
- };
- /*
- * Freeze jParticle animation.
- * @return {Object} jQuery object for chaining.
- */
- $.fn.freezeJParticle = function freezeJParticle(){
- this.each(function( _, el ){
- if ( el.sandbox ) {
- el.sandbox.freeze();
- }
- });
- return this;
- };
- /*
- * Unfreeze jParticle animation.
- * @return {Object} jQuery object for chaining.
- */
- $.fn.unfreezeJParticle = function unfreezeJParticle(){
- this.each(function( _, el ){
- if ( el.sandbox ) {
- el.sandbox.unfreeze();
- }
- });
- return this;
- };
- /*
- * Create a particles sandbox instance.
- * @param {Object} element Element for the sandbox.
- * @param {Object} params Few sandbox's params.
- * @return {Object} Particles sandbox object.
- */
- createParticlesSandbox = function createParticlesSandbox( element, params ){
- var ParticlesSandbox, createParticle;
- ParticlesSandbox = {};
- ParticlesSandbox.canvas = {};
- ParticlesSandbox.mouse = {};
- ParticlesSandbox.particles = [];
- ParticlesSandbox.isAnimated = false;
- /*
- * Initialize the sandbox
- * @param {Object} element Element for the sandbox.
- * @param {Object} params Few sandbox's params.
- */
- ParticlesSandbox.initialize = function initialize( element, params ){
- ParticlesSandbox.initParams( params );
- ParticlesSandbox.initHTML( element );
- ParticlesSandbox.initParticles();
- ParticlesSandbox.initEvents();
- ParticlesSandbox.initAnimation();
- };
- /*
- * Initialize sandbox's params.
- * @param {Object} params Few sandbox's params.
- */
- ParticlesSandbox.initParams = function initParams( params ){
- if ( params && params.color && (!params.particle || ( params.particle && !params.particle.color ) ) ) {
- if ( !params.particle ) {
- params.particle = {};
- }
- params.particle.color = params.color;
- }
- ParticlesSandbox.params = $.extend({
- particlesNumber: 100,
- linkDist: 50,
- createLinkDist: 150,
- disableLinks: false,
- disableMouse: false,
- background: 'black',
- color: 'white',
- width: null,
- height: null,
- linksWidth: 1
- }, params );
- };
- /*
- * Initialize the sandbox's html.
- * @param {Object} element Element for the sandbox.
- */
- ParticlesSandbox.initHTML = function initHTML( element ){
- var canvas;
- canvas = ParticlesSandbox.canvas;
- canvas.container = $( element );
- canvas.element = $('<canvas/>');
- canvas.context = canvas.element.get(0).getContext('2d');
- canvas.container.append( canvas.element );
- canvas.element.css( 'display', 'block' );
- canvas.element.get(0).width = ( ParticlesSandbox.params.width ) ? ParticlesSandbox.params.width : canvas.container.width();
- canvas.element.get(0).height = ( ParticlesSandbox.params.height ) ? ParticlesSandbox.params.height : canvas.container.height();
- canvas.element.css( 'background', ParticlesSandbox.params.background );
- };
- /*
- * Resize canvas.
- */
- ParticlesSandbox.resize = function resize( width, height ){
- if ( width ) {
- canvas.element.get(0).width = width;
- }
- if ( height ) {
- canvas.element.get(0).height = height;
- }
- };
- /*
- * Create all particles in the sandbox.
- */
- ParticlesSandbox.initParticles = function initParticles(){
- var i, count;
- i = 0;
- count = ParticlesSandbox.params.particlesNumber;
- for ( ; i < count; i += 1 ) {
- ParticlesSandbox.particles.push( createParticle(
- ParticlesSandbox.canvas.element.get(0),
- ParticlesSandbox.params.particle
- ) );
- }
- };
- /*
- * Initialize the sandbox's events.
- */
- ParticlesSandbox.initEvents = function initEvents(){
- ParticlesSandbox.canvas.element.mouseenter(function mouseEnterCallback(){
- ParticlesSandbox.mouse.hoverCanvas = true;
- if ( !ParticlesSandbox.isAnimated ) {
- ParticlesSandbox.draw();
- }
- });
- ParticlesSandbox.canvas.element.mouseleave(function mouseLeaveCallback(){
- ParticlesSandbox.mouse.hoverCanvas = false;
- });
- ParticlesSandbox.canvas.element.mousemove(function mouseMoveCallback(e){
- ParticlesSandbox.mouse = $.extend( ParticlesSandbox.mouse, Utils.getMousePosition( e, ParticlesSandbox.canvas.element[0] ) );
- });
- };
- /*
- * Initialize the sandbox's animation.
- */
- ParticlesSandbox.initAnimation = function initAnimation(){
- window.requestAnimFrame =
- window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.ORequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function requestAnimFrame( callback ){
- setTimeOut( callback, 1000/60 );
- };
- ParticlesSandbox.isAnimated = true;
- ParticlesSandbox.draw();
- };
- /*
- * Draw the sandbox canvas.
- */
- ParticlesSandbox.draw = function draw(){
- var i, j, count, canvas, particle, particle2;
- i = 0;
- count = ParticlesSandbox.particles.length;
- canvas = ParticlesSandbox.canvas;
- canvas.context.clearRect( 0, 0, canvas.element.get(0).width, canvas.element.get(0).height );
- for ( ; i < count; i += 1 ) {
- particle = ParticlesSandbox.particles[i];
- if ( ParticlesSandbox.isAnimated ) {
- particle.update();
- }
- particle.draw();
- if ( !ParticlesSandbox.params.disableMouse && ParticlesSandbox.mouse.hoverCanvas ) {
- ParticlesSandbox.drawLink(
- particle.getPosition('x'),
- particle.getPosition('y'),
- ParticlesSandbox.mouse.x,
- ParticlesSandbox.mouse.y
- );
- }
- if ( !ParticlesSandbox.params.disableLinks ) {
- for ( j = i+1; j < count; j += 1 ) {
- particle2 = ParticlesSandbox.particles[j];
- ParticlesSandbox.drawLink(
- particle.getPosition('x'),
- particle.getPosition('y'),
- particle2.getPosition('x'),
- particle2.getPosition('y')
- );
- }
- }
- }
- ParticlesSandbox.requestID = window.requestAnimFrame( ParticlesSandbox.draw );
- };
- /*
- * Draw a link between two particles.
- * @param {int} x First object abscissa coords.
- * @param {int} y First object ordered coords.
- * @param {int} x2 Second object abscissa coords.
- * @param {int} y2 Second object ordered coords.
- */
- ParticlesSandbox.drawLink = function drawLink( x, y, x2, y2 ){
- var context;
- if ( Utils.getDistance( x, y, x2, y2 ) <= ParticlesSandbox.params.createLinkDist ) {
- context = ParticlesSandbox.canvas.context;
- context.save();
- context.beginPath();
- context.lineWidth = ParticlesSandbox.params.linksWidth;
- context.moveTo( x, y );
- context.lineTo( x2, y2 );
- context.globalAlpha = ParticlesSandbox.getOpacityLink( x, y, x2, y2 );
- context.strokeStyle = ParticlesSandbox.params.color;
- context.lineCap = 'round';
- context.stroke();
- context.closePath();
- context.restore();
- }
- };
- /*
- * Get opacity for link two particles.
- * @param {int} x First object abscissa coords.
- * @param {int} y First object ordered coords.
- * @param {int} x2 Second object abscissa coords.
- * @param {int} y2 Second object ordered coords.
- * @return {int} 0 <= opacity <= 1
- */
- ParticlesSandbox.getOpacityLink = function getOpacityLink( x, y, x2, y2 ){
- var dist, opacity, linkDist, createLinkDist;
- dist = Utils.getDistance( x, y, x2, y2 );
- linkDist = ParticlesSandbox.params.linkDist;
- createLinkDist = ParticlesSandbox.params.createLinkDist;
- if ( dist <= linkDist ) {
- opacity = 1;
- } else if ( dist > createLinkDist ) {
- opacity = 0;
- } else {
- opacity = 1 - ( ( ( dist - linkDist ) * 100 ) / ( createLinkDist - linkDist ) ) / 100;
- }
- return opacity;
- };
- /*
- * Freeze the animation.
- */
- ParticlesSandbox.freeze = function freeze(){
- if ( ParticlesSandbox.isAnimated ) {
- ParticlesSandbox.isAnimated = false;
- }
- };
- /*
- * Unfreeze the animation.
- */
- ParticlesSandbox.unfreeze = function unfreeze(){
- if ( !ParticlesSandbox.isAnimated ) {
- ParticlesSandbox.isAnimated = true;
- }
- };
- /*
- * Remove the animation's canvas.
- */
- ParticlesSandbox.remove = function remove(){
- ParticlesSandbox.canvas.element.remove();
- };
- /*
- * Create a particle instance.
- * @param {Object} canvas DOM element.
- * @param {Object} params Few particle's params.
- * @return {Object} Particle object.
- */
- createParticle = function createParticle( canvas, params ){
- var Particle;
- Particle = {};
- Particle.canvas = {};
- Particle.vector = {};
- /*
- * Initialize the particle.
- * @param {Object} canvas DOM element.
- * @param {Object} params Few particle's params.
- */
- Particle.initialize = function initialize( canvas, params ){
- Particle.params = $.extend({
- color: 'white',
- minSize: 2,
- maxSize: 4,
- speed: 60
- }, params );
- Particle.setCanvasContext( canvas );
- Particle.initSize();
- Particle.initPosition();
- Particle.initVectors();
- };
- /*
- * Initialize particle's position.
- */
- Particle.initPosition = function initPosition(){
- Particle.x = Utils.getRandNumber( 0 + Particle.radius, Particle.canvas.element.width - Particle.radius );
- Particle.y = Utils.getRandNumber( 0 + Particle.radius, Particle.canvas.element.height - Particle.radius );
- };
- /*
- * Initialize particle's size.
- */
- Particle.initSize = function initSize(){
- Particle.size = Utils.getRandNumber( Particle.params.minSize, Particle.params.maxSize );
- Particle.radius = Particle.size / 2;
- };
- /*
- * Initialize particle's vectors for speed.
- */
- Particle.initVectors = function initVectors(){
- do {
- Particle.vector.x = Utils.getRandNumber( -Particle.params.speed / 60, Particle.params.speed / 60, false );
- Particle.vector.y = Utils.getRandNumber( -Particle.params.speed / 60, Particle.params.speed / 60, false );
- } while ( Particle.vector.x == 0 || Particle.vector.y == 0 )
- };
- /*
- * Set the context to draw particles.
- * @param {Object} canvas Canvas.
- */
- Particle.setCanvasContext = function setCanvasContext( canvas ){
- var context;
- Particle.canvas.element = canvas;
- context = canvas.getContext('2d');
- if ( typeof context === 'object' && typeof context.canvas === 'object' ) {
- Particle.canvas.context = context;
- } else {
- throw "Error: Can't set canvas context to Particle because context isn't a CanvasRenderingContext2D object.";
- }
- };
- /*
- * Draw particle.
- */
- Particle.draw = function draw(){
- var context = Particle.canvas.context;
- context.beginPath();
- context.arc( Particle.x, Particle.y, Particle.size /2, 0, Math.PI*2 );
- context.fillStyle = Particle.params.color;
- context.fill();
- context.closePath();
- };
- /*
- * Update the particle's position.
- */
- Particle.update = function update(){
- Particle.x += Particle.vector.x;
- Particle.y += Particle.vector.y;
- if ( 0 > ( Particle.x - Particle.radius ) || ( Particle.x + Particle.radius ) > Particle.canvas.element.width ) {
- Particle.vector.x = -Particle.vector.x;
- }
- if ( 0 > ( Particle.y - Particle.radius ) || ( Particle.y + Particle.radius ) > Particle.canvas.element.height ) {
- Particle.vector.y = -Particle.vector.y;
- }
- };
- /*
- * Return position of particle.
- * @param {string} axis Optionnal axis.
- * @return {int|Object} Return object if axis is not defined, else return int.
- */
- Particle.getPosition = function getPosition( axis ){
- if ( typeof axis === 'string' && ( axis != 'x' && axis != 'y' ) ) {
- axis = null;
- }
- return ( typeof( axis ) === 'string' ) ? Particle[ axis ] : { x: Particle.x, y: Particle.y };
- };
- Particle.initialize( canvas, params );
- return {
- getPosition: Particle.getPosition,
- update: Particle.update,
- draw: Particle.draw
- };
- };
- ParticlesSandbox.initialize( element, params );
- return {
- remove: ParticlesSandbox.remove,
- freeze: ParticlesSandbox.freeze,
- unfreeze: ParticlesSandbox.unfreeze,
- resize: ParticlesSandbox.resize
- };
- };
- /*
- * Get rand number between x and y.
- * @param {int} x Minimal number.
- * @param {int} y Maximal number.
- * @param {Boolean} round True is value shouldn't be round.
- * @return {int} Rand number.
- */
- Utils.getRandNumber = function getRandNumber( x, y, round ){
- var value;
- if( x == null ) {
- x = 0;
- }
- if( y == null ) {
- y = 10;
- }
- if( round == null ) {
- round = true;
- }
- value = Math.random() * ( y - x ) + x;
- return ( round ) ? Math.round( value ) : value;
- };
- /*
- * Get distance between to cartesian points.
- * @param {int} x First object abscissa coords.
- * @param {int} y First object ordered coords.
- * @param {int} x2 Second object abscissa coords.
- * @param {int} y2 Second object ordered coords.
- * @return {int} Distance.
- */
- Utils.getDistance = function getDistance( x, y, x2, y2 ){
- return Math.sqrt( Math.pow( x2 - x, 2 ) + Math.pow( y2 - y, 2 ) );
- };
- /*
- * Get mouse position.
- * @param {Object} event The HTML DOM events.
- * @param {Object} element The DOM element.
- * @return {Object} x/y position.
- */
- Utils.getMousePosition = function getMousePosition( event, element ){
- var rect;
- if ( typeof element === 'undefined' ) {
- element = $('body')[0];
- }
- rect = element.getBoundingClientRect();
- return {
- x: event.clientX - rect.left,
- y: event.clientY - rect.top
- };
- };
- })( jQuery )
调用方法:
html:
- <div class="layui-canvs"></div>
js:
- $(".layui-canvs").jParticle({
- background: "#141414",
- color: "#E6E6E6"
- });
注意:需要先设置 canvas的宽高
js之:漂浮线的更多相关文章
- Js弹性漂浮广告代码
<html><head><meta http-equiv="Content-Type" content="text/html; charse ...
- three.js 创建点 线 面
<html> <head> <title>My first three.js app</title> <style> body { marg ...
- d3.js制作连线动画图和编辑器
此文章为原创文章,原文地址:https://www.cnblogs.com/eagle1098/p/11431679.html 连线动画图 编辑器 效果如上图所示.本项目使用主要d3.jsv4制作,分 ...
- js 开源k线图开发库
https://github.com/andredumas/techan.js/wiki http://techanjs.org/ A visual, stock charting (Candlest ...
- vue.js打包部署线上
你完成了工程开发,需要部署到外网环境,要进行下面的步骤: 一.首先你要购买一个服务器或者有自己的服务器.我介绍给大家的一个免费的服务器:http://free.3v.do/index.html可以免费 ...
- JavaScript浮动广告代码,容纯DIV/CSS对联漂浮广告代码,兼容性非常好的js右下角与漂浮广告代码
基于JavaScript代码实现随机漂浮图片广告,javascript图片广告 在网上有很多这样的代码,不过未必符合W3C标准,因为在头部加上<!DOCTYPE html>类似标签之后,漂 ...
- 一些js 插件的作用
前言: 从一些开源网站上下载下来的 后台管理系统模板一般会有很多的js ,其js 的功能是什么呢?这里随手查询了一下,记录下来 正文: 1.zDialog.js 各种弹窗插件详细案例:http://w ...
- 怎么评价Facebook的Relay框架?Meteor.js 是什么?
http://www.zhihu.com/question/34531232?rf=34500201 Meteor.js 是什么? 作者:陈天链接:http://www.zhihu.com/quest ...
- JS常用正则表达式备忘录
摘要: 玩转正则表达式. 原文:JS常用正则表达式备忘录 作者:前端小智 Fundebug经授权转载,版权归原作者所有. 正则表达式或"regex"用于匹配字符串的各个部分 下面是 ...
随机推荐
- .NET写入文件操作
2018-01-16 22:44:35 许多程序需要记录运行日志,这就需要将程序运行记录写入本机,一般是.txt 文本或.csv 文件.具体操作如下: 一.C# using System.IO; ...
- echart图表中legend不显示问题
主要是legend中的name要和series中的name对应上.
- PDIUSBD12管脚简述
PDIUSBD12管脚简述 PDIUSBD12管脚及简述 PDIUSBD12读写时序图 CS_N是片选信号,当片选信号位低电平时,下面的操作才有效.由于板子上将CS_N接地,所以它一 ...
- [译]ABP框架v2.3.0已经发布!
在新冠病毒的日子里,我们发布了ABP框架v2.3, 这篇文章将说明本次发布新增内容和过去的两周我们做了什么. 关于新冠病毒和我们的团队 关于冠状病毒的状况我们很难过.在Volosoft的团队,我们有不 ...
- 在Windows中像Linux里一样使用CMake和make
1. 安装GCC环境 1.1 安装MinGW(Minimalist GNU for Windows) 首先下载MinGW,并安装.安装完成之后运行MinGW Installer.界面如下.勾选自己需要 ...
- F版本SpringCloud1—大白话为啥要有微服务?啥是微服务?SpringCloud为什么有那么多组件?
前言 为什么要有微服务呢? 什么是微服务? SpringCloud 中为什么会有那么多的组件? ...... 作为SpringCloud教程的第一篇,不讲解具体的技术使用,通过一个通俗易懂的小故事,来 ...
- 二维数组及Arrays工具类
1.二维数组 概念: 数组中的每一个元素类型都是一维数组 二维数组初始化方式: 静态初始化: 格式: 元素类型[][] 数组名 = new 元素类型[][]{{一维数组1},{一维数组2},{一维数组 ...
- [UWP]抄抄《CSS 故障艺术》的动画
1. 前言 什么是故障艺术(Glitch Art 风)?我们熟知的抖音的 LOGO 正是故障艺术其中一种表现形式.它有一种魔幻的感觉,看起来具有闪烁.震动的效果,很吸引人眼球.故障艺术它模拟了画面信号 ...
- Django redis的使用
一 简介 redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted ...
- MySQL数据库02
MySQL数据库 前言: 前面我们了解了什么是数据库,什么是MySQL数据库以及如何运用,接下来我们接着深入学习MySQL. (提前声明,以下所提供的事例不标准,仅供参考) 数据库的备份与还原: 备份 ...