├── .travis.yml ├── .gitignore ├── screenshot.png ├── .jshintrc ├── gulpfile.js ├── LICENSE ├── package.json ├── index.html ├── dist ├── canvas-nest.min.js └── canvas-nest.js ├── README-zh.md ├── README.md └── src └── canvas-nest.js /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "4.4.4" -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .settings 3 | 4 | node_modules/* 5 | 6 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hosein2398/canvas-nest.js/master/screenshot.png -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "quotmark": true, 3 | "boss": true, 4 | "eqnull": true, 5 | "expr": true, 6 | "funcscope": true, 7 | "loopfunc": true, 8 | "smarttabs": true, 9 | "node": true, 10 | "browser": true, 11 | "undef": true, 12 | "unused": true 13 | } 14 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const gulp = require('gulp'); 3 | const uglify = require('gulp-uglify'); 4 | const rename = require("gulp-rename"); 5 | const injectVersion = require('gulp-inject-version'); 6 | 7 | gulp.task('mini', () => ( 8 | gulp.src('src/canvas-nest.js') 9 | .pipe(injectVersion()) 10 | .pipe(gulp.dest('dist/')) 11 | .pipe(uglify({ 12 | preserveComments: 'license' 13 | })) //uglify 14 | .pipe(rename("canvas-nest.min.js")) 15 | .pipe(gulp.dest('dist/')) 16 | )); -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Hust.cc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "canvas-nest.js", 3 | "officialName": "canvas-nest.js", 4 | "version": "1.0.1", 5 | "summary": "A nest backgroud of website draw on canvas use javascript, do not depends on jQuery.", 6 | "description": "A nest backgroud of website draw on canvas use javascript, do not depends on jQuery.", 7 | "author": { 8 | "name": "hustcc", 9 | "url": "https://github.com/hustcc" 10 | }, 11 | "homepage": "http://www.atool.org", 12 | "license": "MIT", 13 | "keywords": [ 14 | "canvas", 15 | "html5", 16 | "nest" 17 | ], 18 | "main": "dist/canvas-nest.min.js", 19 | "repository": { 20 | "type": "git", 21 | "url": "https://github.com/hustcc/canvas-nest.js" 22 | }, 23 | "bugs": { 24 | "url": "https://github.com/hustcc/canvas-nest.js/issues" 25 | }, 26 | "devDependencies": { 27 | "gulp": "^3.9.0", 28 | "gulp-uglify": "^1.5.3", 29 | "jshint": "^2.9.2", 30 | "gulp-rename": "^1.2.2", 31 | "gulp-inject-version": "^1.0.1" 32 | }, 33 | "scripts": { 34 | "lint": "jshint src/canvas-nest.js", 35 | "test": "npm run lint", 36 | "build": "gulp mini && npm run test" 37 | }, 38 | "dependencies": { 39 | } 40 | } -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | canvas-nest.js 7 | 8 | 9 |
10 | 11 | 12 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /dist/canvas-nest.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 hustcc 3 | * License: MIT 4 | * Version: v1.0.1 5 | * GitHub: https://github.com/hustcc/canvas-nest.js 6 | **/ 7 | !function(){function n(n,e,t){return n.getAttribute(e)||t}function e(n){return document.getElementsByTagName(n)}function t(){var t=e("script"),o=t.length,i=t[o-1];return{l:o,z:n(i,"zIndex",-1),o:n(i,"opacity",.5),c:n(i,"color","0,0,0"),n:n(i,"count",99)}}function o(){a=m.width=window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth,c=m.height=window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight}function i(){r.clearRect(0,0,a,c);var n,e,t,o,m,l;s.forEach(function(i,x){for(i.x+=i.xa,i.y+=i.ya,i.xa*=i.x>a||i.x<0?-1:1,i.ya*=i.y>c||i.y<0?-1:1,r.fillRect(i.x-.5,i.y-.5,1,1),e=x+1;e=n.max/2&&(i.x-=.03*o,i.y-=.03*m),t=(n.max-l)/n.max,r.beginPath(),r.lineWidth=t/2,r.strokeStyle="rgba("+d.c+","+(t+.2)+")",r.moveTo(i.x,i.y),r.lineTo(n.x,n.y),r.stroke()))}),x(i)}var a,c,u,m=document.createElement("canvas"),d=t(),l="c_n"+d.l,r=m.getContext("2d"),x=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(n){window.setTimeout(n,1e3/45)},w=Math.random,y={x:null,y:null,max:2e4};m.id=l,m.style.cssText="position:fixed;top:0;left:0;z-index:"+d.z+";opacity:"+d.o,e("body")[0].appendChild(m),o(),window.onresize=o,window.onmousemove=function(n){n=n||window.event,y.x=n.clientX,y.y=n.clientY},window.onmouseout=function(){y.x=null,y.y=null};for(var s=[],f=0;d.n>f;f++){var h=w()*a,g=w()*c,v=2*w()-1,p=2*w()-1;s.push({x:h,y:g,xa:v,ya:p,max:6e3})}u=s.concat([y]),setTimeout(function(){i()},100)}(); -------------------------------------------------------------------------------- /README-zh.md: -------------------------------------------------------------------------------- 1 | # canvas-nest.js 2 | 3 | > 一个基于html5 canvas绘制的网页背景效果,非常赞!如果需要 `wordpress插件`,在插件库搜索 `canvas-nest` 或者看看项目 [canvas-nest-for-wp](https://github.com/aTool-org/canvas-nest-for-wp)。 4 | 5 | ![travis-ci](https://travis-ci.org/hustcc/canvas-nest.js.svg?branch=master) ![npm](https://img.shields.io/npm/v/canvas-nest.js.svg?style=flat-square) ![npm](https://img.shields.io/npm/l/canvas-nest.js.svg?style=flat-square) 6 | 7 | 8 | ## 特性 9 | 10 | - 不依赖任何框架或者内库,比如不依赖 jQuery,使用原生的 javascript。 11 | - 非常小,只有1.6 kb,如果开启 gzip,可以更小。 12 | - 非常容易实现,配置简单,即使你不是web开发者,也能简单搞定。 13 | 14 | 15 | ## 使用 16 | 17 | 使用非常简单,感觉都没有必要写这一节内容。 18 | 19 | 将下面的代码插入到 ` 和 之间`. 20 | 21 | ```html 22 | 23 | ``` 24 | 25 | 强烈建议在 ``标签上方. 例如下面的代码结构: 26 | 27 | ```html 28 | 29 | 30 | ... 31 | 32 | 33 | ... 34 | ... 35 | ... 36 | 37 | 38 | 39 | ``` 40 | 41 | `请注意不要将代码置于 里面`. 42 | 43 | 然后就完成了,打开网页即可看到效果! 44 | 45 | 46 | ## 配置和配置项 47 | 48 | - **`color`**: 线条颜色, 默认: `'0,0,0'` ;三个数字分别为(R,G,B),注意用,分割 49 | - **`opacity`**: 线条透明度(0~1), 默认: `0.5` 50 | - **`count`**: 线条的总数量, 默认: `150` 51 | - **`zIndex`**: 背景的z-index属性,css属性用于控制所在层的位置, 默认: `-1` 52 | 53 | 54 | Example: 55 | 56 | ``` 57 | 58 | ``` 59 | 60 | 这些属性配置在引用js的script标签中,作为它的一个属性值。所有的配置项都有默认值,如果你不知道怎么设置,可以先不设置这些配置项,就使用默认值看看效果也可以的。 61 | 62 | 63 | ## 示例 64 | 65 | 1. [在线工具: http://www.atool.org/](http://www.atool.org/) 66 | 2. [爱表情 aiBQ: https://aibq.cn/](https://aibq.cn/) 67 | 3. [夏敏的博客: http://jerey.cn/](http://jerey.cn/) 68 | 69 | 如果你使用的了本项目,也可以修改此处文件,并 pr,我会接受的。 70 | 71 | ![screenshot](https://raw.githubusercontent.com/hustcc/canvas-nest.js/master/screenshot.png) 72 | 73 | 74 | ## 其他 75 | 76 | 本项目的Javascript文件已经存储在CDN上,可以直接使用,地址为: [http://www.bootcdn.cn/canvas-nest.js/](http://www.bootcdn.cn/canvas-nest.js/),如果你不需要 CDN 或者有自己的 CDN,可以直接下载源码 dist 目录中的 `canvas-nest.min.js`,然后相应的修改使用地址即可。 77 | 78 | 有任何的bug或者建议,非常鼓励 issue 和 pr,来者不拒。 79 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # canvas-nest.js 2 | 3 | > A nest backgroud of website draw on canvas. [中文Readme帮助文档](https://github.com/hustcc/canvas-nest.js/blob/master/README-zh.md). For `wordpress plugin`, search `canvas-nest` or see [canvas-nest-for-wp](https://github.com/aTool-org/canvas-nest-for-wp). 4 | 5 | ![travis-ci](https://travis-ci.org/hustcc/canvas-nest.js.svg?branch=master) ![npm](https://img.shields.io/npm/v/canvas-nest.js.svg?style=flat-square) ![npm](https://img.shields.io/npm/l/canvas-nest.js.svg?style=flat-square) 6 | 7 | 8 | 9 | ## feature 10 | 11 | - do not depend on jQuery or other javascript framework. 12 | - very light, only 1.6 kb. can be smaller after gzip. 13 | - so easy to use, even you are not a web developer. 14 | 15 | 16 | ## usage 17 | 18 | so eazy that I do not want write the chapter. 19 | 20 | insert the code below `between and `. 21 | 22 | ```html 23 | 24 | ``` 25 | 26 | suggest before the tag ``. like below: 27 | 28 | ```html 29 | 30 | 31 | ... 32 | 33 | 34 | ... 35 | ... 36 | ... 37 | 38 | 39 | 40 | ``` 41 | 42 | 43 | `please do not add the code in the `. 44 | 45 | then ok! 46 | 47 | 48 | ## config 49 | 50 | - **`color`**: the canvas line color, default: `'0,0,0'` ; the color is (R,G,B) 51 | - **`opacity`**: the opacity of line (0~1), default: `0.5` 52 | - **`count`**: the number of lines, default: `150` 53 | - **`zIndex`**: the index of z space, default: `-1` 54 | 55 | Example: 56 | 57 | ```html 58 | 59 | ``` 60 | 61 | set the config on the script node `as a attribute`. all the config has the default value, you can choose to set any of them. 62 | 63 | 64 | ## preview 65 | 66 | 1. [OnlineTool: http://www.atool.org/](http://www.atool.org/) 67 | 2. [aiBQ: https://aibq.cn/](https://aibq.cn/) 68 | 3. [Jerey's Blog: http://jerey.cn/](http://jerey.cn/) 69 | 70 | if you has used this project, pls let me know, I can add your website on. 71 | 72 | ![screenshot](https://raw.githubusercontent.com/hustcc/canvas-nest.js/master/screenshot.png) 73 | 74 | 75 | ## other 76 | 77 | Project library cdn url: [http://www.bootcdn.cn/canvas-nest.js/](http://www.bootcdn.cn/canvas-nest.js/). 78 | 79 | any bug or question, welcome to push request and issue. 80 | -------------------------------------------------------------------------------- /dist/canvas-nest.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 hustcc 3 | * License: MIT 4 | * Version: v1.0.1 5 | * GitHub: https://github.com/hustcc/canvas-nest.js 6 | **/ 7 | ! function() { 8 | //封装方法,压缩之后减少文件大小 9 | function get_attribute(node, attr, default_value) { 10 | return node.getAttribute(attr) || default_value; 11 | } 12 | //封装方法,压缩之后减少文件大小 13 | function get_by_tagname(name) { 14 | return document.getElementsByTagName(name); 15 | } 16 | //获取配置参数 17 | function get_config_option() { 18 | var scripts = get_by_tagname("script"), 19 | script_len = scripts.length, 20 | script = scripts[script_len - 1]; //当前加载的script 21 | return { 22 | l: script_len, //长度,用于生成id用 23 | z: get_attribute(script, "zIndex", -1), //z-index 24 | o: get_attribute(script, "opacity", 0.5), //opacity 25 | c: get_attribute(script, "color", "0,0,0"), //color 26 | n: get_attribute(script, "count", 99) //count 27 | }; 28 | } 29 | //设置canvas的高宽 30 | function set_canvas_size() { 31 | canvas_width = the_canvas.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth, 32 | canvas_height = the_canvas.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; 33 | } 34 | 35 | //绘制过程 36 | function draw_canvas() { 37 | context.clearRect(0, 0, canvas_width, canvas_height); 38 | //随机的线条和当前位置联合数组 39 | var e, i, d, x_dist, y_dist, dist; //临时节点 40 | //遍历处理每一个点 41 | random_points.forEach(function(r, idx) { 42 | r.x += r.xa, 43 | r.y += r.ya, //移动 44 | r.xa *= r.x > canvas_width || r.x < 0 ? -1 : 1, 45 | r.ya *= r.y > canvas_height || r.y < 0 ? -1 : 1, //碰到边界,反向反弹 46 | context.fillRect(r.x - 0.5, r.y - 0.5, 1, 1); //绘制一个宽高为1的点 47 | //从下一个点开始 48 | for (i = idx + 1; i < all_array.length; i++) { 49 | e = all_array[i]; 50 | // 当前点存在 51 | if (null !== e.x && null !== e.y) { 52 | x_dist = r.x - e.x; //x轴距离 l 53 | y_dist = r.y - e.y; //y轴距离 n 54 | dist = x_dist * x_dist + y_dist * y_dist; //总距离, m 55 | 56 | dist < e.max && (e === current_point && dist >= e.max / 2 && (r.x -= 0.03 * x_dist, r.y -= 0.03 * y_dist), //靠近的时候加速 57 | d = (e.max - dist) / e.max, 58 | context.beginPath(), 59 | context.lineWidth = d / 2, 60 | context.strokeStyle = "rgba(" + config.c + "," + (d + 0.2) + ")", 61 | context.moveTo(r.x, r.y), 62 | context.lineTo(e.x, e.y), 63 | context.stroke()); 64 | } 65 | } 66 | }), frame_func(draw_canvas); 67 | } 68 | //创建画布,并添加到body中 69 | var the_canvas = document.createElement("canvas"), //画布 70 | config = get_config_option(), //配置 71 | canvas_id = "c_n" + config.l, //canvas id 72 | context = the_canvas.getContext("2d"), canvas_width, canvas_height, 73 | frame_func = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(func) { 74 | window.setTimeout(func, 1000 / 45); 75 | }, random = Math.random, 76 | current_point = { 77 | x: null, //当前鼠标x 78 | y: null, //当前鼠标y 79 | max: 20000 // 圈半径的平方 80 | }, 81 | all_array; 82 | the_canvas.id = canvas_id; 83 | the_canvas.style.cssText = "position:fixed;top:0;left:0;z-index:" + config.z + ";opacity:" + config.o; 84 | get_by_tagname("body")[0].appendChild(the_canvas); 85 | 86 | //初始化画布大小 87 | set_canvas_size(); 88 | window.onresize = set_canvas_size; 89 | //当时鼠标位置存储,离开的时候,释放当前位置信息 90 | window.onmousemove = function(e) { 91 | e = e || window.event; 92 | current_point.x = e.clientX; 93 | current_point.y = e.clientY; 94 | }, window.onmouseout = function() { 95 | current_point.x = null; 96 | current_point.y = null; 97 | }; 98 | //随机生成config.n条线位置信息 99 | for (var random_points = [], i = 0; config.n > i; i++) { 100 | var x = random() * canvas_width, //随机位置 101 | y = random() * canvas_height, 102 | xa = 2 * random() - 1, //随机运动方向 103 | ya = 2 * random() - 1; 104 | // 随机点 105 | random_points.push({ 106 | x: x, 107 | y: y, 108 | xa: xa, 109 | ya: ya, 110 | max: 6000 //沾附距离 111 | }); 112 | } 113 | all_array = random_points.concat([current_point]); 114 | //0.1秒后绘制 115 | setTimeout(function() { 116 | draw_canvas(); 117 | }, 100); 118 | }(); 119 | -------------------------------------------------------------------------------- /src/canvas-nest.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2016 hustcc 3 | * License: MIT 4 | * Version: %%GULP_INJECT_VERSION%% 5 | * GitHub: https://github.com/hustcc/canvas-nest.js 6 | **/ 7 | ! function() { 8 | //封装方法,压缩之后减少文件大小 9 | function get_attribute(node, attr, default_value) { 10 | return node.getAttribute(attr) || default_value; 11 | } 12 | //封装方法,压缩之后减少文件大小 13 | function get_by_tagname(name) { 14 | return document.getElementsByTagName(name); 15 | } 16 | //获取配置参数 17 | function get_config_option() { 18 | var scripts = get_by_tagname("script"), 19 | script_len = scripts.length, 20 | script = scripts[script_len - 1]; //当前加载的script 21 | return { 22 | l: script_len, //长度,用于生成id用 23 | z: get_attribute(script, "zIndex", -1), //z-index 24 | o: get_attribute(script, "opacity", 0.5), //opacity 25 | c: get_attribute(script, "color", "0,0,0"), //color 26 | n: get_attribute(script, "count", 99) //count 27 | }; 28 | } 29 | //设置canvas的高宽 30 | function set_canvas_size() { 31 | canvas_width = the_canvas.width = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth, 32 | canvas_height = the_canvas.height = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; 33 | } 34 | 35 | //绘制过程 36 | function draw_canvas() { 37 | context.clearRect(0, 0, canvas_width, canvas_height); 38 | //随机的线条和当前位置联合数组 39 | var e, i, d, x_dist, y_dist, dist; //临时节点 40 | //遍历处理每一个点 41 | random_points.forEach(function(r, idx) { 42 | r.x += r.xa, 43 | r.y += r.ya, //移动 44 | r.xa *= r.x > canvas_width || r.x < 0 ? -1 : 1, 45 | r.ya *= r.y > canvas_height || r.y < 0 ? -1 : 1, //碰到边界,反向反弹 46 | context.fillRect(r.x - 0.5, r.y - 0.5, 1, 1); //绘制一个宽高为1的点 47 | //从下一个点开始 48 | for (i = idx + 1; i < all_array.length; i++) { 49 | e = all_array[i]; 50 | // 当前点存在 51 | if (null !== e.x && null !== e.y) { 52 | x_dist = r.x - e.x; //x轴距离 l 53 | y_dist = r.y - e.y; //y轴距离 n 54 | dist = x_dist * x_dist + y_dist * y_dist; //总距离, m 55 | 56 | dist < e.max && (e === current_point && dist >= e.max / 2 && (r.x -= 0.03 * x_dist, r.y -= 0.03 * y_dist), //靠近的时候加速 57 | d = (e.max - dist) / e.max, 58 | context.beginPath(), 59 | context.lineWidth = d / 2, 60 | context.strokeStyle = "rgba(" + config.c + "," + (d + 0.2) + ")", 61 | context.moveTo(r.x, r.y), 62 | context.lineTo(e.x, e.y), 63 | context.stroke()); 64 | } 65 | } 66 | }), frame_func(draw_canvas); 67 | } 68 | //创建画布,并添加到body中 69 | var the_canvas = document.createElement("canvas"), //画布 70 | config = get_config_option(), //配置 71 | canvas_id = "c_n" + config.l, //canvas id 72 | context = the_canvas.getContext("2d"), canvas_width, canvas_height, 73 | frame_func = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(func) { 74 | window.setTimeout(func, 1000 / 60); 75 | }, random = Math.random, 76 | current_point = { 77 | x: null, //当前鼠标x 78 | y: null, //当前鼠标y 79 | max: 20000 // 圈半径的平方 80 | }, 81 | all_array; 82 | the_canvas.id = canvas_id; 83 | the_canvas.style.cssText = "position:fixed;top:0;left:0;z-index:" + config.z + ";opacity:" + config.o; 84 | get_by_tagname("body")[0].appendChild(the_canvas); 85 | 86 | //初始化画布大小 87 | set_canvas_size(); 88 | window.onresize = set_canvas_size; 89 | //当时鼠标位置存储,离开的时候,释放当前位置信息 90 | window.onmousemove = function(e) { 91 | e = e || window.event; 92 | current_point.x = e.clientX; 93 | current_point.y = e.clientY; 94 | }, window.onmouseout = function() { 95 | current_point.x = null; 96 | current_point.y = null; 97 | }; 98 | //随机生成config.n条线位置信息 99 | for (var random_points = [], i = 0; config.n > i; i++) { 100 | var x = random() * canvas_width, //随机位置 101 | y = random() * canvas_height, 102 | xa = 2 * random() - 1, //随机运动方向 103 | ya = 2 * random() - 1; 104 | // 随机点 105 | random_points.push({ 106 | x: x, 107 | y: y, 108 | xa: xa, 109 | ya: ya, 110 | max: 6000 //沾附距离 111 | }); 112 | } 113 | all_array = random_points.concat([current_point]); 114 | //0.1秒后绘制 115 | setTimeout(function() { 116 | frame_func(draw_canvas); 117 | }, 100); 118 | }(); 119 | --------------------------------------------------------------------------------