1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| import App from './App.vue' import { createRenderer, createApp,nextTick } from '@vue/runtime-dom';
let canvas; let ctx; const d2a = (n)=>{ return n * Math.PI / 180 }
const drawCircle = (start,end,color,cx,cy,r)=>{ let x = cx + Math.cos(d2a(start)) * r; let y = cy + Math.sin(d2a(start)) * r; ctx.beginPath(); ctx.moveTo(cx,cy); ctx.lineTo(x,y); ctx.arc(cx,cy,r,d2a(start),d2a(end)); ctx.fillStyle = color; ctx.fill(); ctx.stroke(); ctx.closePath(); } const draw = (el,noClear) => { if(!noClear){ ctx.clearRect(0,0,canvas.width,canvas.height); } if (el.tag == 'circle') { let { data, x, y, r } = el;
let total = data.reduce((memo, current) => memo + current.count, 0)
let start = 0, end = 0;
data.forEach(item => { end += item.count / total * 360; drawCircle(start, end, item.color, x, y, r); start = end; });
} if (el.tag === 'rect') { console.log('开始绘制矩形') } el.childs && el.childs.forEach(child => { draw(child,true) }); }
const ops = { insert: (child, parent, anchor) => { child.parent = parent; if (!parent.childs) { parent.childs = [child] } else { parent.childs.push(child); } if (parent.nodeType == 1) { draw(child);
if(child.onClick) { canvas.addEventListener('click',()=>{ child.onClick(); nextTick(()=>{ draw(child); }) }) } } }, remove: child => {}, createElement: (tag, isSVG, is) => { return { tag }; }, createText: text => {}, createComment: text => {}, setText: (node, text) => {}, setElementText: (el, text) => {}, parentNode: node => {}, nextSibling: node => {}, querySelector: selector => {}, setScopeId(el, id) {}, cloneNode(el) {}, insertStaticContent(content, parent, anchor, isSVG) {}, patchProp(el, key, prevValue, nextValue) { el[key] = nextValue;
} } const createCanvasApp = (...args) => { let app = createRenderer(ops).createApp(...args); let { mount } = app; app.mount = function(selector) { let el = document.querySelector(selector); canvas = document.createElement('canvas'); canvas.width = window.innerWidth; canvas.height = window.innerHeight; el.appendChild(canvas); ctx = canvas.getContext('2d') mount(canvas); } return app; } createCanvasApp(App).mount('#app');
|