html5 canvas 笔记二(添加样式和颜色)

   2023-03-08 学习力0
核心提示:色彩 ColorsfillStyle = color    设置图形的填充颜色。strokeStyle = color   设置图形轮廓的颜色。透明度 TransparencyglobalAlpha = transparency value1 // globalAlpha 示例2 ctx.fillStyle = '#FD0';3 ctx.globalAlpha = 0.2;4 5 // rgba() 示例6

色彩 Colors

  • fillStyle = color    设置图形的填充颜色。
  • strokeStyle = color   设置图形轮廓的颜色。

透明度 Transparency

globalAlpha = transparency value

1 // globalAlpha 示例
2 ctx.fillStyle = '#FD0';
3 ctx.globalAlpha = 0.2;
4 
5 // rgba() 示例
6 ctx.strokeStyle = "rgba(255,0,0,0.5)";
7 ctx.fillStyle = "rgba(255,0,0,0.5)";

线型 Line styles

可以通过一系列属性来设置线的样式

  • lineWidth = value  设置当前绘线的粗细。属性值必须为正数。默认值是1.0 
  • lineCap = type      设置线段端点显示的样子,类型为:buttroundsquare。默认是 butt。
  • lineJoin = type   设置图形中两线段连接处所显示的样子。类型为:round, bevelmiter。默认是 miter
  • miterLimit = value  设置外延交点与连接点的最大距离,如果交点距离大于此值,连接效果会变成了 bevel。

html5 canvas 笔记二(添加样式和颜色)html5 canvas 笔记二(添加样式和颜色)html5 canvas 笔记二(添加样式和颜色)html5 canvas 笔记二(添加样式和颜色)

渐变 Gradients

相关方法:

  • createLinearGradient(x1,y1,x2,y2)  渐变的起点 (x1,y1) 与终点 (x2,y2)。
  • createRadialGradient(x1,y1,r1,x2,y2,r2)  前三个定义一个以 (x1,y1) 为原点,半径为 r1 的圆,后三个参数则定义另一个以 (x2,y2) 为原点,半径为 r2 的圆。
  • addColorStop(position, color)  position 参数必须是一个 0.0 与 1.0 之间的数值,表示渐变中颜色所在的相对位置。(色标 color stop)

实例:

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 
 5 </head>
 6 <body onload="draw()">
 7 <canvas ></canvas>
 8 
 9 </body>
10 <script>
11 
12 function draw() {
13   var canvas = document.getElementById('tutorial');
14   if (canvas.getContext){
15     var ctx = canvas.getContext('2d');
16 
17       // Create gradients
18   var lingrad = ctx.createLinearGradient(0,0,0,150);
19   lingrad.addColorStop(0, '#00ABEB');
20   lingrad.addColorStop(0.5, '#fff');
21   lingrad.addColorStop(0.5, '#26C000');
22   lingrad.addColorStop(1, '#fff');
23 
24   var lingrad2 = ctx.createLinearGradient(0,50,0,95);
25   lingrad2.addColorStop(0.5, '#000');
26   lingrad2.addColorStop(1, 'rgba(0,0,0,0)');
27 
28   // assign gradients to fill and stroke styles
29   ctx.fillStyle = lingrad;
30   ctx.strokeStyle = lingrad2;
31   
32   // draw shapes
33   ctx.fillRect(10,10,130,130);
34   ctx.strokeRect(50,50,50,50);
35 }
36 }
37 
38 
39 
40 </script>
41 </html>
42 
43 function draw() {
44   var ctx = document.getElementById('canvas').getContext('2d');
45 
46   // Create gradients
47   var lingrad = ctx.createLinearGradient(0,0,0,150);
48   lingrad.addColorStop(0, '#00ABEB');
49   lingrad.addColorStop(0.5, '#fff');
50   lingrad.addColorStop(0.5, '#26C000');
51   lingrad.addColorStop(1, '#fff');
52 
53   var lingrad2 = ctx.createLinearGradient(0,50,0,95);
54   lingrad2.addColorStop(0.5, '#000');
55   lingrad2.addColorStop(1, 'rgba(0,0,0,0)');
56 
57   // assign gradients to fill and stroke styles
58   ctx.fillStyle = lingrad;
59   ctx.strokeStyle = lingrad2;
60   
61   // draw shapes
62   ctx.fillRect(10,10,130,130);
63   ctx.strokeRect(50,50,50,50);
64 
65 }

html5 canvas 笔记二(添加样式和颜色)

图案 Patterns

  • createPattern(image,type)  Image 可以是一个 Image 对象的引用,或者另一个 canvas 对象。Type 必须是下面的字符串值之一:repeatrepeat-xrepeat-yno-repeat

实例:

1 var img = new Image();
2 img.src = 'someimage.png';
3 var ptrn = ctx.createPattern(img,'repeat');
4 ctx.fillStyle = ptrn;
5 ctx.fillRect(0,0,150,150);

阴影 Shadows

  • shadowOffsetX = float  设定阴影在 X 轴的延伸距离,默认是 0。
  • shadowOffsetY = float  设定阴影在 Y 轴的延伸距离,默认是 0。
  • shadowBlur = float  设定阴影的模糊程度,默认为 0。
  • shadowColor = color  设定阴影颜色效果,默认是全透明的黑色。

实例:

 1 function draw() {
 2   var ctx = document.getElementById('canvas').getContext('2d');
 3 
 4   ctx.shadowOffsetX = 2;
 5   ctx.shadowOffsetY = 2;
 6   ctx.shadowBlur = 2;
 7   ctx.shadowColor = "rgba(0, 0, 0, 0.5)";
 8  
 9   ctx.font = "20px Times New Roman";
10   ctx.fillStyle = "Black";
11   ctx.fillText("Sample String", 5, 30);
12 }

Canvas 填充规则

  • "nonzero": non-zero winding rule, 默认值.
  •  "evenodd":  even-odd winding rule.

 

 
反对 0举报 0 评论 0
 

免责声明:本文仅代表作者个人观点,与乐学笔记(本网)无关。其原创性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容、文字的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
    本网站有部分内容均转载自其它媒体,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责,若因作品内容、知识产权、版权和其他问题,请及时提供相关证明等材料并与我们留言联系,本网站将在规定时间内给予删除等相关处理.

  • HTML中将背景颜色渐变 html设置背景颜色渐变
    通过使用 css3 渐变可以让背景两个或多个指定的颜色之间显示平稳的过渡,由于用到css3所以需要考虑下浏览器兼容问题,例如:从左到右的线性渐变,且带有透明度的样式:#grad {background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /*
    03-08
  • html5 Canvas 如何自适应屏幕大小
    但是这样创建出的画布不能随着浏览器窗口大小的改变而动态的改变画布的大小。而这一点往往又非常重要, 因为我们会经常改变浏览器窗口大小,不会一直保持某个固定的大小。 html代码 canvas width="300" height="300" id="myCanvas"/canvas设置样式 * {
    03-08
  • Vue中出现Do not use built-in or reserved HTML elements as component id:footer等等vue warn问题
    Vue中出现Do not use built-in or reserved HTM
    错误示图:原因:是因为在本地项目对应文件的script中,属性name出现了错误的命名方式,导致浏览器控制台报错!  诸如: name: header 、  、 name: menu , 等等都属于错误的命名方式等 错误代码命名如下:解决办法:办法1: 如果我们采用正确命名
    03-08
  • HTML在网页中插入音频视频简单的滚动效果
    HTML在网页中插入音频视频简单的滚动效果
    每次上网,打开网页后大家都会看到在网页的标签栏会有个属于他们官网的logo,现在学了HTML了,怎么不会制作这个小logo呢,其实很简单,也不需要死记硬背,每当这行代码出现的时候能知道这是什么意思就ok1 link rel="shortcuticon" type="image/x-icon" href="
    03-08
  • HTML的video标签,不能下载视频代码
    !-- 在线视频不能下载代码 --!DOCTYPE html html headscript src="../Demo/demo/book/JQuery/jQuery v2.2.0.js"/script/headbody div style="text-align:center;"video src="../images/PreviewVideo.mp4" width="820"controls="controls&
    03-08
  • ThinkPHP报错 The requested URL /admin/index/login.html was not found on this server.
    ThinkPHP报错 The requested URL /admin/index/
           解决方案在入口文件夹public下查看.htaccess是否存在。不存在则新建,存在的话,那内容替换为下面这串代码 就可以解决Not Fund#IfModule mod_rewrite.c#Options +FollowSymlinks -Multiviews#RewriteEngine On##RewriteCond %{REQUEST_FILENAME
    03-08
  • HTML特殊字符、列表、表格总结 html特殊符号对
            HTML实体字符  在HTML中一些特殊的字符需要用特殊的方式才能显示出来,比如小于号、版权等,  在课堂上老师教了我们一个有点意思的:空格,在教材上字符实体是“nbsp”通过老师  的演示我们发现不同的浏览器他所显示的效果不同,有的比
    03-08
  • 【JavaScript】使用document.write输出覆盖HTML
    您只能在 HTML 输出中使用 document.write。如果您在文档加载后使用该方法,会覆盖整个文档。分析HTML输出流是指当前数据形式是HTML格式的数据,这部分数据正在被导出、传输或显示,所以称为“流”。通俗的来说就是HTML文档的加载过程,如果遇到document.writ
    03-08
  • ASP.Net MVC 控制@Html.DisplayFor日期显示格式
    在做一個舊表的查詢頁時,遇到一個問題:字段在db里存儲的是DATETIME,但保存的值只有日期,沒有時間數據,比如2018/2/26 0:00:00,顯示出來比較難看,當然也可以做一個ViewModel,在字段上添加Attribute定義來更改名稱和顯示名稱,如下:[Display(Name = "建
    03-08
  • html 基础代码
    title淄博汉企/title/headbody bgcolor="#00CC66" topmargin="200" leftmargin="200" bottommargin="200"a name="top"/a今天br /天气nbsp;nbsp;nbsp;nbsp;nbsp;不错br /font color="#CC0000"格式控制标签br /b 文字加粗方式1\bbr /str
    03-08
点击排行