自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

[[420999]]

一、背景

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

二、繪制流程

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

2.1 基本元素

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

2.1.1 加載圖片

圖片是一個頁面中的靈魂元素,在頁面中占據(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 繪制文字

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

  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 繪制矩形

通過矩形元素能夠與文字等元素配合達(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)的角色一致,也是在頁面中比較重要的角色。

  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樹

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

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

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

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

  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: '灰太狼是最好的一頭狼,它每天都在夢想著吃羊,一直沒有實現(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 動態(tài)部分AST樹

本次繪制的頁面中動畫部分的AST樹動態(tài)生成,由一系列動態(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ù)類

除了上述一些基本元素類,將通過一個主函數(shù)類對外進(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.     // 工廠模型繪制對應(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)完成,下面將各個函數(shù)和AST樹聯(lián)動起來,達(dá)到想要的效果。

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

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

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

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

2.4.2 繪制動畫跑馬燈

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

  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)載自微信公眾號「執(zhí)鳶者」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系執(zhí)鳶者公眾號。

 

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

2011-08-29 10:02:27

iPadaPad亞馬遜

2020-05-19 13:58:55

私有云容器虛擬化

2024-01-08 09:11:24

編程語言歐洲

2018-07-11 15:31:24

程序員Java編程

2025-03-21 11:02:20

2019-05-15 14:42:56

國產(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è)計網(wǎng)絡(luò)

2019-11-18 10:34:24

戴爾

2009-12-01 17:10:53

Linux版本

2021-03-26 06:00:37

編程語言CPU

2021-03-25 16:01:11

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

2018-09-07 23:10:15

程序員技能溝通

2011-08-11 09:16:50

JavaScript

2011-08-12 08:56:31

JavaScript

2015-06-11 11:19:35

2020-04-20 09:02:33

函數(shù)RPCCPU

2020-08-11 14:47:40

iPhone蘋果APP
點(diǎn)贊
收藏

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