成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

假如只剩下Canvas標(biāo)簽

網(wǎng)絡(luò) 通信技術(shù)
如果只剩下canvas標(biāo)簽,該如何去繪制頁(yè)面中的內(nèi)容呢?這也許是一個(gè)偽命題,但是用canvas確事能夠幫助完成很多事。今天就用canvas+AST語(yǔ)法樹(shù)構(gòu)建一個(gè)信息流樣式。

[[420999]]

一、背景

如果只剩下canvas標(biāo)簽,該如何去繪制頁(yè)面中的內(nèi)容呢?這也許是一個(gè)偽命題,但是用canvas確事能夠幫助完成很多事。今天就用canvas+AST語(yǔ)法樹(shù)構(gòu)建一個(gè)信息流樣式。

二、繪制流程

將整個(gè)繪制流程分為三部分:基本元素、AST語(yǔ)法樹(shù)、主函數(shù)類。基本元素指的是圖片、文字、矩形、圓等;AST語(yǔ)法樹(shù)在本處值得就是包含一些屬性的js對(duì)象;主函數(shù)類指對(duì)外暴露的接口,通過(guò)調(diào)用實(shí)現(xiàn)最終繪制。

2.1 基本元素

不管多么復(fù)雜的事物肯定都是由一系列簡(jiǎn)單的元素組成,例如汽車肯定是通過(guò)一些簡(jiǎn)單的機(jī)械零配件組成;電腦也是通過(guò)電阻、電容等零配件組成。網(wǎng)頁(yè)也不例外,也是通過(guò)文字、圖片、矩形等組成。

2.1.1 加載圖片

圖片是一個(gè)頁(yè)面中的靈魂元素,在頁(yè)面中占據(jù)絕大部分空間。

  1. class DrawImage { 
  2.     constructor(ctx, imageObj) { 
  3.         this.ctx = ctx; 
  4.         this.imageObj = imageObj; 
  5.     } 
  6.  
  7.     draw() { 
  8.         const {centerX, centerY, src, sx = 1, sy = 1} = this.imageObj; 
  9.         const img = new Image(); 
  10.         img.onload = () => { 
  11.             const imgWidth = img.width; 
  12.             const imgHeight = img.height; 
  13.             this.ctx.save(); 
  14.             this.ctx.scale(sx, sy); 
  15.             this.ctx.drawImage(img, centerX - imgWidth * sx / 2, centerY - imgHeight * sy / 2); 
  16.             this.ctx.restore(); 
  17.         }; 
  18.         img.src = src; 
  19.     } 

2.1.2 繪制文字

文字能夠提高頁(yè)面的可讀性,讓觀察該頁(yè)面的每一個(gè)人都能夠快速了解該頁(yè)面的思想。

  1. class DrawText { 
  2.     constructor(ctx, textObj) { 
  3.         this.ctx = ctx; 
  4.         this.textObj = textObj; 
  5.     } 
  6.  
  7.     draw() { 
  8.         const {x, y, font, content, lineHeight = 20, width, fillStyle = '#000000', textAlign = 'start', textBaseline = 'middle'} = this.textObj; 
  9.         const branchsContent = this.getBranchsContent(content, width); 
  10.         this.ctx.save(); 
  11.         this.ctx.fillStyle = fillStyle; 
  12.         this.ctx.textAlign = textAlign; 
  13.         this.ctx.textBaseline = textBaseline; 
  14.         this.ctx.font = font; 
  15.         branchsContent.forEach((branchContent, index) => { 
  16.             this.ctx.fillText(branchContent, x, y + index * lineHeight); 
  17.         }); 
  18.         this.ctx.restore(); 
  19.     } 
  20.  
  21.     getBranchsContent(content, width) { 
  22.         if (!width) { 
  23.             return [content]; 
  24.         } 
  25.         const charArr = content.split(''); 
  26.         const branchsContent = []; 
  27.         let tempContent = ''
  28.         charArr.forEach(char => { 
  29.             if (this.ctx.measureText(tempContent).width < width && this.ctx.measureText(tempContent + char).width <= width) { 
  30.                 tempContent += char
  31.             } 
  32.             else { 
  33.                 branchsContent.push(tempContent); 
  34.                 tempContent = ''
  35.             } 
  36.         }); 
  37.         branchsContent.push(tempContent); 
  38.         return branchsContent; 
  39.     } 

2.1.3 繪制矩形

通過(guò)矩形元素能夠與文字等元素配合達(dá)到意想不到的效果。

  1. class DrawRect { 
  2.     constructor(ctx, rectObj) { 
  3.         this.ctx = ctx; 
  4.         this.rectObj = rectObj; 
  5.     } 
  6.  
  7.     draw() { 
  8.         const {x, y, width, height, fillStyle, lineWidth = 1} = this.rectObj; 
  9.         this.ctx.save(); 
  10.         this.ctx.fillStyle = fillStyle; 
  11.         this.ctx.lineWidth = lineWidth; 
  12.         this.ctx.fillRect(x, y, width, height); 
  13.         this.ctx.restore(); 
  14.     } 

2.1.4 繪制圓

圓與矩形承擔(dān)的角色一致,也是在頁(yè)面中比較重要的角色。

  1. class DrawCircle { 
  2.     constructor(ctx, circleObj) { 
  3.         this.ctx = ctx; 
  4.         this.circleObj = circleObj; 
  5.     } 
  6.  
  7.     draw() { 
  8.         const {x, y, R, startAngle = 0, endAngle = Math.PI * 2, lineWidth = 1, fillStyle} = this.circleObj; 
  9.         this.ctx.save(); 
  10.         this.ctx.lineWidth = lineWidth; 
  11.         this.ctx.fillStyle = fillStyle; 
  12.         this.ctx.beginPath(); 
  13.         this.ctx.arc(x, y, R, startAngle, endAngle); 
  14.         this.ctx.closePath(); 
  15.         this.ctx.fill(); 
  16.         this.ctx.restore(); 
  17.     } 

2.2 AST樹(shù)

AST抽象語(yǔ)法樹(shù)是源代碼語(yǔ)法結(jié)構(gòu)的一種抽象表示。它以樹(shù)狀的形式表現(xiàn)編程語(yǔ)言的語(yǔ)法結(jié)構(gòu),樹(shù)上的每個(gè)節(jié)點(diǎn)都表示源代碼中的一種結(jié)構(gòu)。例如,在Vue中,將模板語(yǔ)法轉(zhuǎn)換為AST抽象語(yǔ)法樹(shù),然后再將抽象語(yǔ)法樹(shù)轉(zhuǎn)換為HTML結(jié)構(gòu),咱們?cè)诶胏anvas繪制頁(yè)面時(shí)也利用AST抽象語(yǔ)法樹(shù)來(lái)表示頁(yè)面中的內(nèi)容,實(shí)現(xiàn)的類型有rect(矩形)、img(圖片)、text(文字)、circle(圓)。

本次將繪制的內(nèi)容包含靜態(tài)頁(yè)面部分和動(dòng)畫部分,所以將利用兩個(gè)canvas實(shí)現(xiàn),每個(gè)canvas將對(duì)應(yīng)一個(gè)AST樹(shù),分別為靜態(tài)部分AST樹(shù)和動(dòng)態(tài)部分AST樹(shù)。

2.2.1 靜態(tài)部分AST樹(shù)

本次繪制的頁(yè)面中靜態(tài)部分的AST樹(shù)如下所示,包含矩形、圖片、文字。

  1. const graphicAst = [ 
  2.     { 
  3.         type: 'rect'
  4.         x: 0, 
  5.         y: 0, 
  6.         width: 1400, 
  7.         height: 400, 
  8.         fillStyle: '#cec9ae' 
  9.     }, 
  10.     { 
  11.         type: 'img'
  12.         centerX: 290, 
  13.         centerY: 200, 
  14.         sx: 0.9, 
  15.         sy: 0.9, 
  16.         src: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Finews.gtimg.com%2Fnewsapp_match%2F0%2F11858683821%2F0.jpg&refer=http%3A%2F%2Finews.gtimg.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1622015341&t=cc1bd95777dfa37d88c48bb6e179778e' 
  17.     }, 
  18.     { 
  19.         type: 'text'
  20.         x: 600, 
  21.         y: 60, 
  22.         textAlign: 'start'
  23.         textBaseline: 'middle'
  24.         font: 'normal 40px serif'
  25.         lineHeight: 50, 
  26.         width: 180, 
  27.         fillStyle: '#000000'
  28.         content: '灰太狼是最好的一頭狼,它每天都在夢(mèng)想著吃羊,一直沒(méi)有實(shí)現(xiàn),但是從不氣餒。' 
  29.     }, 
  30.     { 
  31.         type: 'text'
  32.         x: 600, 
  33.         y: 170, 
  34.         textAlign: 'start'
  35.         textBaseline: 'middle'
  36.         font: 'normal 30px serif'
  37.         lineHeight: 50, 
  38.         width: 180, 
  39.         fillStyle: '#7F7F7F'
  40.         content: '為灰太狼加油、為灰太狼喝彩,😄' 
  41.     }, 
  42.     { 
  43.         type: 'text'
  44.         x: 1200, 
  45.         y: 360, 
  46.         textAlign: 'start'
  47.         textBaseline: 'ideographic'
  48.         font: 'normal 30px serif'
  49.         lineHeight: 50, 
  50.         width: 180, 
  51.         fillStyle: '#949494'
  52.         content: '閱讀' 
  53.     }, 
  54.     { 
  55.         type: 'text'
  56.         x: 1260, 
  57.         y: 363, 
  58.         textAlign: 'start'
  59.         textBaseline: 'ideographic'
  60.         font: 'normal 30px serif'
  61.         lineHeight: 50, 
  62.         width: 180, 
  63.         fillStyle: '#949494'
  64.         content: '520' 
  65.     } 
  66. ]; 

2.2.2 動(dòng)態(tài)部分AST樹(shù)

本次繪制的頁(yè)面中動(dòng)畫部分的AST樹(shù)動(dòng)態(tài)生成,由一系列動(dòng)態(tài)顏色的圓組成。

  1. function getMarqueeAst(startX, endX, count, options = {}) { 
  2.     const {y = 15, R = 15} = options; 
  3.     if (!(endX >= startX && count > 0)) { 
  4.         return []; 
  5.     } 
  6.     const interval = (endX - startX) / count
  7.     const marqueeAstArr = []; 
  8.     for (let i = 0; i < count; i++) { 
  9.         const RValue = Math.random() * 255; 
  10.         const GValue = Math.random() * 255; 
  11.         const BValue = Math.random() * 255; 
  12.         const fillStyle = `rgb(${RValue}, ${GValue}, ${BValue})`; 
  13.         marqueeAstArr.push({ 
  14.             type: 'circle'
  15.             x: startX + i * interval, 
  16.             y, 
  17.             R, 
  18.             fillStyle 
  19.         }); 
  20.     } 
  21.  
  22.     return marqueeAstArr; 

2.3 主函數(shù)類

除了上述一些基本元素類,將通過(guò)一個(gè)主函數(shù)類對(duì)外進(jìn)行暴露。

  1. class Draw { 
  2.     constructor(canvasDom) { 
  3.         this._canvasDom = canvasDom; 
  4.         this.ctx = this._canvasDom.getContext('2d'); 
  5.         this.width = this._canvasDom.width; 
  6.         this.height = this._canvasDom.height; 
  7.     } 
  8.  
  9.     // 繪制函數(shù) 
  10.     draw(ast) { 
  11.         ast.forEach(elementObj => { 
  12.             this.drawFactory(elementObj); 
  13.             const {children} = elementObj; 
  14.             // 遞歸調(diào)用 
  15.             if (children && Array.isArray(children)) { 
  16.                 this.draw(children); 
  17.             } 
  18.         }); 
  19.     } 
  20.  
  21.     // 工廠模型繪制對(duì)應(yīng)基本元素 
  22.     drawFactory(elementObj) { 
  23.         const {type} = elementObj; 
  24.         switch(type) { 
  25.             case 'img': { 
  26.                 this.drawImage(elementObj); 
  27.                 break; 
  28.             } 
  29.             case 'text': { 
  30.                 this.drawText(elementObj); 
  31.                 break; 
  32.             } 
  33.             case 'rect': { 
  34.                 this.drawRect(elementObj); 
  35.                 break; 
  36.             } 
  37.             case 'circle': { 
  38.                 this.drawCircle(elementObj); 
  39.                 break; 
  40.             } 
  41.         } 
  42.     } 
  43.  
  44.     drawImage(imageObj) { 
  45.         const drawImage = new DrawImage(this.ctx, imageObj); 
  46.         drawImage.draw(); 
  47.     } 
  48.  
  49.     drawText(textObj) { 
  50.         const drawText = new DrawText(this.ctx, textObj); 
  51.         drawText.draw(); 
  52.     } 
  53.  
  54.     drawRect(rectObj) { 
  55.         const drawRect = new DrawRect(this.ctx, rectObj); 
  56.         drawRect.draw(); 
  57.     } 
  58.  
  59.     drawCircle(circleObj) { 
  60.         const drawCircle = new DrawCircle(this.ctx, circleObj); 
  61.         drawCircle.draw(); 
  62.     } 
  63.  
  64.     clearCanvas() { 
  65.         this.ctx.clearRect(0, 0, this.width, this.height); 
  66.     } 

2.4 內(nèi)容繪制

前面的準(zhǔn)備工作已經(jīng)完成,下面將各個(gè)函數(shù)和AST樹(shù)聯(lián)動(dòng)起來(lái),達(dá)到想要的效果。

2.4.1 靜態(tài)內(nèi)容繪制

先將靜態(tài)部分的內(nèi)容繪制好,作為頁(yè)面的基石。

  1. const basicCanvasDom = document.getElementById('basicCanvas'); 
  2. const drawBasicInstance = new Draw(basicCanvasDom); 
  3. drawBasicInstance.draw(graphicAst); 

靜態(tài)內(nèi)容.png

2.4.2 繪制動(dòng)畫跑馬燈

再給該部分內(nèi)容來(lái)點(diǎn)動(dòng)畫效果,更加激動(dòng)人心。

  1. const animationCanvasDom = document.getElementById('animationCanvas'); 
  2. const drawAnimationInstance = new Draw(animationCanvasDom); 
  3.  
  4. let renderCount = 0; 
  5. function animate() { 
  6.     if (renderCount % 5 === 0) { 
  7.         drawAnimationInstance.clearCanvas(); 
  8.         drawAnimationInstance.draw(getMarqueeAst(20, 1440, 22)); 
  9.         drawAnimationInstance.draw(getMarqueeAst(20, 1440, 22, { 
  10.             y: 380 
  11.         })); 
  12.     } 
  13.     window.requestAnimationFrame(animate); 
  14.     renderCount++; 
  15. animate(); 

本文轉(zhuǎn)載自微信公眾號(hào)「執(zhí)鳶者」,可以通過(guò)以下二維碼關(guān)注。轉(zhuǎn)載本文請(qǐng)聯(lián)系執(zhí)鳶者公眾號(hào)。

 

責(zé)任編輯:武曉燕 來(lái)源: 前端點(diǎn)線面
相關(guān)推薦

2011-08-29 10:02:27

iPadaPad亞馬遜

2020-05-19 13:58:55

私有云容器虛擬化

2024-01-08 09:11:24

編程語(yǔ)言歐洲

2018-07-11 15:31:24

程序員Java編程

2025-03-21 11:02:20

2019-05-15 14:42:56

國(guó)產(chǎn)手機(jī)華為雷軍

2023-01-31 11:06:01

模型算力

2017-11-30 16:23:46

逆向華為云工程師

2014-08-26 10:42:26

CIO

2020-12-23 13:22:14

Kubernetes設(shè)計(jì)網(wǎng)絡(luò)

2019-11-18 10:34:24

戴爾

2021-03-26 06:00:37

編程語(yǔ)言CPU

2009-12-01 17:10:53

Linux版本

2021-03-25 16:01:11

編程語(yǔ)言CPU機(jī)器語(yǔ)言

2018-09-07 23:10:15

程序員技能溝通

2015-06-11 11:19:35

2020-08-11 14:47:40

iPhone蘋果APP

2020-04-20 09:02:33

函數(shù)RPCCPU

2019-03-28 10:09:49

內(nèi)存CPU硬盤

2021-09-05 18:25:57

文件系統(tǒng)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 久久99精品久久久久久琪琪 | 国产乱一区二区三区视频 | 亚洲区一区二区 | 一级黄色裸片 | 国产精品日韩在线观看 | 欧美天堂| 色综合天天天天做夜夜夜夜做 | 日韩在线视频一区二区三区 | 日本粉嫩一区二区三区视频 | 伊人网伊人 | 欧美一级免费 | 亚洲成人一区二区 | 四虎最新 | 中文一区 | 日韩欧美三区 | 在线观看中文字幕一区二区 | 香蕉国产在线视频 | 亚洲手机视频在线 | 一区二区av| 91久久婷婷 | 成人亚洲| 精品国产免费人成在线观看 | 91精品一区| 91秦先生艺校小琴 | 麻豆一区二区三区 | 久久国产日韩 | 波多野结衣一区二区三区 | 99热成人在线 | 欧美日韩在线播放 | 久久国产综合 | 一区二区中文字幕 | www.久久 | 手机在线一区二区三区 | 91在线免费视频 | 国产成人在线视频播放 | 国产一区二区三区四区五区加勒比 | 国产色片| 国产精品亚洲精品日韩已方 | 精品综合久久久 | 在线播放中文字幕 | 成人高潮片免费视频欧美 |