html,body { width: 100%; height: 100%; margin: 0; padding: 0; overflow: hidden; /*overflow属性指定如果内容溢出一个元素的框,会发生什么 其属性值hidden表示内容会被修建,并且其余内容是不可见的*/ /* overflow属性值: visible 默认值,内容不会被修剪,会呈现在元素框之外 hidden 内容会被修剪,并且其余内容是不可见的 scroll 内容会被修剪,但是浏览器会显示滚动条以便查看其他内容 auto 若内容被修剪,则浏览器会显示滚动条 inherit 规定应该从父元素继承overflow属性的值 注意:overflow属性只工作于指定高度的块元素上 */ } .container{ width: 100%; height: 100%; margin: 0; padding: 0; background-color: #000000; } //绘制图形 var RENDERER = { PARTICLE_COUNT : 1000, PARTICLE_RADIUS : 1, MAX_ROTATION_ANGLE : Math.PI / 60, TRANSLATION_COUNT : 10000, //函数体汇总 init : function(strategy){ this.setParameters(strategy); this.createParticles(); this.setupFigure(); this.reconstructMethod(); this.bindEvent(); this.drawFigure(); }, //窗口响应事件,图形大小控制 setParameters : function(strategy){ this.$window = $(window); //调用html中的样式 this.$container = $('#jsi-particle-container'); this.width = this.$container.width(); this.height = this.$container.height(); this.$canvas = $('').attr({width : this.width, height : this.height}).appendTo(this.$container); this.context = this.$canvas.get(0).getContext('2d');//2D动画 this.center = {x : this.width / 2, y : this.height / 2}; //rotation控制旋转 MAX_ROTATION_ANGLE随机旋转 this.rotationX = this.MAX_ROTATION_ANGLE; this.rotationY = this.MAX_ROTATION_ANGLE; //strategyIndex战略索引 this.strategyIndex = 0; //translationCount动画过渡数量 影响点击颜色变化的深浅度 this.translationCount = 0; this.theta = 0; this.strategies = strategy.getStrategies(); this.particles = []; }, createParticles : function(){ //循环输出 for(var i = 0; i < this.PARTICLE_COUNT; i ++){ this.particles.push(new PARTICLE(this.center)); } }, //绑定事件bind reconstructMethod : function(){ this.setupFigure = this.setupFigure.bind(this);//建立 this.drawFigure = this.drawFigure.bind(this);//绘制 this.changeAngle = this.changeAngle.bind(this);//改变 }, bindEvent : function(){ this.$container.on('click', this.setupFigure);//点击事件 this.$container.on('mousemove', this.changeAngle);//鼠标响应事件 }, changeAngle : function(event){ var offset = this.$container.offset(), //offset方法返回或设置匹配元素相对于文档的偏移(位置) x = event.clientX - offset.left + this.$window.scrollLeft(),//clientX js鼠标事件 y = event.clientY - offset.top + this.$window.scrollTop(); this.rotationX = (this.center.y - y) / this.center.y * this.MAX_ROTATION_ANGLE; this.rotationY = (this.center.x - x) / this.center.x * this.MAX_ROTATION_ANGLE; }, setupFigure : function(){ for(var i = 0, length = this.particles.length; i < length; i++){ this.particles[i].setAxis(this.strategies[this.strategyIndex]()); } if(++this.strategyIndex == this.strategies.length){ this.strategyIndex = 0; } this.translationCount = 0; }, drawFigure : function(){ requestAnimationFrame(this.drawFigure); this.context.fillStyle = 'rgba(0, 0, 0, 0.2)'; this.context.fillRect(0, 0, this.width, this.height); for(var i = 0, length = this.particles.length; i < length; i++){ var axis = this.particles[i].getAxis2D(this.theta); this.context.beginPath(); this.context.fillStyle = axis.color; this.context.arc(axis.x, axis.y, this.PARTICLE_RADIUS, 0, Math.PI * 2, false); this.context.fill(); } this.theta++; this.theta %= 360; for(var i = 0, length = this.particles.length; i < length; i++){ this.particles[i].rotateX(this.rotationX); this.particles[i].rotateY(this.rotationY); } this.translationCount++; this.translationCount %= this.TRANSLATION_COUNT; if(this.translationCount == 0){ this.setupFigure(); } } }; var STRATEGY = { SCATTER_RADIUS :200, CONE_ASPECT_RATIO : 4, RING_COUNT : 8, getStrategies : function(){ var strategies = []; //校验 for(var i in this){ if(this[i] == arguments.callee || typeof this[i] != 'function'){ continue; } strategies.push(this[i].bind(this)); } return strategies; }, //随机数绘制图形 createSphere : function(){ var cosTheta = Math.random() * 2 - 1, sinTheta = Math.sqrt(1 - cosTheta * cosTheta), phi = Math.random() * 2 * Math.PI; return { x : this.SCATTER_RADIUS * sinTheta * Math.cos(phi), y : this.SCATTER_RADIUS * sinTheta * Math.sin(phi), z : this.SCATTER_RADIUS * cosTheta, hue : Math.round(phi / Math.PI * 30) }; }, createTorus : function(){ var theta = Math.random() * Math.PI * 2, x = this.SCATTER_RADIUS + this.SCATTER_RADIUS / 6 * Math.cos(theta), y = this.SCATTER_RADIUS / 6 * Math.sin(theta), phi = Math.random() * Math.PI * 2; return { x : x * Math.cos(phi), y : y, z : x * Math.sin(phi), hue : Math.round(phi / Math.PI * 30) }; }, createCone : function(){ var status = Math.random() > 1 / 3, x, y, phi = Math.random() * Math.PI * 2, rate = Math.tan(30 / 180 * Math.PI) / this.CONE_ASPECT_RATIO; if(status){ y = this.SCATTER_RADIUS * (1 - Math.random() * 2); x = (this.SCATTER_RADIUS - y) * rate; }else{ y = -this.SCATTER_RADIUS; x = this.SCATTER_RADIUS * 2 * rate * Math.random(); } return { x : x * Math.cos(phi), y : y, z : x * Math.sin(phi), hue : Math.round(phi / Math.PI * 30) }; }, createVase : function(){ var theta = Math.random() * Math.PI, x = Math.abs(this.SCATTER_RADIUS * Math.cos(theta) / 2) + this.SCATTER_RADIUS / 8, y = this.SCATTER_RADIUS * Math.cos(theta) * 1.2, phi = Math.random() * Math.PI * 2; return { x : x * Math.cos(phi), y : y, z : x * Math.sin(phi), hue : Math.round(phi / Math.PI * 30) }; } }; var PARTICLE = function(center){ this.center = center; this.init(); }; PARTICLE.prototype = { SPRING : 0.01, FRICTION : 0.9, FOCUS_POSITION : 300, COLOR : 'hsl(%hue, 100%, 50%)',//HSL值,在CSS中,可以使用色相、饱和度和明度(HSL)来指定颜色 //不太懂init : function含义 init : function(){ this.x = 0; this.y = 0; this.z = 0; this.vx = 0; this.vy = 0; this.vz = 0; this.color; }, setAxis : function(axis){ this.translating = true; this.nextX = axis.x; this.nextY = axis.y; this.nextZ = axis.z; this.hue = axis.hue; }, rotateX : function(angle){ var sin = Math.sin(angle), cos = Math.cos(angle), nextY = this.nextY * cos - this.nextZ * sin, nextZ = this.nextZ * cos + this.nextY * sin, y = this.y * cos - this.z * sin, z = this.z * cos + this.y * sin; this.nextY = nextY; this.nextZ = nextZ; this.y = y; this.z = z; }, rotateY : function(angle){ var sin = Math.sin(angle), cos = Math.cos(angle), nextX = this.nextX * cos - this.nextZ * sin, nextZ = this.nextZ * cos + this.nextX * sin, x = this.x * cos - this.z * sin, z = this.z * cos + this.x * sin; this.nextX = nextX; this.nextZ = nextZ; this.x = x; this.z = z; }, rotateZ : function(angle){ var sin = Math.sin(angle), cos = Math.cos(angle), nextX = this.nextX * cos - this.nextY * sin, nextY = this.nextY * cos + this.nextX * sin, x = this.x * cos - this.y * sin, y = this.y * cos + this.x * sin; this.nextX = nextX; this.nextY = nextY; this.x = x; this.y = y; }, getAxis3D : function(){ this.vx += (this.nextX - this.x) * this.SPRING; this.vy += (this.nextY - this.y) * this.SPRING; this.vz += (this.nextZ - this.z) * this.SPRING; this.vx *= this.FRICTION; this.vy *= this.FRICTION; this.vz *= this.FRICTION; this.x += this.vx; this.y += this.vy; this.z += this.vz; return {x : this.x, y : this.y, z : this.z}; }, getAxis2D : function(theta){ var axis = this.getAxis3D(), scale = this.FOCUS_POSITION / (this.FOCUS_POSITION + axis.z); return {x : this.center.x + axis.x * scale, y : this.center.y - axis.y * scale, color : this.COLOR.replace('%hue', this.hue + theta)}; } }; $(function(){ RENDERER.init(STRATEGY); });
该代码来源于菜鸟教程的jQuery教程中的实例,其中添加了个人对于不解知识点的注释分享
另外还有关于代码中init : function(){}部分,不太理解,诚请广大码友解答
|