三:运行效果
目錄
HTML5 Canvas Text Demo - By Gloomy Fish
首頁 web前端 H5教程 HTML5 Canvas如何實作紋理填滿與描邊(Fill And Stroke)_html5教學技巧

HTML5 Canvas如何實作紋理填滿與描邊(Fill And Stroke)_html5教學技巧

May 16, 2016 pm 03:49 PM
canvas html5 描邊

示範HTML5 Canvas Fill 與Stroke文字效果,基於Canvas如何實作紋理填滿與描邊。

一:顏色填充與描邊
顏色填充可以透過fillStyle來實現,描邊顏色可以透過strokeStyle來實現。簡單範例
如下:

複製程式碼
程式碼如下:


程式碼如下:


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也支援紋理填充,透過載入一個紋理影像,然後建立畫筆模式,建立紋理模式的API為ctx.createPattern(imageTexture,"repeat");第二參數支援四個值,分別為”repeat-x”, ”repeat-y”, ”repeat”,”no-repeat”意思是紋理分別沿著X軸,Y軸,XY方向沿重複或不重複。紋理描邊與填充的代碼如下:
複製代碼


代碼如下:


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);
紋理圖片:
 效果


 
代碼:
複製程式碼


複製程式碼



複製程式碼



複製程式碼



複製程式碼



複製程式碼



複製碼






Canvas Fill And Stroke Text Demo

<script> <br />var ctx = null; // global variable 2d context <br />var imageTexture = null; <br />window.onload = function() { <br />var canvas = document.getElementById("text_canvas"); <br />console.log(canvas.parentNode.clientWidth); <br />canvas.width = canvas.parentNode.clientWidth; <br />canvas.width = canvas.parentNode.clientWidth; <br />canvas. parentNode.clientHeight; <br />if (!canvas.getContext) { <br />console.log("Canvas not supported. Please install a HTML5 compatible browser."); <br />return; <br />} <br />} <br />return; / get 2D context of canvas and draw rectangel <br />ctx = canvas.getContext("2d"); <br />ctx.fillStyle="black"; <br />ctx.fillRect(0, 0, canvas.width, canvas.width, canvas. .height); <br />// fill and stroke text <br />ctx.font = '60pt Calibri'; <br />ctx.lineWidth = 3; <br />ctx.strokeStyle = 'green'; <br />ctx. strokeText('Hello World!', 20, 100); <br />ctx.fillStyle = 'red'; <br />ctx.fillText('Hello World!', 20, 100); <br />// fill and stroke by pattern <br />imageTexture = document.createElement('img'); <br />imageTexture.src = "../pattern.png"; <br />imageTexture.onload = loaded(); <br />} <br />imageTexture.onload = loaded(); </script>
} function loaded() { // delay to image loaded setTimeout(textureFill, 1000/30); } function textureFill() { // ctvar woodfate = ctvarx. 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); }

HTML5 Canvas Text Demo - By Gloomy Fish

Fill And Stroke
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

HTML 中的表格邊框 HTML 中的表格邊框 Sep 04, 2024 pm 04:49 PM

HTML 表格邊框指南。在這裡,我們以 HTML 中的表格邊框為例,討論定義表格邊框的多種方法。

HTML 中的巢狀表 HTML 中的巢狀表 Sep 04, 2024 pm 04:49 PM

這是 HTML 中巢狀表的指南。這裡我們討論如何在表中建立表格以及對應的範例。

HTML 左邊距 HTML 左邊距 Sep 04, 2024 pm 04:48 PM

HTML 左邊距指南。在這裡,我們討論 HTML margin-left 的簡要概述及其範例及其程式碼實作。

HTML 表格佈局 HTML 表格佈局 Sep 04, 2024 pm 04:54 PM

HTML 表格佈局指南。在這裡,我們詳細討論 HTML 表格佈局的值以及範例和輸出。

HTML 輸入佔位符 HTML 輸入佔位符 Sep 04, 2024 pm 04:54 PM

HTML 輸入佔位符指南。在這裡,我們討論 HTML 輸入佔位符的範例以及程式碼和輸出。

HTML 有序列表 HTML 有序列表 Sep 04, 2024 pm 04:43 PM

HTML 有序列表指南。在這裡我們也分別討論了 HTML 有序列表和類型的介紹以及它們的範例

在 HTML 中移動文字 在 HTML 中移動文字 Sep 04, 2024 pm 04:45 PM

HTML 中的文字移動指南。在這裡我們討論一下marquee標籤如何使用語法和實作範例。

HTML onclick 按鈕 HTML onclick 按鈕 Sep 04, 2024 pm 04:49 PM

HTML onclick 按鈕指南。這裡我們分別討論它們的介紹、工作原理、範例以及各個事件中的onclick事件。

See all articles