├── .gitignore ├── screenshot.png ├── src ├── index.js ├── iife.js ├── utils.js └── CanvasNest.js ├── .babelrc ├── lib ├── index.js ├── iife.js ├── utils.js └── CanvasNest.js ├── index.d.ts ├── rollup.config.iife.js ├── rollup.config.umd.js ├── LICENSE ├── index.html ├── package.json ├── README-zh.md ├── README.md └── dist ├── canvas-nest.umd.js └── canvas-nest.js /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .settings 3 | .idea 4 | node_modules/* 5 | *.lock 6 | *.log 7 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itning/canvas-nest.js/master/screenshot.png -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hustcc on 18/6/23. 3 | * Contract: i@hust.cc 4 | */ 5 | 6 | import CanvasNest from './CanvasNest'; 7 | 8 | export default CanvasNest; 9 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "rollup": { 4 | "presets": [ 5 | [ 6 | "env", 7 | { 8 | "modules": false 9 | } 10 | ] 11 | ], 12 | "plugins": [ 13 | "transform-class-properties", 14 | "transform-object-rest-spread", 15 | "version" 16 | ] 17 | }, 18 | "babel": { 19 | "presets": ["env"], 20 | "plugins": [ 21 | "transform-class-properties", 22 | "add-module-exports", 23 | "transform-object-rest-spread", 24 | "version" 25 | ] 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _CanvasNest = require('./CanvasNest'); 8 | 9 | var _CanvasNest2 = _interopRequireDefault(_CanvasNest); 10 | 11 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 12 | 13 | exports.default = _CanvasNest2.default; /** 14 | * Created by hustcc on 18/6/23. 15 | * Contract: i@hust.cc 16 | */ 17 | 18 | module.exports = exports['default']; -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare module 'canvas-nest.js' { 2 | interface configType { 3 | /** 4 | * 线条颜色, 默认: '0,0,0' ;三个数字分别为(R,G,B),注意用,分割 5 | */ 6 | color?: string; 7 | 8 | /** 9 | * 线条的总数量, 默认: 150 10 | */ 11 | count?: number; 12 | 13 | /** 14 | * 背景的z-index属性,css属性用于控制所在层的位置, 默认: -1 15 | */ 16 | zIndex?: number; 17 | 18 | /** 19 | * 线条透明度(0~1), 默认: 0.5 20 | */ 21 | opacity?: number; 22 | } 23 | 24 | class CanvasNest { 25 | constructor(element: Element, config?: configType) 26 | destroy(): void; 27 | } 28 | 29 | export default CanvasNest; 30 | } -------------------------------------------------------------------------------- /src/iife.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hustcc on 18/6/23. 3 | * Contract: i@hust.cc 4 | */ 5 | 6 | import CanvasNest from './CanvasNest'; 7 | 8 | const getScriptConfig = () => { 9 | const scripts = document.getElementsByTagName('script'); 10 | const len = scripts.length; 11 | const script = scripts[len - 1]; // 当前加载的script 12 | return { 13 | zIndex: script.getAttribute('zIndex'), 14 | opacity: script.getAttribute('opacity'), 15 | color: script.getAttribute('color'), 16 | pointColor: script.getAttribute('pointColor'), 17 | count: Number(script.getAttribute('count')) || 99, 18 | }; 19 | }; 20 | 21 | new CanvasNest(document.body, getScriptConfig()); 22 | -------------------------------------------------------------------------------- /rollup.config.iife.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hustcc on 18/6/23. 3 | * Contract: i@hust.cc 4 | */ 5 | 6 | import uglify from 'rollup-plugin-uglify'; 7 | import babel from 'rollup-plugin-babel'; 8 | import resolve from 'rollup-plugin-node-resolve'; 9 | import commonjs from 'rollup-plugin-commonjs'; 10 | 11 | export default { 12 | input: 'src/iife.js', 13 | output: { 14 | file: 'dist/canvas-nest.js', 15 | format: 'iife', 16 | }, 17 | plugins: [ 18 | resolve(), 19 | babel({ 20 | exclude: 'node_modules/**', 21 | }), 22 | commonjs(), 23 | uglify({ 24 | output: { comments: false }, 25 | compress: { warnings: false } 26 | }), 27 | ], 28 | external: [], 29 | }; 30 | -------------------------------------------------------------------------------- /rollup.config.umd.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hustcc on 18/6/23. 3 | * Contract: i@hust.cc 4 | */ 5 | 6 | import uglify from 'rollup-plugin-uglify'; 7 | import babel from 'rollup-plugin-babel'; 8 | import resolve from 'rollup-plugin-node-resolve'; 9 | import commonjs from 'rollup-plugin-commonjs'; 10 | 11 | export default { 12 | input: 'src/index.js', 13 | output: { 14 | file: 'dist/canvas-nest.umd.js', 15 | name: 'CanvasNest', 16 | format: 'umd', 17 | }, 18 | plugins: [ 19 | resolve(), 20 | babel({ 21 | exclude: 'node_modules/**', 22 | }), 23 | commonjs(), 24 | uglify({ 25 | output: { comments: false }, 26 | compress: { warnings: false } 27 | }), 28 | ], 29 | external: [], 30 | }; 31 | -------------------------------------------------------------------------------- /lib/iife.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var _CanvasNest = require('./CanvasNest'); 4 | 5 | var _CanvasNest2 = _interopRequireDefault(_CanvasNest); 6 | 7 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 8 | 9 | var getScriptConfig = function getScriptConfig() { 10 | var scripts = document.getElementsByTagName('script'); 11 | var len = scripts.length; 12 | var script = scripts[len - 1]; // 当前加载的script 13 | return { 14 | zIndex: script.getAttribute('zIndex'), 15 | opacity: script.getAttribute('opacity'), 16 | color: script.getAttribute('color'), 17 | pointColor: script.getAttribute('pointColor'), 18 | count: Number(script.getAttribute('count')) || 99 19 | }; 20 | }; /** 21 | * Created by hustcc on 18/6/23. 22 | * Contract: i@hust.cc 23 | */ 24 | 25 | new _CanvasNest2.default(document.body, getScriptConfig()); -------------------------------------------------------------------------------- /src/utils.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by hustcc on 18/6/23. 3 | * Contract: i@hust.cc 4 | */ 5 | 6 | export const requestAnimationFrame = window.requestAnimationFrame || 7 | window.webkitRequestAnimationFrame || 8 | window.mozRequestAnimationFrame || 9 | window.msRequestAnimationFrame || 10 | window.oRequestAnimationFrame || 11 | function(func) { 12 | return window.setTimeout(func, 1000 / 60); 13 | }; 14 | 15 | export const cancelAnimationFrame = window.cancelAnimationFrame || 16 | window.webkitCancelAnimationFrame || 17 | window.mozCancelAnimationFrame || 18 | window.msCancelAnimationFrame || 19 | window.oCancelAnimationFrame || 20 | window.clearTimeout; 21 | 22 | export const range = n => 23 | new Array(n).fill(0).map((e, idx) => idx); 24 | 25 | export const canvasStyle = config => 26 | `display:block;position:absolute;top:0;left:0;height:100%;width:100%;overflow:hidden;pointer-events:none;z-index:${config.zIndex};opacity:${config.opacity}`; 27 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | /** 7 | * Created by hustcc on 18/6/23. 8 | * Contract: i@hust.cc 9 | */ 10 | 11 | var requestAnimationFrame = exports.requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.oRequestAnimationFrame || function (func) { 12 | return window.setTimeout(func, 1000 / 60); 13 | }; 14 | 15 | var cancelAnimationFrame = exports.cancelAnimationFrame = window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.msCancelAnimationFrame || window.oCancelAnimationFrame || window.clearTimeout; 16 | 17 | var range = exports.range = function range(n) { 18 | return new Array(n).fill(0).map(function (e, idx) { 19 | return idx; 20 | }); 21 | }; 22 | 23 | var canvasStyle = exports.canvasStyle = function canvasStyle(config) { 24 | return "display:block;position:absolute;top:0;left:0;height:100%;width:100%;overflow:hidden;pointer-events:none;z-index:" + config.zIndex + ";opacity:" + config.opacity; 25 | }; -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 |`标签上方. 例如下面的代码结构: 40 | 41 | ```html 42 | 43 |
44 | ... 45 | 46 |
51 |