动画的造成:先画出一幅图,改变其中的一些参数,从新绘制图片...不断的重复造成动画。javascript
class Branch { /** * 分枝类,如下为参数,都带有默认值 * 位置 position * 长度 length * 分支位置 divergeAt * 展开角度 angle * 层数 depth * 分支展开角度变化量 spread */ constructor(position = {x : 0, y: 0}, length = 100, divergeAt = 0.5, angle = 0, depth = 0, spread = 45 * TO_RADIAN) { this.position = position; this.length = length; this.divergeAt = divergeAt; this.angle = angle; this.depth = depth; this.color = '#000'; this.spread = spread; this.branches = []; this.grow(); } grow() { /** * 建立分支,若是canBranch = true(未达到最大分支数量) * 新分支长度为父级的0.75,深度加1 * 展开角度变化spread * 因为构造方法中调用了grow方法,因此会不断重复建立上述过程 */ if (!this.canBranch) { return; } this.branches = []; const nextLength = this.length * 0.75; const nextDepth = this.depth + 1; this.branches.push( new Branch( this.growPosition, nextLength, this.divergeAt, this.angle + this.spread, nextDepth, this.spread ), new Branch( this.growPosition, nextLength, this.divergeAt, this.angle - this.spread, nextDepth, this.spread ) ); } update(spread, divergeAt) { this.spread = spread; this.divergeAt = divergeAt; this.grow(); } get growPosition() { const dl = this.length * this.divergeAt; return { x: this.position.x + (Math.cos(this.angle) * dl), y: this.position.y + (Math.sin(this.angle) * dl), }; } get canBranch() { return this.depth < maxDepth; } }
/** * 保存当前状态 * 根据branch类中的数据画图(颜色,直线和圆点) * 递归对分支进行绘图 */ const drawBranch = (branch, phase) => { const h = ~~(200 + (160 * phase)); const l = 50 + ~~(((branch.depth / (maxDepth + 1))) * 50); const endX = branch.length; const endY = 0; const r = 2; ctx.save(); ctx.strokeStyle = `hsl(${h}, 100%, ${l}%)`; ctx.translate(branch.position.x, branch.position.y); ctx.rotate(branch.angle); ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo(endX, endY); ctx.stroke(); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = `hsl(${h}, 100%, 50%)`; ctx.arc(endX, endY, r, 0, PI * 2, false); ctx.fill(); ctx.closePath(); ctx.restore(); branch.branches.forEach((b) => { drawBranch(b, phase); }); };
const loop = () => { /** * 改变类中参数的值 * 将第一个canvas添加到在第二个canvas中循环12次每次旋转2PI/12的角度,造成一个环状 * 调用requestAnimationFrame() 从新根据类中的数据画图 */ let phase = rootBranch.spread / maxSpread; clear(phase); if (autoAnimate) { phase = map(Math.sin(autoTick), -1, 1, 0, 1); spread = phase * maxSpread; divergeAt = map(Math.sin(autoTick), -1, 1, 0, 0.5); autoTick += autoSpeed; } rootBranch.update(spread, divergeAt); drawBranch(rootBranch, phase); const numSegments = 12; const angleInc = PI * 2 / numSegments; let angle = tick; for (let i = 0; i < numSegments; i++) { ctx2.save(); ctx2.translate(midX, midY); ctx2.rotate(angle); ctx2.drawImage(canvas, -w / 2, -h / 2); ctx2.restore(); angle += angleInc; } tick += 0.002; requestAnimationFrame(loop); };
import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI);
须要安装 babel-plugin-component:css
npm install babel-plugin-component -D
在.babelrc中添加:vue
"plugins": [ "transform-vue-jsx", "transform-runtime", [ "component", { "libraryName": "element-ui", "styleLibraryName": "theme-chalk" } ] ]
import {Loading, Tabs, TabPane,} from 'element-ui'; Vue.use(Loading); Vue.use(Tabs); Vue.use(TabPane);
/* 改变主题色变量 */ $--color-primary: teal; /* 改变 icon 字体路径变量,必需 */ $--font-path: '~element-ui/lib/theme-chalk/fonts'; @import "~element-ui/packages/theme-chalk/src/index";
npm i element-theme -g
以后步骤为:java
在星盘改版的时候遇到了相关的问题,数据从父组件传递到子组件,当时没考虑到这方面的问题,只是对数据进行了一层的拷贝,由于当时传进来的数据是有不少层的,因此仍是会致使源数据改变。因此总结一下,写成一个方法,这个应该会比较经常使用。es6
Object属于引用类型,对它进行简单赋值(obj1 = obj2)的话只是建立一个指针指向原数据的地址,改变它的值也会改变源数据的值,会形成不少问题。npm
concat,slice 等方法,es6 新增运算符‘...’element-ui
思路是把对象拆开分别赋值,一样可使用es6 新增运算符‘...’,for循环等。canvas
运用递归逐层拷贝。数组
function depCopy(obj){ let value; if(typeof obj === "object"){ if(Object.prototype.toString.call(obj).slice(8,-1)==='Array'){ // debugger; value = []; for(let i = 0, length = obj.length; i<length; i++){ value.push(depCopy(obj[i])); } } else{ value = {}; for(let j in obj){ value[j] = depCopy(obj[j]); } } } else{ value = obj; } return value; }