濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > HTML5 Canvas如何實(shí)現(xiàn)紋理填充與描邊(Fill And Stroke)

HTML5 Canvas如何實(shí)現(xiàn)紋理填充與描邊(Fill And Stroke)

熱門(mén)標(biāo)簽:臨海地圖標(biāo)注app 咸陽(yáng)穩(wěn)定外呼系統(tǒng)軟件 地圖標(biāo)注柱狀圖 智能芯電話(huà)機(jī)器人 四川移動(dòng)電銷(xiāo)外呼客戶(hù)管理系統(tǒng) 百度地圖標(biāo)注為什么總是封號(hào) 小朱地圖標(biāo)注 怎么做百度地圖標(biāo)注 400開(kāi)頭的電話(huà)好申請(qǐng)不
演示HTML5 Canvas Fill 與Stroke文字效果,基于Canvas如何實(shí)現(xiàn)紋理填充與描邊。

一:顏色填充與描邊
顏色填充可以通過(guò)fillStyle來(lái)實(shí)現(xiàn),描邊顏色可以通過(guò)strokeStyle來(lái)實(shí)現(xiàn)。簡(jiǎn)單示例
如下:

復(fù)制代碼
代碼如下:

// fill and stroke text
ctx.font = '60pt Calibri';
ctx.lineWidth = 3;
ctx.strokeStyle = 'green';
ctx.strokeText('Hello World!', 20, 100);
ctx.fillStyle = 'red';
ctx.fillText('Hello World!', 20, 100);

二:紋理填充與描邊
HTML5 Canvas還支持紋理填充,通過(guò)加載一張紋理圖像,然后創(chuàng)建畫(huà)筆模式,創(chuàng)建紋理模式的API為ctx.createPattern(imageTexture,"repeat");第二參數(shù)支持四個(gè)值,分別為”repeat-x”, ”repeat-y”, ”repeat”,”no-repeat”意思是紋理分別沿著X軸,Y軸,XY方向沿重復(fù)或者不重復(fù)。紋理描邊與填充的代碼如下:

復(fù)制代碼
代碼如下:

var woodfill = ctx.createPattern(imageTexture,"repeat");
ctx.strokeStyle = woodfill;
ctx.strokeText('Hello World!', 20, 200);
// fill rectangle
ctx.fillStyle = woodfill;
ctx.fillRect(60, 240, 260, 440);

紋理圖片:
 
三:運(yùn)行效果
 
代碼:

復(fù)制代碼
代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="chrome=IE8">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Canvas Fill And Stroke Text Demo</title>
<link href="default.css" rel="stylesheet" />
<script>
var ctx = null; // global variable 2d context
var imageTexture = null;
window.onload = function() {
var canvas = document.getElementById("text_canvas");
console.log(canvas.parentNode.clientWidth);
canvas.width = canvas.parentNode.clientWidth;
canvas.height = canvas.parentNode.clientHeight;
if (!canvas.getContext) {
console.log("Canvas not supported. Please install a HTML5 compatible browser.");
return;
}
// get 2D context of canvas and draw rectangel
ctx = canvas.getContext("2d");
ctx.fillStyle="black";
ctx.fillRect(0, 0, canvas.width, canvas.height);
// fill and stroke text
ctx.font = '60pt Calibri';
ctx.lineWidth = 3;
ctx.strokeStyle = 'green';
ctx.strokeText('Hello World!', 20, 100);
ctx.fillStyle = 'red';
ctx.fillText('Hello World!', 20, 100);
// fill and stroke by pattern
imageTexture = document.createElement('img');
imageTexture.src = "../pattern.png";
imageTexture.onload = loaded();
}
function loaded() {
// delay to image loaded
setTimeout(textureFill, 1000/30);
}
function textureFill() {
// var woodfill = ctx.createPattern(imageTexture, "repeat-x");
// var woodfill = ctx.createPattern(imageTexture, "repeat-y");
// var woodfill = ctx.createPattern(imageTexture, "no-repeat");
var woodfill = ctx.createPattern(imageTexture, "repeat");
ctx.strokeStyle = woodfill;
ctx.strokeText('Hello World!', 20, 200);
// fill rectangle
ctx.fillStyle = woodfill;
ctx.fillRect(60, 240, 260, 440);
}
</script>
</head>
<body>
<h1>HTML5 Canvas Text Demo - By Gloomy Fish</h1>
<pre>Fill And Stroke</pre>
<div id="my_painter">
<canvas id="text_canvas"></canvas>
</div>
</body>
</html>

標(biāo)簽:陜西 公主嶺 南平 平頂山 山南 黃石 黃石

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《HTML5 Canvas如何實(shí)現(xiàn)紋理填充與描邊(Fill And Stroke)》,本文關(guān)鍵詞  HTML5,Canvas,如何,實(shí)現(xiàn),紋理,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《HTML5 Canvas如何實(shí)現(xiàn)紋理填充與描邊(Fill And Stroke)》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于HTML5 Canvas如何實(shí)現(xiàn)紋理填充與描邊(Fill And Stroke)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    渝中区| 乌拉特后旗| 邢台县| 崇仁县| 宜丰县| 潜山县| 佳木斯市| 汉川市| 井冈山市| 深圳市| 河间市| 自贡市| 桃园县| 德阳市| 曲水县| 彰化县| 本溪市| 内黄县| 洛隆县| 饶阳县| 四平市| 公主岭市| 墨竹工卡县| 常山县| 宜昌市| 册亨县| 隆安县| 内丘县| 天峻县| 山东省| 利辛县| 辽阳县| 金坛市| 固阳县| 藁城市| 汝南县| 乌拉特前旗| 海南省| 宁武县| 商丘市| 马尔康县|