佳木斯湛栽影视文化发展公司

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

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

熱門標簽:百度競價點擊價格的計算公式 檢查注冊表項 智能手機 硅谷的囚徒呼叫中心 美圖手機 使用U盤裝系統(tǒng) 網(wǎng)站建設(shè) 阿里云
演示HTML5 Canvas Fill 與Stroke文字效果,基于Canvas如何實現(xiàn)紋理填充與描邊。

一:顏色填充與描邊
顏色填充可以通過fillStyle來實現(xiàn),描邊顏色可以通過strokeStyle來實現(xià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還支持紋理填充,通過加載一張紋理圖像,然后創(chuàng)建畫筆模式,創(chuàng)建紋理模式的API為ctx.createPattern(imageTexture,"repeat");第二參數(shù)支持四個值,分別為”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);

紋理圖片:
 
三:運行效果
 
代碼:

復(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>

標簽:煙臺 懷化 黃山 湘潭 湖北 山南 通遼 賀州

巨人網(wǎng)絡(luò)通訊聲明:本文標題《HTML5 Canvas如何實現(xiàn)紋理填充與描邊(Fill And Stroke)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    富平县| 农安县| 黑水县| 通海县| 尼木县| 大足县| 土默特左旗| 白河县| 嘉黎县| 昌平区| 胶南市| 旬邑县| 石狮市| 青川县| 革吉县| 梅河口市| 鹤峰县| 榆林市| 泽普县| 墨江| 磐安县| 若尔盖县| 岳池县| 新源县| 永寿县| 新密市| 宁河县| 双桥区| 东宁县| 庄浪县| 高邮市| 大埔区| 南漳县| 高阳县| 青州市| 盐池县| 武邑县| 乾安县| 含山县| 乳山市| 开平市|