欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

fillstyle属性_html设置或返回用于填充绘画的颜色渐变或模式的属性fillStyle

发布时间:2024/9/27 编程问答 40 豆豆
生活随笔 收集整理的这篇文章主要介绍了 fillstyle属性_html设置或返回用于填充绘画的颜色渐变或模式的属性fillStyle 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

实例

定义用蓝色填充的矩形:

JavaScript:var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

ctx.fillStyle="#0000ff";

ctx.fillRect(20,20,150,100);

浏览器支持

Internet Explorer 9、Firefox、Opera、Chrome 以及 Safari 支持 fillStyle 属性。

注释:Internet Explorer 8 以及更早的版本不支持 元素。

定义和用法

fillStyle 属性设置或返回用于填充绘画的颜色、渐变或模式。默认值:#000000

JavaScript 语法:context.fillStyle=color|gradient|pattern;

属性值值描述

color指示绘图填充色的 CSS 颜色值。默认值是 #000000

gradient用于填充绘图的渐变对象(线性或放射性)

pattern用于填充绘图的 pattern 对象

更多实例

实例 1

定义从上到下的渐变,作为矩形的填充样式:

JavaScript:var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

var my_gradient=ctx.createLinearGradient(0,0,0,170);

my_gradient.addColorStop(0,"black");

my_gradient.addColorStop(1,"white");

ctx.fillStyle=my_gradient;

ctx.fillRect(20,20,150,100);

实例 2

定义从左到右的渐变,作为矩形的填充样式:

JavaScript:var c=document.getElementById("myCanvas");

var ctx=c.getContext("2d");

var my_gradient=ctx.createLinearGradient(0,0,170,0);

my_gradient.addColorStop(0,"black");

my_gradient.addColorStop(1,"white");

ctx.fillStyle=my_gradient;

ctx.fillRect(20,20,150,100);

总结

以上是生活随笔为你收集整理的fillstyle属性_html设置或返回用于填充绘画的颜色渐变或模式的属性fillStyle的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。