How to Implement Fill And Stroke in HTML5 Canvas)

Source: Internet
Author: User

Comments: This article describes in detail the text effects of HTML5 Canvas Fill and Stroke. The code for how to implement texture filling and Stroke, color filling and Stroke Based on Canvas is as follows, if you are interested, you can refer to the following: I hope to help demonstrate the HTML5 Canvas Fill and Stroke text effects, and how to implement texture filling and Stroke Based on Canvas.

I. Color filling and stroke
Color filling can be achieved through fillStyle, and stroke color can be achieved through strokeStyle. Simple Example
As follows:

The Code 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 the paint brush mode, the API for creating the texture mode is ctx. createPattern (imageTexture, "repeat"); the second parameter supports four values: "repeat-x", "repeat-y", "repeat ", "no-repeat" indicates that the texture is repeated or not repeated along the X axis, Y axis, and XY directions. The code for texture stroke and filling is as follows:

The Code 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 Image:
 
Iii. Running Effect
 
Code:

The Code is as follows:
<! 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 (FIG, 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 <Pre> Fill And Stroke </pre>
<Div id = "my_painter">
<Canvas id = "text_canvas"> </canvas>
</Div>
</Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.