超多經(jīng)典 canvas 實例,動態(tài)離子背景、移動炫彩小球、貪吃蛇、坦克大戰(zhàn)、是男人就下100層、心形文字等等等

超多經(jīng)典 canvas 實例

普及<canvas> 元素用于在網(wǎng)頁上繪制圖形。這是一個圖形容器,您可以控制其每一像素,必須使用腳本來繪制圖形。

注意:IE 8 以及更早的版本不支持 <canvas> 元素。

貼士:全部例子都分享在我的 GayHub - https://github.com/bxm0927/canvas-special

尤雨溪個人主頁炫彩三角紐帶效果,點擊還可變換

GitHub源碼 、 Demo演示

知乎登錄注冊頁動態(tài)離子背景效果

GitHub源碼 、 Demo演示

基于 canvas 的五子棋完整功能實現(xiàn)

GitHub源碼 、 Demo演示

基于 canvas 的《是男人就下100層》小游戲完美實現(xiàn)

GitHub源碼Demo演示

毛筆字書寫田字格,可以寫字

GitHub源碼 、 Demo演示

隨心而動,隨刃而行。輸入文字顯示動畫粒子特效

GitHub源碼Demo演示

鼠標(biāo)移動炫彩小球

GitHub源碼 、 Demo演示

2048

GitHub源碼 、 Demo演示

貪吃蛇

GitHub源碼 、 Demo演示

看你有多色

GitHub源碼Demo演示

坦克大戰(zhàn)

GitHub源碼 、 Demo演示

宇宙行星旋轉(zhuǎn)特效

GitHub源碼 、 Demo演示

支付寶咻咻咻動畫特效

GitHub源碼 、 Demo演示

程序員表白代碼

GitHub源碼Demo演示

心形文字

GitHub源碼 、 Demo演示

照片墻

GitHub源碼Demo演示

License

The code is available under the MIT license.

<marquee>不斷更新,歡迎補充!</marquee>

!

canvas 簡介

<canvas> 元素用于在網(wǎng)頁上繪制圖形。這是一個圖形容器,您可以控制其每一像素,必須使用腳本來繪制圖形。

<canvas> 標(biāo)記和 SVG 以及 VML 之間的一個重要的不同是,<canvas> 有一個基于 JavaScript 的繪圖 API,而 SVG 和 VML 使用一個 XML 文檔來描述繪圖。

注意:IE 8 以及更早的版本不支持 <canvas> 元素。

!

canvas 初體驗

<canvas id="myCanvas" width="450" height="450">
  Your browser does not support the Canvas API, Please upgrade your browser.
</canvas>

<script>
let myCanvas = document.getElementById('myCanvas');
let ctx = myCanvas.getContext('2d');

ctx.moveTo(100, 100);
ctx.lineTo(200, 200);
ctx.lineTo(200, 300);

ctx.stroke();
</script>

canvas 核心 API 講解

建議大家看官方文檔來系統(tǒng)的學(xué)習(xí) canvas API,本文下面的例子只是對知識點的鞏固。

顏色、樣式和陰影

fillStyle、strokeStyle

fillStyle 屬性設(shè)置或返回用于填充繪畫的顏色、漸變或模式。

strokeStyle 屬性設(shè)置或返回用于筆觸的顏色、漸變或模式。

// 用藍色填充矩形
ctx.fillStyle="#0000ff";
ctx.fillRect(20,20,150,100);

// 漸變填充
var my_gradient=ctx.createLinearGradient(0,0,0,170);
my_gradient.addColorStop(0,"black");
my_gradient.addColorStop(1,"white");
ctx.fillStyle=my_gradient;
ctx.fillRect(20,20,150,100);

// 圖像填充
var img=document.getElementById("lamp");
var pat=ctx.createPattern(img,"repeat");
ctx.rect(0,0,150,100);
ctx.fillStyle=pat;
ctx.fill();

!

!

shadowBlur、shadowColor

shadowBlur 設(shè)置或返回用于陰影的模糊級別

shadowColor 設(shè)置或返回用于陰影的顏色

注釋1:請將 shadowColor 屬性與 shadowBlur 屬性一起使用,來創(chuàng)建陰影。

注釋2:請通過使用 shadowOffsetX 和 shadowOffsetY 屬性來調(diào)節(jié)陰影效果。

ctx.shadowBlur=20;
ctx.shadowColor="black";
ctx.fillStyle="blue";
ctx.fillRect(20,20,100,80);

!

createLinearGradient()、createRadialGradient()

context.createLinearGradient(x0,y0,x1,y1) 創(chuàng)建線性漸變

context.createRadialGradient(x0,y0,r0,x1,y1,r1) 創(chuàng)建放射狀/環(huán)形的漸變

注釋:addColorStop(stop,color) 方法與 createLinearGradient()createRadialGradient() 一起使用。

var my_gradient=ctx.createLinearGradient(0,0,0,170);
my_gradient.addColorStop(0,"black");
my_gradient.addColorStop(0.5,"red");
my_gradient.addColorStop(1,"white");
ctx.fillStyle=my_gradient;
ctx.fillRect(20,20,150,100);

!
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,100);

!
context.createPattern()

context.createPattern(image,"repeat|repeat-x|repeat-y|no-repeat") 重復(fù)繪制元素,元素可以是圖片、視頻,或者其他 <canvas> 元素。

var img=document.getElementById("lamp");
var pat=ctx.createPattern(img,"repeat");
ctx.rect(0,0,150,100);
ctx.fillStyle=pat;
ctx.fill();

!

線條樣式

lineCap

context.lineCap="butt|round|square"

設(shè)置或返回線條的結(jié)束端點樣式 (平直的邊緣(默認(rèn))、圓形線帽、正方形線帽)

lineJoin

context.lineJoin="miter|bevel|round"

設(shè)置或返回兩條線相交時,所創(chuàng)建的拐角類型 (尖角(默認(rèn))、斜角、圓角)

ctx.beginPath();
ctx.lineJoin="round";
ctx.moveTo(20,20);
ctx.lineTo(100,50);
ctx.lineTo(20,100);
ctx.stroke();

!

lineWidth

ctx.lineWidth = 10

設(shè)置或返回當(dāng)前的線條寬度,單位 px

矩形

rect()、fillRect()、strokeRect()

context.rect(x,y,width,height) 創(chuàng)建矩形

context.fillRect(x,y,width,height) 創(chuàng)建已填色的矩形,默認(rèn)的填充顏色是黑色。

context.strokeRect(x,y,width,height) 創(chuàng)建不填色的矩形,默認(rèn)的筆觸顏色是黑色。

// 紅色矩形
ctx.beginPath();
ctx.lineWidth="6";
ctx.strokeStyle="red";
ctx.rect(5,5,290,140);
ctx.stroke();

clearRect()

clearRect() 在給定的矩形內(nèi)清除指定的像素

// 在給定矩形內(nèi)清空一個矩形
ctx.fillStyle="red";
ctx.fillRect(0,0,300,150);
ctx.clearRect(20,20,100,50);

!

路徑

提示:請使用這些方法來創(chuàng)建路徑:moveTo()、lineTo()、quadricCurveTo()、bezierCurveTo()、arcTo() 以及 arc()。

fill()

填充當(dāng)前的圖像(路徑)。默認(rèn)顏色是黑色。

提示:請使用 fillStyle 屬性來填充另一種顏色/漸變。

注釋:如果路徑未關(guān)閉,那么 fill() 方法會從路徑結(jié)束點到開始點之間添加一條線,以關(guān)閉該路徑,然后填充該路徑。

// 繪制 150*100 像素的矩形,然后用綠色來給它填色:
ctx.rect(20,20,150,100);
ctx.fillStyle="green";
ctx.fill();

!

stroke()

stroke() 方法會繪制出通過 moveTo() 和 lineTo() 方法定義的路徑。默認(rèn)顏色是黑色。

提示:請使用 strokeStyle 屬性來繪制另一種顏色/漸變。

beginPath()

beginPath() 起始一條路徑,或重置當(dāng)前路徑

closePath()

closePath() 創(chuàng)建從當(dāng)前點回到起始點的路徑

moveTo()、lineTo()

moveTo() 把路徑移動到畫布中的指定點,不創(chuàng)建線條

lineTo() 添加一個新點,然后在畫布中創(chuàng)建從該點到最后指定點的線條

ctx.beginPath();
ctx.moveTo(0,0);
ctx.lineTo(300,150);
ctx.stroke();

quadraticCurveTo()、bezierCurveTo()

context.quadraticCurveTo(cpx,cpy,x,y); 創(chuàng)建二次貝塞爾曲線

context.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y); 創(chuàng)建三次方貝塞爾曲線

ctx.beginPath();
ctx.moveTo(20,20);
ctx.quadraticCurveTo(20,100,200,20);
ctx.stroke();
image
ctx.beginPath();
ctx.moveTo(20,20);
ctx.bezierCurveTo(20,100,200,100,200,20);
ctx.stroke();
image

arc()、arcTo()

context.arc(x,y,r,sAngle,eAngle[,counterclockwise]); 創(chuàng)建弧/曲線(用于創(chuàng)建圓形或部分圓)

context.arcTo(x1,y1,x2,y2,r); 創(chuàng)建兩切線之間的弧/曲線

ctx.beginPath();
arc(100, 75, 50, 0*Math.PI, 1.5*Math.PI)
ctx.stroke();
image
ctx.beginPath();
ctx.moveTo(20,20);           // 創(chuàng)建開始點
ctx.lineTo(100,20);          // 創(chuàng)建水平線
ctx.arcTo(150,20,150,70,50); // 創(chuàng)建弧
ctx.lineTo(150,120);         // 創(chuàng)建垂直線
ctx.stroke();                // 進行繪制

!

clip()

clip() 從原始畫布剪切任意形狀和尺寸的區(qū)域

// 剪切矩形區(qū)域
ctx.rect(50,20,200,120);
ctx.stroke();
ctx.clip();
// 在 clip() 之后繪制綠色矩形
ctx.fillStyle="green";
ctx.fillRect(0,0,150,100);

!

isPointInPath()

isPointInPath() 如果指定的點位于當(dāng)前路徑中,則返回 true,否則返回 false

ctx.rect(20,20,150,100);
if (ctx.isPointInPath(20,50)) {
  ctx.stroke()
}

轉(zhuǎn)換

scale()

scale() 縮放當(dāng)前繪圖至更大或更小

// 繪制矩形,放大到 200%,然后再次繪制矩形:
ctx.strokeRect(5,5,25,15);
ctx.scale(2,2);
ctx.strokeRect(5,5,25,15);

!

rotate()

rotate() 旋轉(zhuǎn)當(dāng)前繪圖

// 將矩形旋轉(zhuǎn) 20 度:
ctx.rotate(20 * Math.PI / 180);
ctx.fillRect(50,20,100,50);

!

translate()

translate() 重新定義畫布上的 (0,0) 位置

ctx.fillRect(10,10,100,50);
ctx.translate(70,70);
ctx.fillRect(10,10,100,50);

!

transform()setTransform()

context.transform(a,b,c,d,e,f); 替換繪圖的當(dāng)前轉(zhuǎn)換矩陣

context.setTransform(a,b,c,d,e,f); 將當(dāng)前轉(zhuǎn)換重置為單位矩陣。然后運行 transform()

文本

fonttextAlign、textBaseline

font 設(shè)置或返回文本內(nèi)容的當(dāng)前字體屬性

textAlign 設(shè)置或返回文本內(nèi)容的當(dāng)前對齊方式

textBaseline 設(shè)置或返回在繪制文本時使用的當(dāng)前文本基線

fillText()strokeText()、measureText()

context.fillText(text, x, y, maxWidth); 在畫布上繪制被填充的文本

context.strokeText(text,x,y,maxWidth); 在畫布上繪制文本(無填充)

context.measureText(text).width; 返回包含指定文本寬度的對象

ctx.font="30px Arial";
ctx.fillText("Hello World", 10, 50);
ctx.font="40px Arial";
// 創(chuàng)建漸變
var gradient=ctx.createLinearGradient(0, 0, myCanvas.width, 0);
gradient.addColorStop("0", "magenta");
gradient.addColorStop("0.5", "blue");
gradient.addColorStop("1.0", "red");
// 用漸變填色
ctx.strokeStyle=gradient;
ctx.strokeText("Hello World", 10, 90);

圖像繪制

drawImage()

context.drawImage(img,x,y,width,height); 向畫布上繪制圖像、畫布或視頻

var img=document.getElementById("tulip");
ctx.drawImage(img, 10, 10);

像素操作

width、heightdata

width 返回 ImageData 對象的寬度

height 返回 ImageData 對象的高度

data 返回一個對象,其包含指定的 ImageData 對象的圖像數(shù)據(jù)

createImageData()、getImageData()putImageData()

createImageData() 創(chuàng)建新的、空白的 ImageData 對象

getImageData() 返回 ImageData 對象,該對象為畫布上指定的矩形復(fù)制像素數(shù)據(jù)

putImageData() 把圖像數(shù)據(jù)(從指定的 ImageData 對象)放回畫布上

合成

context.globalAlpha = number; 設(shè)置或返回繪圖的當(dāng)前 alpha 或透明值
context.globalCompositeOperation="source-in"; 設(shè)置或返回新圖像如何繪制到已有的圖像上

其他

save()、restore()

save() 保存當(dāng)前環(huán)境的狀態(tài)
restore() 返回之前保存過的路徑狀態(tài)和屬性

getContext

let cxt = Canvas.getContext('2d')

為不同的繪制類型 (2d、3d) 提供不同的環(huán)境,當(dāng)前唯一支持的是 2d 環(huán)境

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,675評論 25 709
  • 一:canvas簡介 1.1什么是canvas? ①:canvas是HTML5提供的一種新標(biāo)簽 ②:HTML5 ...
    GreenHand1閱讀 4,874評論 2 32
  • Part7收集 那堵白墻大約是城外挺遠(yuǎn)的山上,李帝努細(xì)細(xì)地規(guī)劃去往的路徑,估計得花上幾天,在這之前,最好能拿上幾天...
    幾幾幾分鐘閱讀 429評論 0 0
  • 一 滴答,滴答,滴答,滴答。。。。。。。 隨著耳邊響起時鐘擺動的聲響,我緩緩睜開雙眼。 襁褓中的我想要拼命呼喊,卻...
    梁思成閱讀 379評論 0 4
  • 走過的路我會一直負(fù)責(zé)到底 雨來時我會為你撐傘陪著你 烈日當(dāng)頭我會給你一片陰涼 亂石封路我會背你越過山崗 烈酒灼喉時...
    舊時雨m閱讀 140評論 2 1

友情鏈接更多精彩內(nèi)容