简体   繁体   English

如何在画布中(重新)填充圆圈-requestAnimationFrame-HTML,JS

[英]How to (re)fill circle in canvas - requestAnimationFrame - HTML, JS

how can I fill the "new" canvas circle that appears next to the older one. 如何填充较旧的画布旁边的“新”画布圆圈。 There is no problem with rectangle for example: 矩形没有问题,例如:

** ctx.fillStyle = 'rgba('+quadratto.r+','+quadratto.g+','+quadratto.b+',1)'; ** ctx.fillStyle ='rgba('+ quadratto.r +','+ quadratto.g +','++ quadratto.b +',1)';

  quadratto.x += quadratto.speedX;
  quadratto.y += quadratto.speedY;
  quadratto.speedY += quadratto.speedY*(-0.15);
  ctx.fillRect(quadratto.x-quadratto.h/4, quadratto.y-quadratto.h/2, 2, 2);**

What I want to do? 我想做的事? I'm creating animation in canvas where random-sized-color circle will appear and it will move in a specified direction. 我正在画布中创建动画,其中会出现随机大小的彩色圆圈,并且它将沿指定方向移动。 The new canvas layaer will appear in the next frame (fps) with a new(old) circle. 新的画布层将以新的(旧)圆圈出现在下一帧(fps)中。

 var myCanvasPattern = document.createElement('canvas'); myCanvasPattern.width = window.innerWidth; myCanvasPattern.height = window.innerHeight; document.body.appendChild(myCanvasPattern); var ryC = myCanvasPattern.getContext('2d'); function lottery(min, max) { return Math.floor(Math.random()*(max-min+1))+min; } var allQuadro = []; var fps = 50; var lastTime = 0; animationLoop(); function animationLoop(time){ requestAnimationFrame( animationLoop ); if(time-lastTime>=1000/fps){ lastTime = time; for(var i=0;i<10;i++){ allQuadro.push({ r : lottery(0, 240), g : lottery(0, 240), b : lottery(0, 240), circleR : lottery(10, 30), x : myCanvasPattern.width/2, y : myCanvasPattern.height/2, speedX : lottery(-1000,1000)/100, speedY : lottery(-1000,1000)/100 }) } ryC.fillStyle = 'rgba(255,255,255,0.2)'; ryC.fill(0,0,myCanvasPattern.width, myCanvasPattern.height); for(var i=0; i<allQuadro.length;i++){ var circle = allQuadro[i]; ryC.fillStyle = 'rgba('+circle.r+','+circle.g+','+circle.b+',1)'; circle.x += circle.speedX; circle.y += circle.speedY; //HERE's THE PROBLEM BELOW. HOW TO CREATE NEW ONE THAT APPEARS NEXT TO PREVIOUS ONE WITH NEW RANDOM COLOR ryC.arc(circle.x-circle.circleR/2, circle.y-circle.circleR/2, circleR, 0, 2 * Math.PI); //ryC.fill(); } // ryC.fillStyle = 'rgba('+r+','+g+','+b+',1)'; //ryC.arc(x+speedX, y+speedY, circleR, 0, 2 * Math.PI); //ryC.fill(); } } 
 body { margin: 0; padding: 0; overflow: hidden; } 

The fillRect() will fill directly to the canvas without going via a path (versus for example rect() ). fillRect()将直接填充到画布,而无需通过路径(例如rect() )。

The arc() on the other hand will add to a path which needs to be filled later. 另一方面, arc()将添加到以后需要填充的路径。 It also require the path to be cleared in-between the calls using beginPath() . 它还要求使用beginPath()在调用之间清除路径。

A simple way to think about it is to wrap the necessary code into a function that acts like fillRect() : 一种简单的思考方法是将必要的代码包装到一个类似于fillRect()的函数中:

function fillArc() {
  ctx.beginPath();                 // clear current path
  ctx.arc.apply(ctx, arguments);   // apply arguments to arc() call
  ctx.fill();
  // notice that the arc still exist on the path after this call
  // so to "truly" behave like fillRect() you could consider an
  // additional beginPath() here.. it will depend on your code
}

In action: 实际上:

 var ctx = c.getContext("2d"); ctx.fillStyle = "#09f"; fillArc(70, 70, 70, 0, 6.28); ctx.fillStyle = "#0a9"; fillArc(220, 70, 70, 0, 6.28); function fillArc() { ctx.beginPath(); ctx.arc.apply(ctx, arguments); ctx.fill(); } 
 <canvas id=c></canvas> 

If you are bold you can also add the method to the context itself before calling getContext() : 如果您大胆,也可以在调用getContext()之前将方法添加到上下文本身:

CanvasRenderingContext2D.prototype.fillArc = function() {
  this.beginPath();
  this.arc.apply(this, arguments);
  this.fill();
}

The use it like any other method: 像其他任何方法一样使用它:

ctx.fillArc( ... );

 CanvasRenderingContext2D.prototype.fillArc = function() { this.beginPath(); this.arc.apply(this, arguments); this.fill(); } var ctx = c.getContext("2d"); ctx.fillStyle = "#09f"; ctx.fillArc(70, 70, 70, 0, 6.28); ctx.fillStyle = "#0a9"; ctx.fillArc(220, 70, 70, 0, 6.28); 
 <canvas id=c></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM