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

假如只剩下Canvas標簽

網絡 通信技術
如果只剩下canvas標簽,該如何去繪制頁面中的內容呢?這也許是一個偽命題,但是用canvas確事能夠幫助完成很多事。今天就用canvas+AST語法樹構建一個信息流樣式。

[[420999]]

一、背景

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

二、繪制流程

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

2.1 基本元素

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

2.1.1 加載圖片

圖片是一個頁面中的靈魂元素,在頁面中占據絕大部分空間。

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

通過矩形元素能夠與文字等元素配合達到意想不到的效果。

  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 繪制圓

圓與矩形承擔的角色一致,也是在頁面中比較重要的角色。

  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抽象語法樹是源代碼語法結構的一種抽象表示。它以樹狀的形式表現編程語言的語法結構,樹上的每個節點都表示源代碼中的一種結構。例如,在Vue中,將模板語法轉換為AST抽象語法樹,然后再將抽象語法樹轉換為HTML結構,咱們在利用canvas繪制頁面時也利用AST抽象語法樹來表示頁面中的內容,實現的類型有rect(矩形)、img(圖片)、text(文字)、circle(圓)。

本次將繪制的內容包含靜態頁面部分和動畫部分,所以將利用兩個canvas實現,每個canvas將對應一個AST樹,分別為靜態部分AST樹和動態部分AST樹。

2.2.1 靜態部分AST樹

本次繪制的頁面中靜態部分的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: '灰太狼是最好的一頭狼,它每天都在夢想著吃羊,一直沒有實現,但是從不氣餒。' 
  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 動態部分AST樹

本次繪制的頁面中動畫部分的AST樹動態生成,由一系列動態顏色的圓組成。

  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 主函數類

除了上述一些基本元素類,將通過一個主函數類對外進行暴露。

  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.     // 繪制函數 
  10.     draw(ast) { 
  11.         ast.forEach(elementObj => { 
  12.             this.drawFactory(elementObj); 
  13.             const {children} = elementObj; 
  14.             // 遞歸調用 
  15.             if (children && Array.isArray(children)) { 
  16.                 this.draw(children); 
  17.             } 
  18.         }); 
  19.     } 
  20.  
  21.     // 工廠模型繪制對應基本元素 
  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 內容繪制

前面的準備工作已經完成,下面將各個函數和AST樹聯動起來,達到想要的效果。

2.4.1 靜態內容繪制

先將靜態部分的內容繪制好,作為頁面的基石。

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

靜態內容.png

2.4.2 繪制動畫跑馬燈

再給該部分內容來點動畫效果,更加激動人心。

  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(); 

本文轉載自微信公眾號「執鳶者」,可以通過以下二維碼關注。轉載本文請聯系執鳶者公眾號。

 

責任編輯:武曉燕 來源: 前端點線面
相關推薦

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

國產手機華為雷軍

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設計網絡

2019-11-18 10:34:24

戴爾

2021-03-26 06:00:37

編程語言CPU

2009-12-01 17:10:53

Linux版本

2021-03-25 16:01:11

編程語言CPU機器語言

2018-09-07 23:10:15

程序員技能溝通

2015-06-11 11:19:35

2020-08-11 14:47:40

iPhone蘋果APP

2019-03-28 10:09:49

內存CPU硬盤

2020-04-20 09:02:33

函數RPCCPU

2021-09-05 18:25:57

文件系統
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产传媒视频在线观看 | 人人人艹 | 日韩国产精品一区二区三区 | 97精品国产97久久久久久免费 | 国产成人免费视频网站高清观看视频 | 亚洲成人精 | 四虎影视免费观看 | 国产男女猛烈无遮掩视频免费网站 | 欧美aaaaaa| 日韩色在线 | 理论片免费在线观看 | 日本爱爱视频 | 精品欧美一区二区三区久久久小说 | 国产免费一区二区三区 | 午夜精品久久久久久久久久久久久 | 美国十次成人欧美色导视频 | 天堂中文字幕av | 作爱视频免费看 | 免费观看一级视频 | 91看片网址| 中文字幕成人网 | 中文字幕成人 | 精品国产一区二区三区日日嗨 | 国产精品亚洲精品日韩已方 | 久久精彩 | 天天曰夜夜 | 日韩在线不卡 | 国产999精品久久久久久 | 欧美日韩在线高清 | 91精品国产一区二区三区蜜臀 | 亚洲国产成人精品在线 | 欧美性视频在线播放 | 色婷婷国产精品 | 亚洲在线免费观看 | 国产午夜影院 | 免费播放一级片 | 国产精品18hdxxxⅹ在线 | 黄视频国产 | 久久成人精品视频 | 欧美精品一区二区在线观看 | 亚洲h视频 |