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

主頁(yè) > 知識(shí)庫(kù) > HTML5 Canvas陰影使用方法實(shí)例演示

HTML5 Canvas陰影使用方法實(shí)例演示

熱門標(biāo)簽:檢查注冊(cè)表項(xiàng) 網(wǎng)站建設(shè) 阿里云 硅谷的囚徒呼叫中心 智能手機(jī) 使用U盤裝系統(tǒng) 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 美圖手機(jī)
HTML5 Canvas中提供了設(shè)置陰影的四個(gè)屬性值分別為:
context.shadowColor = “red” 表示設(shè)置陰影顏色為紅色
context.shadowOffsetX = 0表示陰影相對(duì)TEXT的水平距離,0表示兩者水平位置重合
context.shadowOffsetY = 0表示陰影相對(duì)TEXT的垂直距離,0表示兩者垂直位置重合
context.shadowBlur = 10 陰影模糊效果,值越大模糊越厲害。
一個(gè)最簡(jiǎn)單的帶有陰影的矩形代碼如下:
context.shadowColor = "RGBA(127,127,127,1)";
context.shadowOffsetX = 3;
context.shadowOffsetY = 3;
context.shadowBlur = 0;
context.fillStyle = "RGBA(0, 0, 0, 0.8)";
context.fillRect(10, hh+10, 200,canvas.height/4-20);
效果如下:
 
陰影文字:
只要設(shè)置shadowOffsetX與shadowOffsetY的值,當(dāng)值都正數(shù)時(shí),陰影相對(duì)文字的右下
方偏移。當(dāng)值都為負(fù)數(shù)時(shí),陰影相對(duì)文字的左上方偏移。
3D拉影效果:
在同一位置不斷的重復(fù)繪制文字同時(shí)改變shadowOffsetX、shadowOffsetY、shadowBlur
的值,從小到大不斷偏移不斷增加,透明度也不斷增加。就得到了拉影效果文字。
邊緣模糊效果文字:
在3D拉影效果的基礎(chǔ)上在四個(gè)方向重復(fù),就得到了邊緣羽化的文字效果。
運(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 Clip 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;
}
var context = canvas.getContext('2d');
// section one - shadow and blur
context.fillStyle="black";
context.fillRect(0, 0, canvas.width, canvas.height/4);
context.font = '60pt Calibri';
context.shadowColor = "white";
context.shadowOffsetX = 0;
context.shadowOffsetY = 0;
context.shadowBlur = 20;
context.fillText("Blur Canvas", 40, 80);
context.strokeStyle = "RGBA(0, 255, 0, 1)";
context.lineWidth = 2;
context.strokeText("Blur Canvas", 40, 80);
// section two - shadow font
var hh = canvas.height/4;
context.fillStyle="white";
context.fillRect(0, hh, canvas.width, canvas.height/4);
context.font = '60pt Calibri';
context.shadowColor = "RGBA(127,127,127,1)";
context.shadowOffsetX = 3;
context.shadowOffsetY = 3;
context.shadowBlur = 0;
context.fillStyle = "RGBA(0, 0, 0, 0.8)";
context.fillText("Blur Canvas", 40, 80+hh);
// section three - down shadow effect
var hh = canvas.height/4 + hh;
context.fillStyle="black";
context.fillRect(0, hh, canvas.width, canvas.height/4);
for(var i = 0; i < 10; i++)
{
context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
context.shadowOffsetX = i*2;
context.shadowOffsetY = i*2;
context.shadowBlur = i*2;
context.fillStyle = "RGBA(127, 127, 127, 1)";
context.fillText("Blur Canvas", 40, 80+hh);
}
// section four - fade effect
var hh = canvas.height/4 + hh;
context.fillStyle="green";
context.fillRect(0, hh, canvas.width, canvas.height/4);
for(var i = 0; i < 10; i++)
{
context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
context.shadowOffsetX = 0;
context.shadowOffsetY = -i*2;
context.shadowBlur = i*2;
context.fillStyle = "RGBA(127, 127, 127, 1)";
context.fillText("Blur Canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
context.shadowOffsetX = 0;
context.shadowOffsetY = i*2;
context.shadowBlur = i*2;
context.fillStyle = "RGBA(127, 127, 127, 1)";
context.fillText("Blur Canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
context.shadowOffsetX = i*2;
context.shadowOffsetY = 0;
context.shadowBlur = i*2;
context.fillStyle = "RGBA(127, 127, 127, 1)";
context.fillText("Blur Canvas", 40, 80+hh);
}
for(var i = 0; i < 10; i++)
{
context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
context.shadowOffsetX = -i*2;
context.shadowOffsetY = 0;
context.shadowBlur = i*2;
context.fillStyle = "RGBA(127, 127, 127, 1)";
context.fillText("Blur Canvas", 40, 80+hh);
}
}
</script>
</head>
<body>
<h1>HTML5 Canvas Clip Demo - By Gloomy Fish</h1>
<pre>Fill And Stroke Clip</pre>
<div id="my_painter">
<canvas id="text_canvas"></canvas>
</div>
</body>
</html>

標(biāo)簽:通遼 湖北 懷化 湘潭 賀州 黃山 山南 煙臺(tái)

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

    • 400-1100-266
    吴忠市| 岳阳市| 通许县| 甘孜县| 刚察县| 定边县| 旺苍县| 株洲县| 湖北省| 邻水| 宝山区| 锡林浩特市| 翁牛特旗| 嘉黎县| 肇州县| 泸州市| 林周县| 高唐县| 边坝县| 秦皇岛市| 建宁县| 宁国市| 宜丰县| 东乡县| 理塘县| 重庆市| 大连市| 延安市| 华安县| 宁安市| 宁晋县| 玛纳斯县| 宜兰县| 潮安县| 泰和县| 吕梁市| 高尔夫| 濮阳县| 双辽市| 来宾市| 岑巩县|