前端知識梳理-1/Canvas

HTML5 <canvas> 元素用于圖形的繪制,通過腳本 (通常是JavaScript)來完成.<canvas> 標(biāo)簽只是圖形容器,您必須使用腳本來繪制圖形。

  • 創(chuàng)建畫布
<canvas id="myCanvas" width="400" height="200">
你的瀏覽器不支持canvas!
</canvas>
//上面代碼中,如果瀏覽器不支持這個API,則就會顯示<canvas>標(biāo)簽中間的文字——“您de瀏覽器不支持canvas!”。
  • 每個canvas節(jié)點都有一個對應(yīng)的context對象(上下文對象),Canvas API定義在這個context對象上面,所以需要獲取這個對象,方法是使用getContext方法。
var canvas = document.getElementById('myCanvas');

if (canvas.getContext) {
  var ctx = canvas.getContext('2d');
}

//上面代碼中,getContext方法指定參數(shù)2d,表示該canvas節(jié)點用于生成2D圖案(即平面圖案)。如果參數(shù)是webgl,就表示用于生成3D圖像(即立體圖案),這部分實際上單獨叫做WebGL API。

繪圖方法

(1)繪制路徑

//beginPath方法表示開始繪制路徑,moveTo(x, y)方法設(shè)置線段的起點,lineTo(x, y)方法設(shè)置線段的終點,stroke方法用來給透明的線段著色。

ctx.beginPath(); // 開始路徑繪制
ctx.moveTo(20, 20); // 設(shè)置路徑起點,坐標(biāo)為(20,20)
ctx.lineTo(200, 20); // 繪制一條到(200,20)的直線
ctx.lineWidth = 1.0; // 設(shè)置線寬
ctx.strokeStyle = '#CC0000'; // 設(shè)置線的顏色
ctx.stroke(); // 進(jìn)行線的著色,這時整條線才變得可見

//moveto和lineto方法可以多次使用。最后,還可以使用closePath方法,自動繪制一條當(dāng)前點到起點的直線,形成一個封閉圖形,省卻使用一次lineto方法。

(2)繪制矩形

//fillRect(x, y, width, height)方法用來繪制矩形,它的四個參數(shù)分別為矩形左上角頂點的x坐標(biāo)、y坐標(biāo),以及矩形的寬和高。fillStyle屬性用來設(shè)置矩形的填充色。

ctx.fillStyle = 'yellow';
ctx.fillRect(50, 50, 200, 100); 
strokeRect方法與fillRect類似,用來繪制空心矩形。
ctx.strokeRect(10,10,200,100); 
clearRect方法用來清除某個矩形區(qū)域的內(nèi)容。
ctx.clearRect(100,50,50,50); 


(3)繪制文本

//fillText(string, x, y) 用來繪制文本,它的三個參數(shù)分別為文本內(nèi)容、起點的x坐標(biāo)、y坐標(biāo)。使用之前,需用font設(shè)置字體、大小、樣式(寫法類似與CSS的font屬性)。與此類似的還有strokeText方法,用來添加空心字。

// 設(shè)置字體
ctx.font = "Bold 20px Arial"; 
// 設(shè)置對齊方式
ctx.textAlign = "left";
// 設(shè)置填充顏色
ctx.fillStyle = "#008600"; 
// 設(shè)置字體內(nèi)容,以及在畫布上的位置
ctx.fillText("Hello!", 10, 50); 
// 繪制空心字
ctx.strokeText("Hello!", 10, 100); 

//fillText方法不支持文本斷行,即所有文本出現(xiàn)在一行內(nèi)。所以,如果要生成多行文本,只有調(diào)用多次fillText方法。

(4)繪制圓形和扇形

//arc方法用來繪制扇形

ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
arc方法的x和y參數(shù)是圓心坐標(biāo),radius是半徑,startAngle和endAngle則是扇形的起始角度和終止角度(以弧度表示),anticlockwise表示做圖時應(yīng)該逆時針畫(true)還是順時針畫(false)。
下面是如何繪制實心的圓形。
ctx.beginPath(); 
ctx.arc(60, 60, 50, 0, Math.PI*2, true); 
ctx.fillStyle = "#000000"; 
ctx.fill();
繪制空心圓形的例子。
ctx.beginPath(); 
ctx.arc(60, 60, 50, 0, Math.PI*2, true); 
ctx.lineWidth = 1.0; 
ctx.strokeStyle = "#000"; 
ctx.stroke();

(5)設(shè)置漸變色

//createLinearGradient方法用來設(shè)置漸變色。

var myGradient = ctx.createLinearGradient(0, 0, 0, 160); 

myGradient.addColorStop(0, "#BABABA"); 

myGradient.addColorStop(1, "#636363");
createLinearGradient方法的參數(shù)是(x1, y1, x2, y2),其中x1和y1是起點坐標(biāo),x2和y2是終點坐標(biāo)。通過不同的坐標(biāo)值,可以生成從上至下、從左到右的漸變等等。
使用方法如下:
ctx.fillStyle = myGradient;
ctx.fillRect(10,10,200,100);


(6)設(shè)置陰影

//一系列與陰影相關(guān)的方法,可以用來設(shè)置陰影。

ctx.shadowOffsetX = 10; // 設(shè)置水平位移
ctx.shadowOffsetY = 10; // 設(shè)置垂直位移
ctx.shadowBlur = 5; // 設(shè)置模糊度
ctx.shadowColor = "rgba(0,0,0,0.5)"; // 設(shè)置陰影顏色

ctx.fillStyle = "#CC0000"; 
ctx.fillRect(10,10,200,100);

圖像處理方法

drawImage方法

//Canvas API 允許將圖像文件插入畫布,做法是讀取圖片后,使用drawImage方法在畫布內(nèi)進(jìn)行重繪。

var img = new Image();
img.src = 'image.png';
ctx.drawImage(img, 0, 0); // 設(shè)置對應(yīng)的圖像對象,以及它在畫布上的位置

上面代碼將一個PNG圖像載入畫布。drawImage()方法接受三個參數(shù),第一個參數(shù)是圖像文件的DOM元素(即<img>節(jié)點),第二個和第三個參數(shù)是圖像左上角在畫布中的坐標(biāo),上例中的(0, 0)就表示將圖像左上角放置在畫布的左上角。
由于圖像的載入需要時間,drawImage方法只能在圖像完全載入后才能調(diào)用,因此上面的代碼需要

var image = new Image();

image.onload = function() {
  var canvas = document.createElement('canvas');
  canvas.width = image.width;
  canvas.height = image.height;
  canvas.getContext('2d').drawImage(image, 0, 0);
  // 插入頁面底部
  document.body.appendChild(image);
  return canvas;
}

image.src = 'image.png';


getImageData方法,putImageData方法

//getImageData方法可以用來讀取Canvas的內(nèi)容,返回一個對象,包含了每個像素的信息。

var imageData = context.getImageData(0, 0, canvas.width, canvas.height);

imageData對象有一個data屬性,它的值是一個一維數(shù)組。該數(shù)組的值,依次是每個像素的紅、綠、藍(lán)、alpha通道值,因此該數(shù)組的長度等于 圖像的像素寬度 x 圖像的像素高度 x 4,每個值的范圍是0–255。這個數(shù)組不僅可讀,而且可寫,因此通過操作這個數(shù)組的值,就可以達(dá)到操作圖像的目的。修改這個數(shù)組以后,使用putImageData方法將數(shù)組內(nèi)容重新繪制在Canvas上。

context.putImageData(imageData, 0, 0);

toDataURL方法

//對圖像數(shù)據(jù)做出修改以后,可以使用toDataURL方法,將Canvas數(shù)據(jù)重新轉(zhuǎn)化成一般的圖像文件形式。

function convertCanvasToImage(canvas) {
  var image = new Image();
  image.src = canvas.toDataURL('image/png');
  return image;
}
//上面的代碼將Canvas數(shù)據(jù),轉(zhuǎn)化成PNG data URI。

save方法,restore方法

//save方法用于保存上下文環(huán)境,restore方法用于恢復(fù)到上一次保存的上下文環(huán)境。

ctx.save();

ctx.shadowOffsetX = 10;
ctx.shadowOffsetY = 10;
ctx.shadowBlur = 5;
ctx.shadowColor = 'rgba(0,0,0,0.5)';

ctx.fillStyle = '#CC0000';
ctx.fillRect(10,10,150,100);

ctx.restore();

ctx.fillStyle = '#000000';
ctx.fillRect(180,10,150,100);

//上面代碼先用save方法,保存了當(dāng)前設(shè)置,然后繪制了一個有陰影的矩形。接著,使用restore方法,恢復(fù)了保存前的設(shè)置,繪制了一個沒有陰影的矩形。


動畫

//利用JavaScript,可以在canvas元素上很容易地產(chǎn)生動畫效果。

var posX = 20,
    posY = 100;

setInterval(function() {
    context.fillStyle = "black";
    context.fillRect(0,0,canvas.width, canvas.height);

    posX += 1;
    posY += 0.25;

    context.beginPath();
    context.fillStyle = "white";

    context.arc(posX, posY, 10, 0, Math.PI*2, true); 
    context.closePath();
    context.fill();
}, 30);


上面代碼會產(chǎn)生一個小圓點,每隔30毫秒就向右下方移動的效果。setInterval函數(shù)的一開始,之所以要將畫布重新渲染黑色底色,是為了抹去上一步的小圓點。

通過設(shè)置圓心坐標(biāo),可以產(chǎn)生各種運動軌跡。

先上升后下降。

var vx = 10,
    vy = -10,
    gravity = 1;

setInterval(function() {
    posX += vx;
    posY += vy;
    vy += gravity;
    // ...
});

上面代碼中,x坐標(biāo)始終增大,表示持續(xù)向右運動。y坐標(biāo)先變小,然后在重力作用下,不斷增大,表示先上升后下降。

小球不斷反彈后,逐步趨于靜止。

var vx = 10,
    vy = -10,
    gravity = 1;

setInterval(function() {
    posX += vx;
    posY += vy;

    if (posY > canvas.height * 0.75) {
          vy *= -0.6;
          vx *= 0.75;
          posY = canvas.height * 0.75;
    }
    
    vy += gravity;
    // ...
});

上面代碼表示,一旦小球的y坐標(biāo)處于屏幕下方75%的位置,向x軸移動的速度變?yōu)樵瓉淼?5%,而向y軸反彈上一次反彈高度的40%。

像素處理

通過getImageData方法和putImageData方法,可以處理每個像素,進(jìn)而操作圖像內(nèi)容。

假定filter是一個處理像素的函數(shù),那么整個對Canvas的處理流程,可以用下面的代碼表示。
if (canvas.width > 0 && canvas.height > 0) {

    var imageData = context.getImageData(0, 0, canvas.width, canvas.height);

    filter(imageData);

    context.putImageData(imageData, 0, 0);

}

以下是幾種常見的處理方法。
灰度效果

灰度圖(grayscale)就是取紅、綠、藍(lán)三個像素值的算術(shù)平均值,這實際上將圖像轉(zhuǎn)成了黑白形式。假定d[i]是像素數(shù)組中一個象素的紅色值,則d[i+1]為綠色值,d[i+2]為藍(lán)色值,d[i+3]就是alpha通道值。轉(zhuǎn)成灰度的算法,就是將紅、綠、藍(lán)三個值相加后除以3,再將結(jié)果寫回數(shù)組。

grayscale = function (pixels) {

    var d = pixels.data;

    for (var i = 0; i < d.length; i += 4) {
      var r = d[i];
      var g = d[i + 1];
      var b = d[i + 2];
      d[i] = d[i + 1] = d[i + 2] = (r+g+b)/3;
    }

    return pixels;

};

復(fù)古效果

//復(fù)古效果(sepia)則是將紅、綠、藍(lán)三個像素,分別取這三個值的某種加權(quán)平均值,使得圖像有一種古舊的效果。

sepia = function (pixels) {

    var d = pixels.data;

    for (var i = 0; i < d.length; i += 4) {
      var r = d[i];
      var g = d[i + 1];
      var b = d[i + 2];
      d[i]     = (r * 0.393)+(g * 0.769)+(b * 0.189); // red
      d[i + 1] = (r * 0.349)+(g * 0.686)+(b * 0.168); // green
      d[i + 2] = (r * 0.272)+(g * 0.534)+(b * 0.131); // blue
    }

    return pixels;

};

紅色蒙版效果

//紅色蒙版指的是,讓圖像呈現(xiàn)一種偏紅的效果。算法是將紅色通道設(shè)為紅、綠、藍(lán)三個值的平均值,而將綠色通道和藍(lán)色通道都設(shè)為0。

red = function (pixels) {
    
    var d = pixels.data;

    for (var i = 0; i < d.length; i += 4) {
      var r = d[i];
      var g = d[i + 1];
      var b = d[i + 2];
      d[i] = (r+g+b)/3;        // 紅色通道取平均值
      d[i + 1] = d[i + 2] = 0; // 綠色通道和藍(lán)色通道都設(shè)為0
    }

    return pixels;

};

亮度效果

//亮度效果(brightness)是指讓圖像變得更亮或更暗。算法將紅色通道、綠色通道、藍(lán)色通道,同時加上一個正值或負(fù)值。

brightness = function (pixels, delta) {

    var d = pixels.data;

    for (var i = 0; i < d.length; i += 4) {
          d[i] += delta;     // red
          d[i + 1] += delta; // green
          d[i + 2] += delta; // blue   
    }

    return pixels;

};

反轉(zhuǎn)效果

//反轉(zhuǎn)效果(invert)是指圖片呈現(xiàn)一種色彩顛倒的效果。算法為紅、綠、藍(lán)通道都取各自的相反值(255-原值)。

invert = function (pixels) {

    var d = pixels.data;

    for (var i = 0; i < d.length; i += 4) {
        d[i] = 255 - d[i];
        d[i+1] = 255 - d[i + 1];
        d[i+2] = 255 - d[i + 2];
    }

    return pixels;

};

vue 動畫小組件

<template>
  <canvas ref="bubble" :width="width" :height="height" :style="style"></canvas>
</template>

<script type="text/ecmascript-6">
  export default {
    props: {
      y: {
        type: Number,
        default: 0
      }
    },
    data() {
      return {
        width: 50,
        height: 80
      }
    },
    computed: {
      distance() {
        return Math.max(0, Math.min(this.y * this.ratio, this.maxDistance))
      },
      style() {
        return `width:${this.width / this.ratio}px;height:${this.height / this.ratio}px`
      }
    },
    created() {
      this.ratio = window.devicePixelRatio
      this.width *= this.ratio
      this.height *= this.ratio
      this.initRadius = 18 * this.ratio
      this.minHeadRadius = 12 * this.ratio
      this.minTailRadius = 5 * this.ratio
      this.initArrowRadius = 10 * this.ratio
      this.minArrowRadius = 6 * this.ratio
      this.arrowWidth = 3 * this.ratio
      this.maxDistance = 40 * this.ratio
      this.initCenterX = 25 * this.ratio
      this.initCenterY = 25 * this.ratio
      this.headCenter = {
        x: this.initCenterX,
        y: this.initCenterY
      }
    },
    mounted() {
      this._draw()
    },
    methods: {
      _draw() {
        const bubble = this.$refs.bubble
        let ctx = bubble.getContext('2d')
        ctx.clearRect(0, 0, bubble.width, bubble.height)

        this._drawBubble(ctx)

        this._drawArrow(ctx)
      },
      _drawBubble(ctx) {
        ctx.save()
        ctx.beginPath()

        const rate = this.distance / this.maxDistance
        const headRadius = this.initRadius - (this.initRadius - this.minHeadRadius) * rate

        this.headCenter.y = this.initCenterY - (this.initRadius - this.minHeadRadius) * rate

        // 畫上半弧線
        ctx.arc(this.headCenter.x, this.headCenter.y, headRadius, 0, Math.PI, true)

        // 畫左側(cè)貝塞爾
        const tailRadius = this.initRadius - (this.initRadius - this.minTailRadius) * rate
        const tailCenter = {
          x: this.headCenter.x,
          y: this.headCenter.y + this.distance
        }

        const tailPointL = {
          x: tailCenter.x - tailRadius,
          y: tailCenter.y
        }
        const controlPointL = {
          x: tailPointL.x,
          y: tailPointL.y - this.distance / 2
        }

        ctx.quadraticCurveTo(controlPointL.x, controlPointL.y, tailPointL.x, tailPointL.y)

        // 畫下半弧線
        ctx.arc(tailCenter.x, tailCenter.y, tailRadius, Math.PI, 0, true)

        // 畫右側(cè)貝塞爾
        const headPointR = {
          x: this.headCenter.x + headRadius,
          y: this.headCenter.y
        }
        const controlPointR = {
          x: tailCenter.x + tailRadius,
          y: headPointR.y + this.distance / 2
        }
        ctx.quadraticCurveTo(controlPointR.x, controlPointR.y, headPointR.x, headPointR.y)

        ctx.fillStyle = 'rgb(170,170,170)'
        ctx.fill()
        ctx.strokeStyle = 'rgb(153,153,153)'
        ctx.stroke()
        ctx.restore()
      },
      _drawArrow(ctx) {
        ctx.save()
        ctx.beginPath()

        const rate = this.distance / this.maxDistance
        const arrowRadius = this.initArrowRadius - (this.initArrowRadius - this.minArrowRadius) * rate

        // 畫內(nèi)圓
        ctx.arc(this.headCenter.x, this.headCenter.y, arrowRadius - (this.arrowWidth - rate), -Math.PI / 2, 0, true)

        // 畫外圓
        ctx.arc(this.headCenter.x, this.headCenter.y, arrowRadius, 0, Math.PI * 3 / 2, false)

        ctx.lineTo(this.headCenter.x, this.headCenter.y - arrowRadius - this.arrowWidth / 2 + rate)
        ctx.lineTo(this.headCenter.x + this.arrowWidth * 2 - rate * 2, this.headCenter.y - arrowRadius + this.arrowWidth / 2)

        ctx.lineTo(this.headCenter.x, this.headCenter.y - arrowRadius + this.arrowWidth * 3 / 2 - rate)

        ctx.fillStyle = 'rgb(255,255,255)'
        ctx.fill()
        ctx.strokeStyle = 'rgb(170,170,170)'
        ctx.stroke()
        ctx.restore()
      }
    },
    watch: {
      y() {
        this._draw()
      }
    }
  }
</script>

<style scoped>

</style>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,488評論 6 531
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,034評論 3 414
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事?!?“怎么了?”我有些...
    開封第一講書人閱讀 175,327評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,554評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 71,337評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 54,883評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,975評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,114評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,625評論 1 332
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 40,555評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,737評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,244評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 43,973評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,615評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,343評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 47,699評論 2 370

推薦閱讀更多精彩內(nèi)容

  • 【Android 自定義View之繪圖】 基礎(chǔ)圖形的繪制 一、Paint與Canvas 繪圖需要兩個工具,筆和紙。...
    Rtia閱讀 11,709評論 5 34
  • 一:canvas簡介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標(biāo)簽 ②:HTML5 ...
    GreenHand1閱讀 4,702評論 2 32
  • ??HTML5 添加的最受歡迎的功能就是 元素。這個元素負(fù)責(zé)在頁面中設(shè)定一個區(qū)域,然后就可以通過 JavaScri...
    霜天曉閱讀 3,041評論 0 2
  • 一、簡介 HTML5 中的定義:“它是依賴分辨率的位圖畫布,你可以在 canvas 上面繪制任何圖形,甚至加載照片...
    destiny0904閱讀 10,564評論 1 4
  • 我們趨之若鶩的美食,真的好吃好喝嗎?如果只是滿足視覺的嘩眾取寵、眾人皆知我不知的好奇心,這些經(jīng)由口舌最后通向胃里的...
    樂曦閱讀 572評論 0 3