当前位置:Gxlcms > html代码 > 实例讲解使用HTML5Canvas绘制阴影效果的方法_html5教程技巧

实例讲解使用HTML5Canvas绘制阴影效果的方法_html5教程技巧

时间:2021-07-01 10:21:17 帮助过:27人阅读

创建阴影效果需要操作以下4个属性:

1.context.shadowColor:阴影颜色。
2.context.shadowOffsetX:阴影x轴位移。正值向右,负值向左。
3.context.shadowOffsetY:阴影y轴位移。正值向下,负值向上。
4.context.shadowBlur:阴影模糊滤镜。数据越大,扩散程度越大。
这四个属性只要设置了第一个和剩下三个中的任意一个就有阴影效果。不过通常情况下,四个属性都要设置。

例如,创建一个向右下方位移各5px的红色阴影,模糊2px,可以这样写。

  1. context.shadowColor = "red";
  2. context.shadowOffsetX = 5;
  3. context.shadowOffsetY = 5;
  4. context.shadowBlur= 2;


需要注意的是,这里的阴影同其他属性设置一样,都是基于状态的设置。因此,如果只想为某一个对象应用阴影而不是全局阴影,需要在下次绘制前重置阴影的这四个属性。
运行结果:
2016325110954383.jpg (850×500)

阴影文字:

只要设置shadowOffsetX与shadowOffsetY的值,当值都正数时,阴影相对文字的右下

方偏移。当值都为负数时,阴影相对文字的左上方偏移。

3D拉影效果:

在同一位置不断的重复绘制文字同时改变shadowOffsetX、shadowOffsetY、shadowBlur

的值,从小到大不断偏移不断增加,透明度也不断增加。就得到了拉影效果文字。

边缘模糊效果文字:

在3D拉影效果的基础上在四个方向重复,就得到了边缘羽化的文字效果。

运行效果:
2016325111023538.jpg (472×470)

程序代码:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="X-UA-Compatible" content="chrome=IE8">
  5. <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
  6. <title>Canvas Clip Demo</title>
  7. <link href="default.css" rel="stylesheet" />
  8. <script>
  9. var ctx = null; // global variable 2d context
  10. var imageTexture = null;
  11. window.onload = function() {
  12. var canvas = document.getElementById("text_canvas");
  13. console.log(canvas.parentNode.clientWidth);
  14. canvas.width = canvas.parentNode.clientWidth;
  15. canvas.height = canvas.parentNode.clientHeight;
  16. if (!canvas.getContext) {
  17. console.log("Canvas not supported. Please install a HTML5 compatible browser.");
  18. return;
  19. }
  20. var context = canvas.getContext('2d');
  21. // section one - shadow and blur
  22. context.fillStyle="black";
  23. context.fillRect(0, 0, canvas.width, canvas.height/4);
  24. context.font = '60pt Calibri';
  25. context.shadowColor = "white";
  26. context.shadowOffsetX = 0;
  27. context.shadowOffsetY = 0;
  28. context.shadowBlur = 20;
  29. context.fillText("Blur Canvas", 40, 80);
  30. context.strokeStyle = "RGBA(0, 255, 0, 1)";
  31. context.lineWidth = 2;
  32. context.strokeText("Blur Canvas", 40, 80);
  33. // section two - shadow font
  34. var hh = canvas.height/4;
  35. context.fillStyle="white";
  36. context.fillRect(0, hh, canvas.width, canvas.height/4);
  37. context.font = '60pt Calibri';
  38. context.shadowColor = "RGBA(127,127,127,1)";
  39. context.shadowOffsetX = 3;
  40. context.shadowOffsetY = 3;
  41. context.shadowBlur = 0;
  42. context.fillStyle = "RGBA(0, 0, 0, 0.8)";
  43. context.fillText("Blur Canvas", 40, 80+hh);
  44. // section three - down shadow effect
  45. var hh = canvas.height/4 + hh;
  46. context.fillStyle="black";
  47. context.fillRect(0, hh, canvas.width, canvas.height/4);
  48. for(var i = 0; i < 10; i++)
  49. {
  50. context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
  51. context.shadowOffsetX = i*2;
  52. context.shadowOffsetY = i*2;
  53. context.shadowBlur = i*2;
  54. context.fillStyle = "RGBA(127, 127, 127, 1)";
  55. context.fillText("Blur Canvas", 40, 80+hh);
  56. }
  57. // section four - fade effect
  58. var hh = canvas.height/4 + hh;
  59. context.fillStyle="green";
  60. context.fillRect(0, hh, canvas.width, canvas.height/4);
  61. for(var i = 0; i < 10; i++)
  62. {
  63. context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
  64. context.shadowOffsetX = 0;
  65. context.shadowOffsetY = -i*2;
  66. context.shadowBlur = i*2;
  67. context.fillStyle = "RGBA(127, 127, 127, 1)";
  68. context.fillText("Blur Canvas", 40, 80+hh);
  69. }
  70. for(var i = 0; i < 10; i++)
  71. {
  72. context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
  73. context.shadowOffsetX = 0;
  74. context.shadowOffsetY = i*2;
  75. context.shadowBlur = i*2;
  76. context.fillStyle = "RGBA(127, 127, 127, 1)";
  77. context.fillText("Blur Canvas", 40, 80+hh);
  78. }
  79. for(var i = 0; i < 10; i++)
  80. {
  81. context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
  82. context.shadowOffsetX = i*2;
  83. context.shadowOffsetY = 0;
  84. context.shadowBlur = i*2;
  85. context.fillStyle = "RGBA(127, 127, 127, 1)";
  86. context.fillText("Blur Canvas", 40, 80+hh);
  87. }
  88. for(var i = 0; i < 10; i++)
  89. {
  90. context.shadowColor = "RGBA(255, 255, 255," + ((10-i)/10) + ")";
  91. context.shadowOffsetX = -i*2;
  92. context.shadowOffsetY = 0;
  93. context.shadowBlur = i*2;
  94. context.fillStyle = "RGBA(127, 127, 127, 1)";
  95. context.fillText("Blur Canvas", 40, 80+hh);
  96. }
  97. }
  98. </script>
  99. </head>
  100. <body>
  101. <h1>HTML5 Canvas Clip Demo - By Gloomy Fish</h1>
  102. <pre>Fill And Stroke Clip</pre>
  103. <div id="my_painter">
  104. <canvas id="text_canvas"></canvas>
  105. </div>
  106. </body>
  107. </html>

以上就是实例讲解使用HTML5 Canvas绘制阴影效果的方法_html5教程技巧的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!

人气教程排行