首页前端开发HTMLHTML5 Canvas如何实现纹理填充与描边(Fill And Stroke)

HTML5 Canvas如何实现纹理填充与描边(Fill And Stroke)

时间2024-01-24 08:48:22发布访客分类HTML浏览486
导读:收集整理的这篇文章主要介绍了HTML5 Canvas如何实现纹理填充与描边(Fill And Stroke ,觉得挺不错的,现在分享给大家,也给大家做个参考。 演示HTML5 canvas Fill 与stroke文字效果,基于C...
收集整理的这篇文章主要介绍了HTML5 Canvas如何实现纹理填充与描边(Fill And Stroke),觉得挺不错的,现在分享给大家,也给大家做个参考。 演示HTML5 canvas Fill 与stroke文字效果,基于Canvas如何实现纹理填充与描边。

一:颜色填充与描边
颜色填充可以通过fillStyle来实现,描边颜色可以通过strokeStyle来实现。简单示例
如下:

复制代码代码如下:
// 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);

二:纹理填充与描边
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);

纹理图片:
 
三:运行效果
 
代码:

复制代码代码如下:
!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(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('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/h1>
PRe> Fill And Stroke/pre>
div id="my_painter">
canvas id="text_canvas"> /canvas>
/div>
/body>
/html>

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

上一篇: html5 音乐播放器 audio 标签使用...下一篇:css 如何让背景图片拉伸填充避免...猜你在找的html5相关文章 关于移动端h5开发相关内容总结2022-05-17html5教程-学表单的第二天2018-12-10html5教程-HTML5浏览器支持2018-12-10html5教程-HTML5智能表单2018-12-10html5教程-微信H5使用resLoader实现加载页效果2018-12-10html5教程-day01-初级-JS0-热身运动JS入门教程2018-12-10html5教程-html5shiv.js和respond.min.js2018-12-10html5教程-不同浏览器对于html5 audio标签和音频格式的兼容性2018-12-10html5教程-使用Html5实现手风琴案例2018-12-10html5教程-html5笔记2018-12-10 其他相关热搜词更多phpjavapython程序员load

若转载请注明出处: HTML5 Canvas如何实现纹理填充与描边(Fill And Stroke)
本文地址: https://pptw.com/jishu/585214.html
仿CSDN Blog返回页面顶部功能实现原理及代码 Html5实现iPhone开机界面示例代码

游客 回复需填写必要信息