Home  >  Article  >  Web Front-end  >  Example of Fill and Stroke text effects in h5 Canvas

Example of Fill and Stroke text effects in h5 Canvas

零下一度
零下一度Original
2017-05-17 14:24:552764browse

This article will give you a detailed introduction to HTML5 Canvas Fill and Stroke text effects, how to implement texture fill and stroke, color fill and stroke based on Canvas, the specific code is as follows, feel Interested friends can refer to it. I hope it will be helpful to everyone. Demonstrates HTML5 Canvas Fill and Stroke text effects, and how to implement texture filling and stroke based on Canvas.
1: Color filling and stroke
Color filling can be achieved through fillStyle, and stroke color can be achieved through strokeStyle. A simple example
is as follows:

// 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);

2: Texture filling and stroke
HTML5 Canvas also supports texture filling. By loading a texture image and then creating a brush mode, create The API of the texture mode is ctx.createPattern(imageTexture,"repeat"); the second parameter supports four values, namely "repeat-x", "repeat-y", "repeat", "no -repeat" means that the texture repeats or does not repeat along the X-axis, Y-axis, and XY direction. The code for texture stroke and fill is as follows:

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);

Texture picture:

3: Running effect

The code is as follows:

<!DOCTYPE html> 
<html> 
<head> 
<meta http-equiv="X-UA-Compatible" content="
chr
ome=IE8"> 
<meta http-equiv="Content-type" content="text/html;char
set
=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 inst
all
 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 = &#39;60pt Calibri&#39;; 
ctx.li
neW
idth = 3; 
ctx.strokeStyle = &#39;green&#39;; 
ctx.strokeText(&#39;Hello World!&#39;, 20, 100); 
ctx.fillStyle = &#39;red&#39;; 
ctx.fillText(&#39;Hello World!&#39;, 20, 100); 
// fill and stroke by pattern 
imageTexture = document.createElement(&#39;img&#39;); 
imageTexture.src = "../pattern.png"; 
imageTexture.onload = loaded(); 
} 
function loaded() { 
// delay to image loaded 
set
Time
out(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(&#39;Hello World!&#39;, 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 class="brush:php;toolbar:false">Fill And Stroke

[Related recommendations]

1. Special recommendation:"php programmer Toolbox" V0.1 version download

2. h5canvas to realize the special effects code of falling snow

3. Share the use of canvas to realize water flow and pools Animation code

The above is the detailed content of Example of Fill and Stroke text effects in h5 Canvas. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn