使用requestAnimationFrame()时交替更改ctx.strokeStyle。

huangapple go评论110阅读模式
英文:

Alternating ctx.strokeStyle while using requestAnimationFrame()

问题

以下是您要翻译的内容:

"I'm trying to draw simple random motion as my landing page's background canvas, and I'm having a hard time getting the lines to have unique colors.

I've tried changing it after the moveTo as well, but didn't have success (just appeared to be a mix of the colors).

  1. const canvas = document.getElementById('myCanvas');
  2. const ctx = canvas.getContext('2d');
  3. //ctx.canvas.width = window.innerWidth;
  4. //ctx.canvas.height = window.innerHeight*4;
  5. const numSteps = ctx.canvas.width / 2 + 10;
  6. const stepSize = 10;
  7. const startX = 0;
  8. const startY = canvas.height / 2;
  9. const timeInterval = 15;
  10. var Point = function(color_inp){
  11. this.x = startX;
  12. this.y = startY;
  13. this.color = color_inp;
  14. }
  15. const colors = [
  16. '#FF1493', // Pink
  17. '#FF00FF', // Magenta
  18. '#800080', // Purple
  19. '#4B0082', // Indigo
  20. '#0000FF', // Blue
  21. '#00FFFF', // Cyan
  22. '#00FF00', // Green
  23. '#FFFF00', // Yellow
  24. '#FF7F00', // Orange
  25. '#8B4513' // Saddle Brown
  26. ];
  27. function drawRandomWalk(stPoint, steps) {
  28. ctx.globalAlpha = 0.01;
  29. let stepCount = 0;
  30. ctx.beginPath();
  31. ctx.strokeStyle = stPoint.color;
  32. ctx.moveTo(stPoint.x, stPoint.y);
  33. setTimeout(drawStep, 10);
  34. function drawStep() {
  35. if (stepCount >= steps) {
  36. return;
  37. }
  38. ctx.moveTo(stPoint.x, stPoint.y);
  39. const direction = Math.random() < 0.5 ? -1 : 1;
  40. stPoint.y += stepSize * direction;
  41. stPoint.x = startX + stepCount * 2;
  42. ctx.lineTo(stPoint.x, stPoint.y);
  43. ctx.lineWidth = 1;
  44. ctx.stroke();
  45. stepCount++;
  46. requestAnimationFrame(drawStep);
  47. }
  48. }
  49. for(const color of colors)
  50. {
  51. const startPoint = new Point(color);
  52. drawRandomWalk(startPoint, numSteps);
  53. }
  1. <canvas id="myCanvas" width="600" height="600"></canvas>

"

英文:

I'm trying to draw simple random motion as my landing page's background canvas, and I'm having a hard time getting the lines to have unique colors.

I've tried changing it after the moveTo as well, but didnt have success (just appeared to be a mix of the colors).

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const canvas = document.getElementById(&#39;myCanvas&#39;);
  2. const ctx = canvas.getContext(&#39;2d&#39;);
  3. //ctx.canvas.width = window.innerWidth;
  4. //ctx.canvas.height = window.innerHeight*4;
  5. const numSteps = ctx.canvas.width / 2 + 10;
  6. const stepSize = 10;
  7. const startX = 0;
  8. const startY = canvas.height / 2;
  9. const timeInterval = 15;
  10. var Point = function(color_inp){
  11. this.x = startX;
  12. this.y = startY;
  13. this.color = color_inp;
  14. }
  15. const colors = [
  16. &#39;#FF1493&#39;, // Pink
  17. &#39;#FF00FF&#39;, // Magenta
  18. &#39;#800080&#39;, // Purple
  19. &#39;#4B0082&#39;, // Indigo
  20. &#39;#0000FF&#39;, // Blue
  21. &#39;#00FFFF&#39;, // Cyan
  22. &#39;#00FF00&#39;, // Green
  23. &#39;#FFFF00&#39;, // Yellow
  24. &#39;#FF7F00&#39;, // Orange
  25. &#39;#8B4513&#39; // Saddle Brown
  26. ];
  27. function drawRandomWalk(stPoint, steps) {
  28. ctx.globalAlpha = 0.01;
  29. let stepCount = 0;
  30. ctx.beginPath();
  31. ctx.strokeStyle = stPoint.color;
  32. ctx.moveTo(stPoint.x, stPoint.y);
  33. setTimeout(drawStep, 10);
  34. function drawStep() {
  35. if (stepCount &gt;= steps) {
  36. return;
  37. }
  38. ctx.moveTo(stPoint.x, stPoint.y);
  39. const direction = Math.random() &lt; 0.5 ? -1 : 1;
  40. stPoint.y += stepSize * direction;
  41. stPoint.x = startX + stepCount * 2;
  42. ctx.lineTo(stPoint.x, stPoint.y);
  43. ctx.lineWidth = 1;
  44. ctx.stroke();
  45. stepCount++;
  46. requestAnimationFrame(drawStep);
  47. }
  48. }
  49. for(const color of colors)
  50. {
  51. const startPoint = new Point(color);
  52. drawRandomWalk(startPoint, numSteps);
  53. }

<!-- language: lang-html -->

  1. &lt;canvas id=&quot;myCanvas&quot; width=&quot;600&quot; height=&quot;600&quot;&gt;&lt;/canvas&gt;

<!-- end snippet -->

答案1

得分: 0

问题在于每支“笔”绘制线条时都共享状态 - 你正在绘制一个由许多movetos/linetos组成的单一长路径,一次又一次地绘制它。

如果你想要globalAlpha淡入效果(它依赖于能够一次又一次地绘制线条),你需要分别跟踪每支笔所绘制的路径,并像下面这样绘制它。

我去掉了Point结构,因为它实际上不是必需的。

  1. const canvas = document.getElementById('myCanvas');
  2. const ctx = canvas.getContext('2d');
  3. const numSteps = ctx.canvas.width / 2 + 10;
  4. const stepSize = 10;
  5. const startX = 0;
  6. const startY = canvas.height / 2;
  7. const timeInterval = 15;
  8. const colors = [
  9. '#FF1493', // Pink
  10. '#FF00FF', // Magenta
  11. '#800080', // Purple
  12. '#4B0082', // Indigo
  13. '#0000FF', // Blue
  14. '#00FFFF', // Cyan
  15. '#00FF00', // Green
  16. '#FFFF00', // Yellow
  17. '#FF7F00', // Orange
  18. '#8B4513' // Saddle Brown
  19. ];
  20. function drawRandomWalk(startX, startY, color, nSteps) {
  21. setTimeout(drawStep, 10);
  22. const steps = [[startX, startY]];
  23. function drawStep() {
  24. if (steps.length >= nSteps) {
  25. return;
  26. }
  27. // Draw current line
  28. ctx.globalAlpha = 0.01;
  29. ctx.beginPath();
  30. ctx.strokeStyle = color;
  31. ctx.lineWidth = 1;
  32. let x = 0, y = 0;
  33. for(let i = 0; i < steps.length; i++) {
  34. [x, y] = steps[i];
  35. if(i === 0) ctx.moveTo(x, y);
  36. else ctx.lineTo(x, y);
  37. }
  38. ctx.stroke();
  39. // Compute next point
  40. const direction = Math.random() < 0.5 ? -1 : 1;
  41. y += stepSize * direction;
  42. x = startX + steps.length * 2;
  43. steps.push([x, y]);
  44. requestAnimationFrame(drawStep);
  45. }
  46. }
  47. for(const color of colors)
  48. {
  49. drawRandomWalk(startX, startY, color, numSteps);
  50. }
  1. <canvas id="myCanvas" width="600" height="600"></canvas>

你只需要复制上述的代码,然后粘贴到你的项目中即可。

英文:

The issue is each of the "pens" drawing the lines are sharing state – you're drawing a single long path that consists of many movetos/linetos over and over and again.

If you want the globalAlpha fade effect (which relies on being able to draw the line over and over again), you'll need to separately keep track of the trail drawn by each pen, and draw it, like below.

I got rid of the Point structure, since it's not really needed.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const canvas = document.getElementById(&#39;myCanvas&#39;);
  2. const ctx = canvas.getContext(&#39;2d&#39;);
  3. const numSteps = ctx.canvas.width / 2 + 10;
  4. const stepSize = 10;
  5. const startX = 0;
  6. const startY = canvas.height / 2;
  7. const timeInterval = 15;
  8. const colors = [
  9. &#39;#FF1493&#39;, // Pink
  10. &#39;#FF00FF&#39;, // Magenta
  11. &#39;#800080&#39;, // Purple
  12. &#39;#4B0082&#39;, // Indigo
  13. &#39;#0000FF&#39;, // Blue
  14. &#39;#00FFFF&#39;, // Cyan
  15. &#39;#00FF00&#39;, // Green
  16. &#39;#FFFF00&#39;, // Yellow
  17. &#39;#FF7F00&#39;, // Orange
  18. &#39;#8B4513&#39; // Saddle Brown
  19. ];
  20. function drawRandomWalk(startX, startY, color, nSteps) {
  21. setTimeout(drawStep, 10);
  22. const steps = [[startX, startY]];
  23. function drawStep() {
  24. if (steps.length &gt;= nSteps) {
  25. return;
  26. }
  27. // Draw current line
  28. ctx.globalAlpha = 0.01;
  29. ctx.beginPath();
  30. ctx.strokeStyle = color;
  31. ctx.lineWidth = 1;
  32. let x = 0, y = 0;
  33. for(let i = 0; i &lt; steps.length; i++) {
  34. [x, y] = steps[i];
  35. if(i === 0) ctx.moveTo(x, y);
  36. else ctx.lineTo(x, y);
  37. }
  38. ctx.stroke();
  39. // Compute next point
  40. const direction = Math.random() &lt; 0.5 ? -1 : 1;
  41. y += stepSize * direction;
  42. x = startX + steps.length * 2;
  43. steps.push([x, y]);
  44. requestAnimationFrame(drawStep);
  45. }
  46. }
  47. for(const color of colors)
  48. {
  49. drawRandomWalk(startX, startY, color, numSteps);
  50. }

<!-- language: lang-html -->

  1. &lt;canvas id=&quot;myCanvas&quot; width=&quot;600&quot; height=&quot;600&quot;&gt;&lt;/canvas&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月31日 19:39:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定