关键词搜索

源码搜索 ×
×

canvas笔记-线性渐变与非线性渐变

发布2020-06-01浏览1003次

详情内容

首先来看下线性渐变

  1. //第一步
  2. let grd = context.createLinearGradient(xstart, ystart, xend, yend);
  3. //第二步
  4. Grd.addColorStop(stop, color);

 

程序运行截图如下:

源码如下:

  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.save();
  18. let line1 = context.createLinearGradient(0, 0, 400, 800);
  19. line1.addColorStop(0.0, "#fff");
  20. line1.addColorStop(1.0, "#000");
  21. context.fillStyle = line1;
  22. context.fillRect(0, 0, 400, 800);
  23. context.restore();
  24. context.save();
  25. let line2 = context.createLinearGradient(400, 0, 400, 800);
  26. line2.addColorStop(0.0, "red");
  27. line2.addColorStop(0.5, "blue");
  28. line2.addColorStop(1.0, "green");
  29. context.fillStyle = line2;
  30. context.fillRect(400, 0, 400, 800);
  31. context.restore();
  32. }
  33. </script>
  34. </body>
  35. </html>

 

下面是非线性变换

  1. let grd = context.createRadialGradient(x0, y0, r0, x1, y1, r1);
  2. grd.addColorStop(stop, color);

程序运行截图如下:

代码如下:

  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. let radialGrad = context.createRadialGradient(400, 400, 0, 400, 400, 500);
  18. radialGrad.addColorStop(0.0, "white");
  19. radialGrad.addColorStop(0.25, "yellow");
  20. radialGrad.addColorStop(0.5, "green");
  21. radialGrad.addColorStop(0.75, "blue");
  22. radialGrad.addColorStop(1.0, "black");
  23. context.fillStyle = radialGrad;
  24. context.fillRect(0, 0, 800, 800);
  25. }
  26. </script>
  27. </body>
  28. </html>

 

 

相关技术文章

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

提示信息

×

选择支付方式

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