動畫的造成:先畫出一幅圖,改變其中的一些參數,從新繪製圖片...不斷的重複造成動畫。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; }