关键词搜索

源码搜索 ×
×

canvas笔记-clip裁剪函数的使用及探照灯实例

发布2020-06-03浏览1613次

详情内容

在canvas中有clip函数,也就是裁剪,从原始画布中剪切任意形状和尺寸。

 

如下例子:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. window.onload = function(){
  13. let canvas = document.getElementById("canvas");
  14. canvas.width = 800;
  15. canvas.height = 800;
  16. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  17. context.beginPath();
  18. context.fillStyle = "black";
  19. context.fillRect(0, 0, canvas.width, canvas.height);
  20. context.beginPath();
  21. context.strokeStyle = "green";
  22. context.arc(400, 400, 150, 0, Math.PI * 2);
  23. context.stroke();
  24. context.font = "bold 150px Arial";
  25. context.textAlign = "center";
  26. context.textBaseline = "middle";
  27. context.fillStyle = "#058";
  28. context.fillText("CANVAS", canvas.width / 2, canvas.height / 2);
  29. }
  30. </script>
  31. </body>
  32. </html>

程序运行截图如下:

添加clip代码后运行截图如下:

源码如下:

  1. <script>
  2. window.onload = function(){
  3. let canvas = document.getElementById("canvas");
  4. canvas.width = 800;
  5. canvas.height = 800;
  6. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  7. context.beginPath();
  8. context.fillStyle = "black";
  9. context.fillRect(0, 0, canvas.width, canvas.height);
  10. context.beginPath();
  11. //context.strokeStyle = "green";
  12. context.arc(400, 400, 150, 0, Math.PI * 2);
  13. //context.stroke();
  14. context.clip();
  15. context.font = "bold 150px Arial";
  16. context.textAlign = "center";
  17. context.textBaseline = "middle";
  18. context.fillStyle = "#058";
  19. context.fillText("CANVAS", canvas.width / 2, canvas.height / 2);
  20. }
  21. </script>

 

下面记录下探照灯的例子:

程序运行截图如下,那个探照灯的圆是会动的。

源码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
  9. 当前浏览器不支持canvas
  10. </canvas>
  11. <script>
  12. let searchLight = {
  13. x: 400,
  14. y: 400,
  15. radius: 150,
  16. vx: Math.random() * 5 + 10,
  17. vy: Math.random() * 5 + 10
  18. }
  19. window.onload = function(){
  20. let canvas = document.getElementById("canvas");
  21. canvas.width = 800;
  22. canvas.height = 800;
  23. let context = canvas.getContext("https://cdn.jxasp.com:9143/image/2d");
  24. setInterval(function(){
  25. draw(context);
  26. update(canvas.width, canvas.height);
  27. }, 40)
  28. }
  29. function draw(cxt){
  30. let canvas = cxt.canvas;
  31. cxt.clearRect(0, 0, canvas.width, canvas.height);
  32. cxt.save();
  33. cxt.beginPath();
  34. cxt.fillStyle = "black";
  35. cxt.fillRect(0, 0, canvas.width, canvas.height);
  36. cxt.beginPath();
  37. cxt.arc(searchLight.x, searchLight.y, searchLight.radius, 0, Math.PI * 2);
  38. cxt.fillStyle = "#fff";
  39. cxt.fill();
  40. cxt.clip();
  41. cxt.font = "bold 150px Arial";
  42. cxt.textAlign = "center";
  43. cxt.textBaseline = "middle";
  44. cxt.fillStyle = "#058";
  45. cxt.fillText("CANVAS", canvas.width / 2, canvas.height / 4);
  46. cxt.fillText("CANVAS", canvas.width / 2, canvas.height / 2);
  47. cxt.fillText("CANVAS", canvas.width / 2, canvas.height * 3 / 4);
  48. cxt.restore();
  49. }
  50. function update(canvasWidth, canvasHeight){
  51. searchLight.x += searchLight.vx;
  52. searchLight.y += searchLight.vy;
  53. if(searchLight.x - searchLight.radius <= 0){
  54. searchLight.vx = -searchLight.vx;
  55. searchLight.x = searchLight.radius;
  56. }
  57. if(searchLight.x + searchLight.radius >= canvasWidth){
  58. searchLight.vx = -searchLight.vx;
  59. searchLight.x = canvasWidth - searchLight.radius;
  60. }
  61. if(searchLight.y - searchLight.radius <= 0){
  62. searchLight.vy = -searchLight.vy;
  63. searchLight.y = searchLight.radius;
  64. }
  65. if(searchLight.y + searchLight.radius >= canvasHeight){
  66. searchLight.vy = -searchLight.vy;
  67. searchLight.y = canvasHeight - searchLight.radius;
  68. }
  69. }
  70. </script>
  71. </body>
  72. </html>

这里说明下实现的逻辑,碰撞检测,清空画布方面的不提了。

就提下这个draw。

这里是先绘制出了一个全黑的矩形,这个矩形和画布一样大。然后再绘制一个圆,这个圆是白色的,然后裁剪,这样的画,显示的时候就只会显示圆。然后就搞3个canvas字绘制到画布上。再加上运动效果,就变成探照灯实例了。

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载