標籤:des style http color os java 使用 io strong
HTML5 Canvas 將使用像素在螢幕上繪製圖形映像。 本節示範了五種用於操作像素以建立攝影特效的 Canvas 技術。 您可使用這些技術來產生獨具特色的映像,為您的網站、部落格、視頻遊戲畫面、廣告和插圖等提供資訊或藝術趣味。
本教程包含五個獨立的帶有注釋的程式碼範例,為您示範了這些技術如何改善一張有關美洲鷹的攝影映像。 這些樣本解釋了如何執行以下操作: 將美洲鷹的照片放置在一個圓形中;向背景中添加白雪紋理圖片;大幅改變色彩構成;使映像變得透明;並將整張圖片從彩色變為黑白。
本主題介紹如何使用 HTML5 Canvas 將照片放入不同形狀中,並為 Web 設計建立視覺特效。
本主題包含一個獨立的帶有注釋的程式碼範例,示範如何將一張有關美洲鷹的照片放入到一個圓形形狀中。 此程式碼範例示範了如何使用 Canvas 載入美洲鷹照片並顯示在螢幕上。 然後,它示範如何使用 Canvas arc 方法建立一個圓,並圍繞美洲鷹添加一個白色圓形框。 此樣本末尾的討論材料將說明有關代碼如何工作來開發此技術的更多資訊。
Canvas 程式碼範例討論:
此討論解釋了此 Canvas 樣本的設計和結構以及所有組件如何協同工作。 此樣本使用標準 HTML5 標題 ,以便瀏覽器可以將其作為 HTML5 規範的一部分加以區別。 代碼分成兩個主要部分:
1、主體代碼
2、指令碼代碼
主體代碼
當載入頁面時,主體標記使用 onload 函數來調用 displayImage 函數。 將美洲鷹的原始映像載入到主體中,以便您可以將其與要由 Canvas 修改的映像進行比較。 canvas 標記是主體的一部分。 指定了 Canvas 的初始寬度和高度,還指定了 id 特性。 必須使用 ID,才能將 Canvas 元素添加到頁面的物件模型中。
指令碼代碼
指令碼程式碼封裝括兩個函數: displayImage和getColorData。 載入頁面時將調用 displayImage 函數。 getColorData 函數是從 displayImage 調用的。 在指令碼部分的開頭建立一個全域變數以建立一個空白影像檔,稍後將會用到這個檔案。
displayImage 函數
載入頁面時將調用此函數。 它通過在主體代碼中使用 canvas 元素的 ID 來擷取畫布。 然後,它擷取畫布的 CanvasRenderingContext2D 對象,使其準備好接受繪製,並使用 DrawImage 將映像載入到上下文中。 在將上下文初始化為二維畫布之後,就可以在畫布上開始繪製了。
該函數的最後工作是通過提供一條路徑來指定映像的來源。 由於映像可能不會立即載入,因此設定了一個事件,以便在映像實際載入之後調用某個函數。 在載入映像之後,將會顯示映像,並調用用於完成繪製工作的 changeImage 函數。
changeImage 函數
通過使用 arc 方法圍繞映像中心繪製一個圓,從而修改映像。 映像為 200 x 200 像素,圓的半徑將為 150 個像素。 因為圓的中心是映像中心,並且圓邊框為 100 個像素厚,因此外部邊緣上的像素將用白色像素替換。 strokeStyle 屬性、lineWidth 屬性、beginPath 方法、arc 方法、closePath 方法和 stroke 方法定義這個圓。
代碼要求
此代碼在 Windows Internet Explorer 9 中運行。 它無法在早期版本的 Windows Internet Explorer 中正常運行,但也許能在支援 HTML5 Canvas 的其他瀏覽器中運行。
?1. [代碼][HTML]代碼
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
//Global variables
var myImage =
new Image(); // Create a new blank image.
// Load the image and display it.
function displayImage() {
//
Get the canvas element.
canvas = document.getElementById("myCanvas");
// Make sure you got it.
if (canvas.getContext) {
// Specify 2d canvas type.
ctx = canvas.getContext("2d");
// When the image is loaded, draw it.
myImage.onload =
function() {
// Load the image into the context.
ctx.drawImage(myImage, 0, 0);
//
Get
and modify the image data.
changeImage();
}
// Define the source of the image.
myImage.src =
"http://samples.msdn.microsoft.com/workshop/samples/canvas/kestral.png";
}http://www.enterdesk.com/special/sexygirl/?
}美女圖片
function changeImage() {
ctx.strokeStyle =
"white";
ctx.lineWidth =
"100";
ctx.beginPath();
ctx.arc(100, 100, 150, 0, Math.PI *
2, true);
ctx.closePath();
ctx.stroke();
}
</script>
</head>
<body onload="displayImage()">
<h1>
American Kestral
</h1>
<p>
The original image is
on the left
and the modified image is
on the right.
</p>
<img id="myPhoto" src="http://samples.msdn.microsoft.com/workshop/samples/canvas/kestral.png">
<canvas id="myCanvas" width="200" height="200">
</canvas>
<p>
Public domain image courtesy of U.S. Fish and Wildlife Service.
</p>
</body>
</html>
?
用HTML5 Canvas為Web圖形建立特效